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;
021import java.util.Objects;
022
023import org.apache.commons.collections4.KeyValue;
024
025/**
026 * A {@link java.util.Map.Entry Map.Entry} tied to a map underneath.
027 * <p>
028 * This can be used to enable a map entry to make changes on the underlying
029 * map, however this will probably mess up any iterators.
030 * </p>
031 *
032 * @param <K> the type of keys
033 * @param <V> the type of mapped values
034 * @since 3.0
035 */
036public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Serializable {
037
038    /** Serialization version */
039    private static final long serialVersionUID = -8453869361373831205L;
040
041    /** The map underlying the entry/iterator */
042    private final Map<K, V> map;
043
044    /** The key */
045    private final K key;
046
047    /**
048     * Constructs a new entry with the given Map and key.
049     *
050     * @param map  the map
051     * @param key  the key
052     */
053    public TiedMapEntry(final Map<K, V> map, final K key) {
054        this.map = map;
055        this.key = key;
056    }
057
058    /**
059     * Compares this {@code Map.Entry} with another {@code Map.Entry}.
060     * <p>
061     * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
062     *
063     * @param obj  the object to compare to
064     * @return true if equal key and value
065     */
066    @Override
067    public boolean equals(final Object obj) {
068        if (obj == this) {
069            return true;
070        }
071        if (!(obj instanceof Map.Entry)) {
072            return false;
073        }
074        final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
075        return
076            Objects.equals(key, other.getKey()) &&
077            Objects.equals(getValue(), 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}