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