source: trunk/src/gcc/libjava/java/util/AbstractMap.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: 16.8 KB
Line 
1/* AbstractMap.java -- Abstract implementation of most of Map
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.util;
40
41/**
42 * An abstract implementation of Map to make it easier to create your own
43 * implementations. In order to create an unmodifiable Map, subclass
44 * AbstractMap and implement the <code>entrySet</code> (usually via an
45 * AbstractSet). To make it modifiable, also implement <code>put</code>,
46 * and have <code>entrySet().iterator()</code> support <code>remove</code>.
47 * <p>
48 *
49 * It is recommended that classes which extend this support at least the
50 * no-argument constructor, and a constructor which accepts another Map.
51 * Further methods in this class may be overridden if you have a more
52 * efficient implementation.
53 *
54 * @author Original author unknown
55 * @author Bryce McKinlay
56 * @author Eric Blake <[email protected]>
57 * @see Map
58 * @see Collection
59 * @see HashMap
60 * @see LinkedHashMap
61 * @see TreeMap
62 * @see WeakHashMap
63 * @see IdentityHashMap
64 * @since 1.2
65 * @status updated to 1.4
66 */
67public abstract class AbstractMap implements Map
68{
69 /** An "enum" of iterator types. */
70 // Package visible for use by subclasses.
71 static final int KEYS = 0,
72 VALUES = 1,
73 ENTRIES = 2;
74
75 /**
76 * The cache for {@link #keySet()}.
77 */
78 // Package visible for use by subclasses.
79 Set keys;
80
81 /**
82 * The cache for {@link #values()}.
83 */
84 // Package visible for use by subclasses.
85 Collection values;
86
87 /**
88 * The main constructor, for use by subclasses.
89 */
90 protected AbstractMap()
91 {
92 }
93
94 /**
95 * Remove all entries from this Map (optional operation). This default
96 * implementation calls entrySet().clear(). NOTE: If the entry set does
97 * not permit clearing, then this will fail, too. Subclasses often
98 * override this for efficiency. Your implementation of entrySet() should
99 * not call <code>AbstractMap.clear</code> unless you want an infinite loop.
100 *
101 * @throws UnsupportedOperationException if <code>entrySet().clear()</code>
102 * does not support clearing.
103 * @see Set#clear()
104 */
105 public void clear()
106 {
107 entrySet().clear();
108 }
109
110 /**
111 * Create a shallow copy of this Map, no keys or values are copied. The
112 * default implementation simply calls <code>super.clone()</code>.
113 *
114 * @return the shallow clone
115 * @throws CloneNotSupportedException if a subclass is not Cloneable
116 * @see Cloneable
117 * @see Object#clone()
118 */
119 protected Object clone() throws CloneNotSupportedException
120 {
121 AbstractMap copy = (AbstractMap) super.clone();
122 // Clear out the caches; they are stale.
123 copy.keys = null;
124 copy.values = null;
125 return copy;
126 }
127
128 /**
129 * Returns true if this contains a mapping for the given key. This
130 * implementation does a linear search, O(n), over the
131 * <code>entrySet()</code>, returning <code>true</code> if a match
132 * is found, <code>false</code> if the iteration ends. Many subclasses
133 * can implement this more efficiently.
134 *
135 * @param key the key to search for
136 * @return true if the map contains the key
137 * @throws NullPointerException if key is <code>null</code> but the map
138 * does not permit null keys
139 * @see #containsValue(Object)
140 */
141 public boolean containsKey(Object key)
142 {
143 Iterator entries = entrySet().iterator();
144 int pos = size();
145 while (--pos >= 0)
146 if (equals(key, ((Map.Entry) entries.next()).getKey()))
147 return true;
148 return false;
149 }
150
151 /**
152 * Returns true if this contains at least one mapping with the given value.
153 * This implementation does a linear search, O(n), over the
154 * <code>entrySet()</code>, returning <code>true</code> if a match
155 * is found, <code>false</code> if the iteration ends. A match is
156 * defined as <code>(value == null ? v == null : value.equals(v))</code>
157 * Subclasses are unlikely to implement this more efficiently.
158 *
159 * @param value the value to search for
160 * @return true if the map contains the value
161 * @see #containsKey(Object)
162 */
163 public boolean containsValue(Object value)
164 {
165 Iterator entries = entrySet().iterator();
166 int pos = size();
167 while (--pos >= 0)