001: /**
002: *
003: */package clime.messadmin.utils;
004:
005: import java.io.Serializable;
006: import java.util.Map;
007:
008: /**
009: * An Entry maintaining a key and a value. The value may be
010: * changed using the <tt>setValue</tt> method. This class
011: * facilitates the process of building custom map
012: * implementations. For example, it may be convenient to return
013: * arrays of <tt>SimpleEntry</tt> instances in method
014: * <tt>Map.entrySet().toArray</tt>.
015: *
016: * @since 1.6
017: * @author Cédrik LIME
018: */
019: public class SimpleEntry implements Map.Entry, Serializable {
020: private final Object key;
021: private Object value;
022:
023: /**
024: * Creates an entry representing a mapping from the specified
025: * key to the specified value.
026: *
027: * @param key the key represented by this entry
028: * @param value the value represented by this entry
029: */
030: public SimpleEntry(Object key, Object value) {
031: this .key = key;
032: this .value = value;
033: }
034:
035: /**
036: * Creates an entry representing the same mapping as the
037: * specified entry.
038: *
039: * @param entry the entry to copy
040: */
041: public SimpleEntry(Map.Entry entry) {
042: this .key = entry.getKey();
043: this .value = entry.getValue();
044: }
045:
046: /**
047: * @return the key corresponding to this entry
048: */
049: public Object getKey() {
050: return key;
051: }
052:
053: /**
054: * @return the value corresponding to this entry
055: */
056: public Object getValue() {
057: return value;
058: }
059:
060: /**
061: * Replaces the value corresponding to this entry with the specified
062: * value.
063: *
064: * @param value new value to be stored in this entry
065: * @return the old value corresponding to the entry
066: */
067: public Object setValue(Object value) {
068: Object oldValue = this .value;
069: this .value = value;
070: return oldValue;
071: }
072:
073: /**
074: * Compares the specified object with this entry for equality.
075: * Returns {@code true} if the given object is also a map entry and
076: * the two entries represent the same mapping. More formally, two
077: * entries {@code e1} and {@code e2} represent the same mapping
078: * if<pre>
079: * (e1.getKey()==null ?
080: * e2.getKey()==null :
081: * e1.getKey().equals(e2.getKey()))
082: * &&
083: * (e1.getValue()==null ?
084: * e2.getValue()==null :
085: * e1.getValue().equals(e2.getValue()))</pre>
086: * This ensures that the {@code equals} method works properly across
087: * different implementations of the {@code Map.Entry} interface.
088: *
089: * @param o object to be compared for equality with this map entry
090: * @return {@code true} if the specified object is equal to this map
091: * entry
092: * @see #hashCode
093: */
094: public boolean equals(Object o) {
095: if (!(o instanceof Map.Entry))
096: return false;
097: Map.Entry e = (Map.Entry) o;
098: return eq(key, e.getKey()) && eq(value, e.getValue());
099: }
100:
101: /**
102: * Returns the hash code value for this map entry. The hash code
103: * of a map entry {@code e} is defined to be: <pre>
104: * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
105: * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
106: * This ensures that {@code e1.equals(e2)} implies that
107: * {@code e1.hashCode()==e2.hashCode()} for any two Entries
108: * {@code e1} and {@code e2}, as required by the general
109: * contract of {@link Object#hashCode}.
110: *
111: * @return the hash code value for this map entry
112: * @see #equals
113: */
114: public int hashCode() {
115: return (key == null ? 0 : key.hashCode())
116: ^ (value == null ? 0 : value.hashCode());
117: }
118:
119: /**
120: * Returns a String representation of this map entry. This
121: * implementation returns the string representation of this
122: * entry's key followed by the equals character ("<tt>=</tt>")
123: * followed by the string representation of this entry's value.
124: *
125: * @return a String representation of this map entry
126: */
127: public String toString() {
128: return key + "=" + value;//$NON-NLS-1$
129: }
130:
131: /**
132: * Utility method for SimpleEntry and SimpleImmutableEntry.
133: * Test for equality, checking for nulls.
134: */
135: private static boolean eq(Object o1, Object o2) {
136: return o1 == null ? o2 == null : o1.equals(o2);
137: }
138: }
|