/[classpath]/classpath/java/util/BasicMapEntry.java
ViewVC logotype

Diff of /classpath/java/util/BasicMapEntry.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by mark, Sun Feb 18 15:39:58 2001 UTC revision 1.8 by ericb, Fri Oct 19 00:17:44 2001 UTC
# Line 1  Line 1 
1  /* BasicMapEntry.java -- a class providing a plain-vanilla implementation of  /* BasicMapEntry.java -- a class providing a plain-vanilla implementation of
2     the Map.Entry interface; could be used anywhere in java.util     the Map.Entry interface; could be used anywhere in java.util
3     Copyright (C) 1998, 2000 Free Software Foundation, Inc.     Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
4    
5  This file is part of GNU Classpath.  This file is part of GNU Classpath.
6    
# Line 29  executable file might be covered by the Line 29  executable file might be covered by the
29  package java.util;  package java.util;
30    
31  /**  /**
32   * A class which implements Map.Entry. It is shared by HashMap, TreeMap, and   * A class which implements Map.Entry. It is shared by HashMap, TreeMap,
33   * Hashtable.   * Hashtable, and Collections. It is not specified by the JDK, but makes
34     * life much easier.
35   *   *
36   * @author      Jon Zeppieri   * @author Jon Zeppieri
37     * @author Eric Blake <ebb9@email.byu.edu>
38   */   */
39  class BasicMapEntry implements Map.Entry  class BasicMapEntry implements Map.Entry
40  {  {
41      /**
42       * The key. Package visible for direct manipulation.
43       */
44    Object key;    Object key;
45    
46      /**
47       * The value. Package visible for direct manipulation.
48       */
49    Object value;    Object value;
50    
51      /**
52       * Basic constructor initializes the fields.
53       * @param newKey the key
54       * @param newValue the value
55       */
56    BasicMapEntry(Object newKey, Object newValue)    BasicMapEntry(Object newKey, Object newValue)
57    {    {
58      key = newKey;      key = newKey;
59      value = newValue;      value = newValue;
60    }    }
61    
62      /**
63       * Compares the specified object with this entry. Returns true only if
64       * the object is a mapping of identical key and value. In other words,
65       * this must be:
66       * <pre>
67       * (o instanceof Map.Entry) &&
68       * (getKey() == null ? ((HashMap) o).getKey() == null
69       *                   : getKey().equals(((HashMap) o).getKey())) &&
70       * (getValue() == null ? ((HashMap) o).getValue() == null
71       *                   : getValue().equals(((HashMap) o).getValue()))
72       * </pre>
73       *
74       * @param o the object to compare
75       * @return true if it is equal
76       */
77    public final boolean equals(Object o)    public final boolean equals(Object o)
78    {    {
79      if (!(o instanceof Map.Entry))      if (! (o instanceof Map.Entry))
80        return false;        return false;
81        // Optimize for our own entries.
82        if (o instanceof BasicMapEntry)
83          {
84            BasicMapEntry e = (BasicMapEntry) o;
85            return (AbstractCollection.equals(key, e.key)
86                    && AbstractCollection.equals(value, e.value));
87          }
88      Map.Entry e = (Map.Entry) o;      Map.Entry e = (Map.Entry) o;
89      return (key == null ? e.getKey() == null : key.equals(e.getKey())      return (AbstractCollection.equals(key, e.getKey())
90              && value == null ? e.getValue() == null              && AbstractCollection.equals(value, e.getValue()));
                              : value.equals(e.getValue()));  
91    }    }
92    
93      /**
94       * Get the key corresponding to this entry.
95       *
96       * @return the key
97       */
98    public final Object getKey()    public final Object getKey()
99    {    {
100      return key;      return key;
101    }    }
102    
103      /**
104       * Get the value corresponding to this entry. If you already called
105       * Iterator.remove(), the behavior undefined, but in this case it works.
106       *
107       * @return the value
108       */
109    public final Object getValue()    public final Object getValue()
110    {    {
111      return value;      return value;
112    }    }
113    
114      /**
115       * Returns the hash code of the entry.  This is defined as the exclusive-or
116       * of the hashcodes of the key and value (using 0 for null). In other
117       * words, this must be:
118       * <pre>
119       *  (getKey() == null ? 0 : getKey().hashCode()) ^
120       *  (getValue() == null ? 0 : getValue().hashCode())
121       * </pre>
122       *
123       * @return the hash code
124       */
125    public final int hashCode()    public final int hashCode()
126    {    {
127      int kc = (key == null ? 0 : key.hashCode());      return (AbstractCollection.hashCode(key)
128      int vc = (value == null ? 0 : value.hashCode());              ^ AbstractCollection.hashCode(value));
     return kc ^ vc;  
129    }    }
130    
131    /**    /**
132     * sets the value of this Map.Entry. Note that this is overriden by     * Replaces the value with the specified object. This writes through
133     * Hashtable.Entry, which does not permit a null value.     * to the map, unless you have already called Iterator.remove(). It
134       * may be overridden to restrict a null value.
135       *
136       * @param newVal the new value to store
137       * @return the old value
138       * @throws NullPointerException if the map forbids null values
139     */     */
140    public Object setValue(Object newVal)    public Object setValue(Object newVal)
141    {    {
# Line 83  class BasicMapEntry implements Map.Entry Line 144  class BasicMapEntry implements Map.Entry
144      return r;      return r;
145    }    }
146    
147      /**
148       * This provides a string representation of the entry. It is of the form
149       * "key=value", where string concatenation is used on key and value.
150       *
151       * @return the string representation
152       */
153    public final String toString()    public final String toString()
154    {    {
155      return key + "=" + value;      return key + "=" + value;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26