001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.keyvalue;
018
019import java.util.Map;
020import java.util.Map.Entry;
021import java.util.Objects;
022
023/**
024 * Abstract Pair class to assist with creating correct
025 * {@link Entry Map.Entry} implementations.
026 *
027 * @param <K> The type of keys
028 * @param <V> The type of mapped values
029 * @since 3.0
030 */
031public abstract class AbstractMapEntry<K, V> extends AbstractKeyValue<K, V> implements Map.Entry<K, V> {
032
033    /**
034     * Constructs a new entry with the given key and given value.
035     *
036     * @param key  The key for the entry, may be null
037     * @param value  The value for the entry, may be null
038     */
039    protected AbstractMapEntry(final K key, final V value) {
040        super(key, value);
041    }
042
043    /**
044     * Compares this {@code Map.Entry} with another {@code Map.Entry}.
045     * <p>
046     * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
047     *
048     * @param obj  The object to compare to
049     * @return true if equal key and value
050     */
051    @Override
052    public boolean equals(final Object obj) {
053        if (obj == this) {
054            return true;
055        }
056        if (!(obj instanceof Map.Entry)) {
057            return false;
058        }
059        final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
060        return Objects.equals(getKey(), other.getKey()) &&
061               Objects.equals(getValue(), other.getValue());
062    }
063
064    /**
065     * Gets a hashCode compatible with the equals method.
066     * <p>
067     * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
068     *
069     * @return A suitable hash code
070     */
071    @Override
072    public int hashCode() {
073        return (getKey() == null ? 0 : getKey().hashCode()) ^
074               (getValue() == null ? 0 : getValue().hashCode());
075    }
076
077    /**
078     * Sets the value stored in this {@code Map.Entry}.
079     * <p>
080     * This {@code Map.Entry} is not connected to a Map, so only the
081     * local data is changed.
082     *
083     * @param value  The new value
084     * @return The previous value
085     */
086    @Override
087    public V setValue(final V value) { // NOPMD
088        return super.setValue(value);
089    }
090
091}