TiedMapEntry.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.keyvalue;

  18. import java.io.Serializable;
  19. import java.util.Map;
  20. import java.util.Objects;

  21. import org.apache.commons.collections4.KeyValue;

  22. /**
  23.  * A {@link java.util.Map.Entry Map.Entry} tied to a map underneath.
  24.  * <p>
  25.  * This can be used to enable a map entry to make changes on the underlying
  26.  * map, however this will probably mess up any iterators.
  27.  * </p>
  28.  *
  29.  * @param <K> the type of keys
  30.  * @param <V> the type of mapped values
  31.  * @since 3.0
  32.  */
  33. public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Serializable {

  34.     /** Serialization version */
  35.     private static final long serialVersionUID = -8453869361373831205L;

  36.     /** The map underlying the entry/iterator */
  37.     private final Map<K, V> map;

  38.     /** The key */
  39.     private final K key;

  40.     /**
  41.      * Constructs a new entry with the given Map and key.
  42.      *
  43.      * @param map  the map
  44.      * @param key  the key
  45.      */
  46.     public TiedMapEntry(final Map<K, V> map, final K key) {
  47.         this.map = map;
  48.         this.key = key;
  49.     }

  50.     /**
  51.      * Compares this {@code Map.Entry} with another {@code Map.Entry}.
  52.      * <p>
  53.      * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
  54.      *
  55.      * @param obj  the object to compare to
  56.      * @return true if equal key and value
  57.      */
  58.     @Override
  59.     public boolean equals(final Object obj) {
  60.         if (obj == this) {
  61.             return true;
  62.         }
  63.         if (!(obj instanceof Map.Entry)) {
  64.             return false;
  65.         }
  66.         final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
  67.         return
  68.             Objects.equals(key, other.getKey()) &&
  69.             Objects.equals(getValue(), other.getValue());
  70.     }

  71.     /**
  72.      * Gets the key of this entry
  73.      *
  74.      * @return the key
  75.      */
  76.     @Override
  77.     public K getKey() {
  78.         return key;
  79.     }

  80.     /**
  81.      * Gets the value of this entry direct from the map.
  82.      *
  83.      * @return the value
  84.      */
  85.     @Override
  86.     public V getValue() {
  87.         return map.get(key);
  88.     }

  89.     /**
  90.      * Gets a hashCode compatible with the equals method.
  91.      * <p>
  92.      * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
  93.      *
  94.      * @return a suitable hash code
  95.      */
  96.     @Override
  97.     public int hashCode() {
  98.         final Object value = getValue();
  99.         return (getKey() == null ? 0 : getKey().hashCode()) ^
  100.                (value == null ? 0 : value.hashCode());
  101.     }

  102.     /**
  103.      * Sets the value associated with the key direct onto the map.
  104.      *
  105.      * @param value  the new value
  106.      * @return the old value
  107.      * @throws IllegalArgumentException if the value is set to this map entry
  108.      */
  109.     @Override
  110.     public V setValue(final V value) {
  111.         if (value == this) {
  112.             throw new IllegalArgumentException("Cannot set value to this map entry");
  113.         }
  114.         return map.put(key, value);
  115.     }

  116.     /**
  117.      * Gets a string version of the entry.
  118.      *
  119.      * @return entry as a string
  120.      */
  121.     @Override
  122.     public String toString() {
  123.         return getKey() + "=" + getValue();
  124.     }

  125. }