View Javadoc
1   package edu.uci.ics.jung.algorithms.util;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   /**
7    * An simple minimal implementation of <code>Map.Entry</code>.
8    *
9    * @param <K> the key type
10   * @param <V> the value type
11   */
12  public class BasicMapEntry<K,V> implements Map.Entry<K,V> {
13      final K key;
14      V value;
15      
16      /**
17       * @param k the key
18       * @param v the value
19       */
20      public BasicMapEntry(K k, V v) {
21          value = v;
22          key = k;
23      }
24  
25      public K getKey() {
26          return key;
27      }
28  
29      public V getValue() {
30          return value;
31      }
32  
33      public V setValue(V newValue) {
34      V oldValue = value;
35          value = newValue;
36          return oldValue;
37      }
38  
39      @Override
40      public boolean equals(Object o) {
41          if (!(o instanceof Map.Entry))
42              return false;
43          @SuppressWarnings("rawtypes")
44  		Map.Entry e = (Map.Entry)o;
45          Object k1 = getKey();
46          Object k2 = e.getKey();
47          if (k1 == k2 || (k1 != null && k1.equals(k2))) {
48              Object v1 = getValue();
49              Object v2 = e.getValue();
50              if (v1 == v2 || (v1 != null && v1.equals(v2))) 
51                  return true;
52          }
53          return false;
54      }
55  
56      @Override
57      public int hashCode() {
58          return (key==null ? 0 : key.hashCode()) ^
59                 (value==null   ? 0 : value.hashCode());
60      }
61  
62      @Override
63      public String toString() {
64          return getKey() + "=" + getValue();
65      }
66  
67      /**
68       * This method is invoked whenever the value in an entry is
69       * overwritten by an invocation of put(k,v) for a key k that's already
70       * in the HashMap.
71       */
72      void recordAccess(HashMap<K,V> m) {
73      }
74  
75      /**
76       * This method is invoked whenever the entry is
77       * removed from the table.
78       */
79      void recordRemoval(HashMap<K,V> m) {
80      }
81  }