KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > Entry


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11 import java.util.Map JavaDoc;
12
13 /**
14  * Represents a pair of values ('key' and a 'value'). It is a straight-forward implementation of
15  * {@link java.util.Map.Entry}, and can be used as a utility for Map implementations.
16  *
17  * @since MMBase-1.8
18  * @version $Id: Entry.java,v 1.5 2006/04/18 13:08:24 michiel Exp $
19  * @author Michiel Meeuwissen
20  */

21 public final class Entry implements Map.Entry JavaDoc, PublicCloneable, java.io.Serializable JavaDoc {
22
23     private Object JavaDoc key; // cannot be final because of cloneable/serializable, but logically, it could.
24
private Object JavaDoc value;
25
26     protected Entry() {
27         // serializable
28
}
29
30     /**
31      * @param k The key of this Map.Entry
32      * @param v The value of this Map.Entry
33      */

34     public Entry(Object JavaDoc k, Object JavaDoc v) {
35         key = k ;
36         value = v;
37     }
38     public Entry(Map.Entry JavaDoc e) {
39         key = e.getKey();
40         value = e.getValue();
41     }
42
43     // see Map.Entry
44
public Object JavaDoc getKey() {
45         return key;
46     }
47
48     // see Map.Entry
49
public Object JavaDoc getValue() {
50         return value;
51     }
52
53     // see Map.Entry
54
public Object JavaDoc setValue(Object JavaDoc v) {
55         Object JavaDoc r = value;
56         value = v;
57         return r;
58     }
59
60     public Object JavaDoc clone() {
61         return new Entry(key, value); // can do this, because this class is final
62
}
63
64     public int hashCode() {
65         return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
66     }
67     public boolean equals(Object JavaDoc o) {
68         if (o instanceof Map.Entry JavaDoc) {
69             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) o;
70             return
71                 (key == null ? entry.getKey() == null : key.equals(entry.getKey())) &&
72                 (value == null ? entry.getValue() == null : value.equals(entry.getValue()));
73         } else {
74             return false;
75         }
76     }
77     /**
78      * A sensible toString, for debugging purposes ('<key>=<value>').
79      */

80     public String JavaDoc toString() {
81         return key + "=" + value;
82     }
83 }
84
Popular Tags