source: trunk/src/gcc/libjava/java/util/BasicMapEntry.java@ 1213

Last change on this file since 1213 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.1 KB
Line 
1/* BasicMapEntry.java -- a class providing a plain-vanilla implementation of
2 the Map.Entry interface; could be used anywhere in java.util
3 Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
4
5This file is part of GNU Classpath.
6
7GNU Classpath is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Classpath is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Classpath; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
2002111-1307 USA.
21
22Linking this library statically or dynamically with other modules is
23making a combined work based on this library. Thus, the terms and
24conditions of the GNU General Public License cover the whole
25combination.
26
27As a special exception, the copyright holders of this library give you
28permission to link this library with independent modules to produce an
29executable, regardless of the license terms of these independent
30modules, and to copy and distribute the resulting executable under
31terms of your choice, provided that you also meet, for each linked
32independent module, the terms and conditions of the license of that
33module. An independent module is a module which is not derived from
34or based on this library. If you modify this library, you may extend
35this exception to your version of the library, but you are not
36obligated to do so. If you do not wish to do so, delete this
37exception statement from your version. */
38
39
40package java.util;
41
42/**
43 * A class which implements Map.Entry. It is shared by HashMap, TreeMap,
44 * Hashtable, and Collections. It is not specified by the JDK, but makes
45 * life much easier.
46 *
47 * @author Jon Zeppieri
48 * @author Eric Blake <[email protected]>
49 */
50class BasicMapEntry implements Map.Entry
51{
52 /**
53 * The key. Package visible for direct manipulation.
54 */
55 Object key;
56
57 /**
58 * The value. Package visible for direct manipulation.
59 */
60 Object value;
61
62 /**
63 * Basic constructor initializes the fields.
64 * @param newKey the key
65 * @param newValue the value
66 */
67 BasicMapEntry(Object newKey, Object newValue)
68 {
69 key = newKey;
70 value = newValue;
71 }
72
73 /**
74 * Compares the specified object with this entry. Returns true only if
75 * the object is a mapping of identical key and value. In other words,
76 * this must be:
77 * <pre>
78 * (o instanceof Map.Entry) &&
79 * (getKey() == null ? ((HashMap) o).getKey() == null
80 * : getKey().equals(((HashMap) o).getKey())) &&
81 * (getValue() == null ? ((HashMap) o).getValue() == null
82 * : getValue().equals(((HashMap) o).getValue()))
83 * </pre>
84 *
85 * @param o the object to compare
86 * @return true if it is equal
87 */
88 public final boolean equals(Object o)
89 {
90 if (! (o instanceof Map.Entry))
91 return false;
92 // Optimize for our own entries.
93 if (o instanceof BasicMapEntry)
94 {
95 BasicMapEntry e = (BasicMapEntry) o;
96 return (AbstractCollection.equals(key, e.key)
97 && AbstractCollection.equals(value, e.value));
98 }
99 Map.Entry e = (Map.Entry) o;
100 return (AbstractCollection.equals(key, e.getKey())
101 && AbstractCollection.equals(value, e.getValue()));
102 }
103
104 /**
105 * Get the key corresponding to this entry.
106 *
107 * @return the key
108 */
109 public final Object getKey()
110 {
111 return key;
112 }
113
114 /**
115 * Get the value corresponding to this entry. If you already called
116 * Iterator.remove(), the behavior undefined, but in this case it works.
117 *
118 * @return the value
119 */
120 public final Object getValue()
121 {
122 return value;
123 }
124
125 /**
126 * Returns the hash code of the entry. This is defined as the exclusive-or
127 * of the hashcodes of the key and value (using 0 for null). In other
128 * words, this must be:
129 * <pre>
130 * (getKey() == null ? 0 : getKey().hashCode()) ^
131 * (getValue() == null ? 0 : getValue().hashCode())
132 * </pre>
133 *
134 * @return the hash code
135 */
136 public final int hashCode()
137 {
138 return (AbstractCollection.hashCode(key)
139 ^ AbstractCollection.hashCode(value));
140 }
141
142 /**
143 * Replaces the value with the specified object. This writes through
144 * to the map, unless you have already called Iterator.remove(). It
145 * may be overridden to restrict a null value.
146 *
147 * @param newVal the new value to store
148 * @return the old value
149 * @throws NullPointerException if the map forbids null values
150 */
151 public Object setValue(Object newVal)
152 {
153 Object r = value;
154 value = newVal;
155 return r;
156 }
157
158 /**
159 * This provides a string representation of the entry. It is of the form
160 * "key=value", where string concatenation is used on key and value.
161 *
162 * @return the string representation
163 */
164 public final String toString()
165 {
166 return key + "=" + value;
167 }
168}
Note: See TracBrowser for help on using the repository browser.