View Javadoc
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  
19  import java.io.Serializable;
20  import java.util.Map;
21  
22  import org.apache.commons.collections4.KeyValue;
23  
24  /**
25   * A {@link java.util.Map.Entry Map.Entry} tied to a map underneath.
26   * <p>
27   * This can be used to enable a map entry to make changes on the underlying
28   * map, however this will probably mess up any iterators.
29   * </p>
30   *
31   * @param <K> the type of keys
32   * @param <V> the type of mapped values
33   * @since 3.0
34   */
35  public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Serializable {
36  
37      /** Serialization version */
38      private static final long serialVersionUID = -8453869361373831205L;
39  
40      /** The map underlying the entry/iterator */
41      private final Map<K, V> map;
42  
43      /** The key */
44      private final K key;
45  
46      /**
47       * Constructs a new entry with the given Map and key.
48       *
49       * @param map  the map
50       * @param key  the key
51       */
52      public TiedMapEntry(final Map<K, V> map, final K key) {
53          this.map = map;
54          this.key = key;
55      }
56  
57      /**
58       * Compares this {@code Map.Entry} with another {@code Map.Entry}.
59       * <p>
60       * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
61       *
62       * @param obj  the object to compare to
63       * @return true if equal key and value
64       */
65      @Override
66      public boolean equals(final Object obj) {
67          if (obj == this) {
68              return true;
69          }
70          if (!(obj instanceof Map.Entry)) {
71              return false;
72          }
73          final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
74          final Object value = getValue();
75          return
76              (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
77              (value == null ? other.getValue() == null : value.equals(other.getValue()));
78      }
79  
80      /**
81       * Gets the key of this entry
82       *
83       * @return the key
84       */
85      @Override
86      public K getKey() {
87          return key;
88      }
89  
90      /**
91       * Gets the value of this entry direct from the map.
92       *
93       * @return the value
94       */
95      @Override
96      public V getValue() {
97          return map.get(key);
98      }
99  
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 }