KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > MapEntryParameter


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: MapEntryParameter.java,v 1.19 2006/10/30 21:14:10 bostic Exp $
7  */

8
9 package com.sleepycat.collections;
10
11 import java.util.Map JavaDoc;
12
13 /**
14  * A simple <code>Map.Entry</code> implementation that can be used as in
15  * input parameter. Since a <code>MapEntryParameter</code> is not obtained
16  * from a map, it is not attached to any map in particular. To emphasize that
17  * changing this object does not change the map, the {@link #setValue} method
18  * always throws <code>UnsupportedOperationException</code>.
19  *
20  * <p><b>Warning:</b> Use of this interface violates the Java Collections
21  * interface contract since these state that <code>Map.Entry</code> objects
22  * should only be obtained from <code>Map.entrySet()</code> sets, while this
23  * class allows constructing them directly. However, it is useful for
24  * performing operations on an entry set such as add(), contains(), etc. For
25  * restrictions see {@link #getValue} and {@link #setValue}.</p>
26  *
27  * @author Mark Hayes
28  */

29 public class MapEntryParameter implements Map.Entry JavaDoc {
30
31     private Object JavaDoc key;
32     private Object JavaDoc value;
33
34     /**
35      * Creates a map entry with a given key and value.
36      *
37      * @param key is the key to use.
38      *
39      * @param value is the value to use.
40      */

41     public MapEntryParameter(Object JavaDoc key, Object JavaDoc value) {
42
43         this.key = key;
44         this.value = value;
45     }
46
47     /**
48      * Computes a hash code as specified by {@link
49      * java.util.Map.Entry#hashCode}.
50      *
51      * @return the computed hash code.
52      */

53     public int hashCode() {
54
55         return ((key == null) ? 0 : key.hashCode()) ^
56                ((value == null) ? 0 : value.hashCode());
57     }
58
59     /**
60      * Compares this entry to a given entry as specified by {@link
61      * java.util.Map.Entry#equals}.
62      *
63      * @return the computed hash code.
64      */

65     public boolean equals(Object JavaDoc other) {
66
67         if (!(other instanceof Map.Entry JavaDoc)) {
68             return false;
69         }
70
71         Map.Entry JavaDoc e = (Map.Entry JavaDoc) other;
72
73         return ((key == null) ? (e.getKey() == null)
74                               : key.equals(e.getKey())) &&
75                ((value == null) ? (e.getValue() == null)
76                                 : value.equals(e.getValue()));
77     }
78
79     /**
80      * Returns the key of this entry.
81      *
82      * @return the key of this entry.
83      */

84     public final Object JavaDoc getKey() {
85
86         return key;
87     }
88
89     /**
90      * Returns the value of this entry. Note that this will be the value
91      * passed to the constructor or the last value passed to {@link #setValue}.
92      * It will not reflect changes made to a Map.
93      *
94      * @return the value of this entry.
95      */

96     public final Object JavaDoc getValue() {
97
98         return value;
99     }
100
101     /**
102      * Always throws <code>UnsupportedOperationException</code> since this
103      * object is not attached to a map.
104      */

105     public Object JavaDoc setValue(Object JavaDoc newValue) {
106
107         throw new UnsupportedOperationException JavaDoc();
108     }
109
110     final void setValueInternal(Object JavaDoc newValue) {
111
112         this.value = newValue;
113     }
114
115     /**
116      * Converts the entry to a string representation for debugging.
117      *
118      * @return the string representation.
119      */

120     public String JavaDoc toString() {
121
122         return "[key [" + key + "] value [" + value + ']';
123     }
124 }
125
126
Popular Tags