01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util;
11:
12: import java.util.Map;
13:
14: /**
15: * Represents a pair of values ('key' and a 'value'). It is a straight-forward implementation of
16: * {@link java.util.Map.Entry}, and can be used as a utility for Map implementations.
17: *
18: * @since MMBase-1.8
19: * @version $Id: Entry.java,v 1.7 2007/02/24 21:57:50 nklasens Exp $
20: * @author Michiel Meeuwissen
21: */
22: public final class Entry<K, V> implements Map.Entry<K, V>,
23: PublicCloneable, java.io.Serializable {
24:
25: private K key; // cannot be final because of cloneable/serializable, but logically, it could.
26: private V value;
27:
28: protected Entry() {
29: // serializable
30: }
31:
32: /**
33: * @param k The key of this Map.Entry
34: * @param v The value of this Map.Entry
35: */
36: public Entry(K k, V v) {
37: key = k;
38: value = v;
39: }
40:
41: public Entry(Map.Entry<K, V> e) {
42: key = e.getKey();
43: value = e.getValue();
44: }
45:
46: // see Map.Entry
47: public K getKey() {
48: return key;
49: }
50:
51: // see Map.Entry
52: public V getValue() {
53: return value;
54: }
55:
56: // see Map.Entry
57: public V setValue(V v) {
58: V r = value;
59: value = v;
60: return r;
61: }
62:
63: public Object clone() {
64: return new Entry<K, V>(key, value); // can do this, because this class is final
65: }
66:
67: public int hashCode() {
68: return (key == null ? 0 : key.hashCode())
69: ^ (value == null ? 0 : value.hashCode());
70: }
71:
72: public boolean equals(Object o) {
73: if (o instanceof Map.Entry) {
74: Map.Entry<K, V> entry = (Map.Entry<K, V>) o;
75: return (key == null ? entry.getKey() == null : key
76: .equals(entry.getKey()))
77: && (value == null ? entry.getValue() == null
78: : value.equals(entry.getValue()));
79: } else {
80: return false;
81: }
82: }
83:
84: /**
85: * A sensible toString, for debugging purposes ('<key>=<value>').
86: */
87: public String toString() {
88: return key + "=" + value;
89: }
90: }
|