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    *      https://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  import java.util.Map.Entry;
22  import java.util.Objects;
23  
24  import org.apache.commons.collections4.KeyValue;
25  
26  /**
27   * A {@link Entry Map.Entry} tied to a map underneath.
28   * <p>
29   * This can be used to enable a map entry to make changes on the underlying
30   * map, however this will probably mess up any iterators.
31   * </p>
32   *
33   * @param <K> The type of keys
34   * @param <V> The type of mapped values
35   * @since 3.0
36   */
37  public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Serializable {
38  
39      /** Serialization version */
40      private static final long serialVersionUID = -8453869361373831205L;
41  
42      /** The map underlying the entry/iterator */
43      private final Map<K, V> map;
44  
45      /** The key */
46      private final K key;
47  
48      /**
49       * Constructs a new entry with the given Map and key.
50       *
51       * @param map  The map
52       * @param key  The key
53       */
54      public TiedMapEntry(final Map<K, V> map, final K key) {
55          this.map = map;
56          this.key = key;
57      }
58  
59      /**
60       * Compares this {@code Map.Entry} with another {@code Map.Entry}.
61       * <p>
62       * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
63       *
64       * @param obj  The object to compare to
65       * @return true if equal key and value
66       */
67      @Override
68      public boolean equals(final Object obj) {
69          if (obj == this) {
70              return true;
71          }
72          if (!(obj instanceof Map.Entry)) {
73              return false;
74          }
75          final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
76          return
77              Objects.equals(key, other.getKey()) &&
78              Objects.equals(getValue(), other.getValue());
79      }
80  
81      /**
82       * Gets the key of this entry
83       *
84       * @return The key
85       */
86      @Override
87      public K getKey() {
88          return key;
89      }
90  
91      /**
92       * Gets the value of this entry direct from the map.
93       *
94       * @return The value
95       */
96      @Override
97      public V getValue() {
98          return map.get(key);
99      }
100 
101     /**
102      * Gets a hashCode compatible with the equals method.
103      * <p>
104      * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
105      *
106      * @return A suitable hash code
107      */
108     @Override
109     public int hashCode() {
110         final Object value = getValue();
111         return (getKey() == null ? 0 : getKey().hashCode()) ^
112                (value == null ? 0 : value.hashCode());
113     }
114 
115     /**
116      * Sets the value associated with the key direct onto the map.
117      *
118      * @param value  The new value
119      * @return The old value
120      * @throws IllegalArgumentException if the value is set to this map entry
121      */
122     @Override
123     public V setValue(final V value) {
124         if (value == this) {
125             throw new IllegalArgumentException("Cannot set value to this map entry");
126         }
127         return map.put(key, value);
128     }
129 
130     /**
131      * Gets a string version of the entry.
132      *
133      * @return entry as a string
134      */
135     @Override
136     public String toString() {
137         return getKey() + "=" + getValue();
138     }
139 
140 }