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 *      http://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;
020
021/**
022 * Abstract Pair class to assist with creating correct
023 * {@link java.util.Map.Entry Map.Entry} implementations.
024 *
025 * @since 3.0
026 */
027public abstract class AbstractMapEntry<K, V> extends AbstractKeyValue<K, V> implements Map.Entry<K, V> {
028
029    /**
030     * Constructs a new entry with the given key and given value.
031     *
032     * @param key  the key for the entry, may be null
033     * @param value  the value for the entry, may be null
034     */
035    protected AbstractMapEntry(final K key, final V value) {
036        super(key, value);
037    }
038
039    // Map.Entry interface
040    //-------------------------------------------------------------------------
041    /**
042     * Sets the value stored in this <code>Map.Entry</code>.
043     * <p>
044     * This <code>Map.Entry</code> is not connected to a Map, so only the
045     * local data is changed.
046     *
047     * @param value  the new value
048     * @return the previous value
049     */
050    @Override
051    public V setValue(final V value) { // NOPMD
052        return super.setValue(value);
053    }
054
055    /**
056     * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
057     * <p>
058     * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
059     *
060     * @param obj  the object to compare to
061     * @return true if equal key and value
062     */
063    @Override
064    public boolean equals(final Object obj) {
065        if (obj == this) {
066            return true;
067        }
068        if (obj instanceof Map.Entry == false) {
069            return false;
070        }
071        final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
072        return
073            (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
074            (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
075    }
076
077    /**
078     * Gets a hashCode compatible with the equals method.
079     * <p>
080     * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
081     *
082     * @return a suitable hash code
083     */
084    @Override
085    public int hashCode() {
086        return (getKey() == null ? 0 : getKey().hashCode()) ^
087               (getValue() == null ? 0 : getValue().hashCode());
088    }
089
090}