1 16 package org.apache.commons.collections.keyvalue; 17 18 import java.io.Serializable ; 19 import java.util.Map ; 20 21 import org.apache.commons.collections.KeyValue; 22 23 34 public class TiedMapEntry implements Map.Entry , KeyValue, Serializable { 35 36 37 private static final long serialVersionUID = -8453869361373831205L; 38 39 40 private final Map map; 41 42 private final Object key; 43 44 50 public TiedMapEntry(Map map, Object key) { 51 super(); 52 this.map = map; 53 this.key = key; 54 } 55 56 63 public Object getKey() { 64 return key; 65 } 66 67 72 public Object getValue() { 73 return map.get(key); 74 } 75 76 83 public Object setValue(Object value) { 84 if (value == this) { 85 throw new IllegalArgumentException ("Cannot set value to this map entry"); 86 } 87 return map.put(key, value); 88 } 89 90 98 public boolean equals(Object obj) { 99 if (obj == this) { 100 return true; 101 } 102 if (obj instanceof Map.Entry == false) { 103 return false; 104 } 105 Map.Entry other = (Map.Entry ) obj; 106 Object value = getValue(); 107 return 108 (key == null ? other.getKey() == null : key.equals(other.getKey())) && 109 (value == null ? other.getValue() == null : value.equals(other.getValue())); 110 } 111 112 119 public int hashCode() { 120 Object value = getValue(); 121 return (getKey() == null ? 0 : getKey().hashCode()) ^ 122 (value == null ? 0 : value.hashCode()); 123 } 124 125 130 public String toString() { 131 return getKey() + "=" + getValue(); 132 } 133 134 } 135 | Popular Tags |