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 */
032public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Serializable {
033
034    /** Serialization version */
035    private static final long serialVersionUID = -8453869361373831205L;
036
037    /** The map underlying the entry/iterator */
038    private final Map<K, V> map;
039
040    /** The key */
041    private final K key;
042
043    /**
044     * Constructs a new entry with the given Map and key.
045     *
046     * @param map  the map
047     * @param key  the key
048     */
049    public TiedMapEntry(final Map<K, V> map, final K key) {
050        super();
051        this.map = map;
052        this.key = key;
053    }
054
055    // Map.Entry interface
056    //-------------------------------------------------------------------------
057    /**
058     * Gets the key of this entry
059     *
060     * @return the key
061     */
062    @Override
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    @Override
073    public V getValue() {
074        return map.get(key);
075    }
076
077    /**
078     * Sets the value associated with the key direct onto the map.
079     *
080     * @param value  the new value
081     * @return the old value
082     * @throws IllegalArgumentException if the value is set to this map entry
083     */
084    @Override
085    public V setValue(final V value) {
086        if (value == this) {
087            throw new IllegalArgumentException("Cannot set value to this map entry");
088        }
089        return map.put(key, value);
090    }
091
092    /**
093     * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
094     * <p>
095     * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
096     *
097     * @param obj  the object to compare to
098     * @return true if equal key and value
099     */
100    @Override
101    public boolean equals(final Object obj) {
102        if (obj == this) {
103            return true;
104        }
105        if (obj instanceof Map.Entry == false) {
106            return false;
107        }
108        final Map.Entry<?,?> other = (Map.Entry<?,?>) obj;
109        final Object value = getValue();
110        return
111            (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
112            (value == null ? other.getValue() == null : value.equals(other.getValue()));
113    }
114
115    /**
116     * Gets a hashCode compatible with the equals method.
117     * <p>
118     * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
119     *
120     * @return a suitable hash code
121     */
122    @Override
123    public int hashCode() {
124        final Object value = getValue();
125        return (getKey() == null ? 0 : getKey().hashCode()) ^
126               (value == null ? 0 : value.hashCode());
127    }
128
129    /**
130     * Gets a string version of the entry.
131     *
132     * @return entry as a string
133     */
134    @Override
135    public String toString() {
136        return getKey() + "=" + getValue();
137    }
138
139}