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.io.Serializable;
020import java.util.Map;
021
022import org.apache.commons.collections4.KeyValue;
023
024/**
025 * A {@link java.util.Map.Entry Map.Entry} tied to a map underneath.
026 * <p>
027 * This can be used to enable a map entry to make changes on the underlying
028 * map, however this will probably mess up any iterators.
029 *
030 * @since 3.0
031 * @version $Id: TiedMapEntry.html 972421 2015-11-14 20:00:04Z tn $
032 */
033public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Serializable {
034
035    /** Serialization version */
036    private static final long serialVersionUID = -8453869361373831205L;
037
038    /** The map underlying the entry/iterator */
039    private final Map<K, V> map;
040
041    /** The key */
042    private final K key;
043
044    /**
045     * Constructs a new entry with the given Map and key.
046     *
047     * @param map  the map
048     * @param key  the key
049     */
050    public TiedMapEntry(final Map<K, V> map, final K key) {
051        super();
052        this.map = map;
053        this.key = key;
054    }
055
056    // Map.Entry interface
057    //-------------------------------------------------------------------------
058    /**
059     * Gets the key of this entry
060     *
061     * @return the key
062     */
063    public K getKey() {
064        return key;
065    }
066
067    /**
068     * Gets the value of this entry direct from the map.
069     *
070     * @return the value
071     */
072    public V getValue() {
073        return map.get(key);
074    }
075
076    /**
077     * Sets the value associated with the key direct onto the map.
078     *
079     * @param value  the new value
080     * @return the old value
081     * @throws IllegalArgumentException if the value is set to this map entry
082     */
083    public V setValue(final V value) {
084        if (value == this) {
085            throw new IllegalArgumentException("Cannot set value to this map entry");
086        }
087        return map.put(key, value);
088    }
089
090    /**
091     * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
092     * <p>
093     * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
094     *
095     * @param obj  the object to compare to
096     * @return true if equal key and value
097     */
098    @Override
099    public boolean equals(final Object obj) {
100        if (obj == this) {
101            return true;
102        }
103        if (obj instanceof Map.Entry == false) {
104            return false;
105        }
106        final Map.Entry<?,?> other = (Map.Entry<?,?>) obj;
107        final Object value = getValue();
108        return
109            (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
110            (value == null ? other.getValue() == null : value.equals(other.getValue()));
111    }
112
113    /**
114     * Gets a hashCode compatible with the equals method.
115     * <p>
116     * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
117     *
118     * @return a suitable hash code
119     */
120    @Override
121    public int hashCode() {
122        final Object value = getValue();
123        return (getKey() == null ? 0 : getKey().hashCode()) ^
124               (value == null ? 0 : value.hashCode());
125    }
126
127    /**
128     * Gets a string version of the entry.
129     *
130     * @return entry as a string
131     */
132    @Override
133    public String toString() {
134        return getKey() + "=" + getValue();
135    }
136
137}