KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > map > AbstractLongKeyMap


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.map;
20
21 import bak.pcj.hash.DefaultLongHashFunction;
22
23 /**
24  * This class represents an abstract base for implementing
25  * maps from long values to objects. All operations that can be implemented
26  * using iterators
27  * are implemented as such. In most cases, this is
28  * hardly an efficient solution, and at least some of those
29  * methods should be overridden by sub-classes.
30  *
31  * @author Søren Bak
32  * @version 1.0 2003/10/1
33  * @since 1.0
34  */

35 public abstract class AbstractLongKeyMap implements LongKeyMap {
36
37     /** Default constructor to be invoked by sub-classes. */
38     protected AbstractLongKeyMap() { }
39
40     public void clear() {
41         LongKeyMapIterator i = entries();
42         while (i.hasNext()) {
43             i.next();
44             i.remove();
45         }
46     }
47
48     public Object JavaDoc remove(long key) {
49         LongKeyMapIterator i = entries();
50         while (i.hasNext()) {
51             i.next();
52             if (i.getKey() == key) {
53                 Object JavaDoc value = i.getValue();
54                 i.remove();
55                 return value;
56             }
57         }
58         return null;
59     }
60
61     public void putAll(LongKeyMap map) {
62         LongKeyMapIterator i = map.entries();
63         while (i.hasNext()) {
64             i.next();
65             put(i.getKey(), i.getValue());
66         }
67     }
68
69     public boolean containsKey(long key) {
70         LongKeyMapIterator i = entries();
71         while (i.hasNext()) {
72             i.next();
73             if (i.getKey() == key)
74                 return true;
75         }
76         return false;
77     }
78
79     public Object JavaDoc get(long key) {
80         LongKeyMapIterator i = entries();
81         while (i.hasNext()) {
82             i.next();
83             if (i.getKey() == key)
84                 return i.getValue();
85         }
86         return null;
87     }
88
89     public boolean containsValue(Object JavaDoc value) {
90         LongKeyMapIterator i = entries();
91         if (value == null) {
92             while (i.hasNext()) {
93                 i.next();
94                 if (value == null)
95                     return true;
96             }
97         } else {
98             while (i.hasNext()) {
99                 i.next();
100                 if (value.equals(i.getValue()))
101                     return true;
102             }
103         }
104         return false;
105     }
106
107     public boolean equals(Object JavaDoc obj) {
108         if (!(obj instanceof LongKeyMap))
109             return false;
110         LongKeyMap map = (LongKeyMap)obj;
111         if (size() != map.size())
112             return false;
113         LongKeyMapIterator i = entries();
114         while (i.hasNext()) {
115             i.next();
116             long k = i.getKey();
117             Object JavaDoc v = i.getValue();
118             if (v == null) {
119                 if (map.get(k) != null)
120                     return false;
121                 if (!map.containsKey(k))
122                     return false;
123             } else {
124                 if (!v.equals(map.get(k)))
125                     return false;
126             }
127         }
128         return true;
129     }
130
131     public int hashCode() {
132         int h = 0;
133         LongKeyMapIterator i = entries();
134         while (i.hasNext()) {
135             i.next();
136             h += (DefaultLongHashFunction.INSTANCE.hash(i.getKey()) ^ i.getValue().hashCode());
137         }
138         return h;
139     }
140
141     public boolean isEmpty()
142     { return size() == 0; }
143
144     public int size() {
145         int size = 0;
146         LongKeyMapIterator i = entries();
147         while (i.hasNext()) {
148             i.next();
149             size++;
150         }
151         return size;
152     }
153
154     /**
155      * Returns a string representation of this map.
156      *
157      * @return a string representation of this map.
158      */

159     public String JavaDoc toString() {
160         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
161         s.append('[');
162         LongKeyMapIterator i = entries();
163         while (i.hasNext()) {
164             if (s.length() > 1)
165                 s.append(',');
166             i.next();
167             s.append(String.valueOf(i.getKey()));
168             s.append("->");
169             s.append(String.valueOf(i.getValue()));
170         }
171         s.append(']');
172         return s.toString();
173     }
174
175     /**
176      * Does nothing. Sub-classes may provide an implementation to
177      * minimize memory usage, but this is not required since many
178      * implementations will always have minimal memory usage.
179      */

180     public void trimToSize()
181     { }
182
183 }
Popular Tags