KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002, 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.DefaultDoubleHashFunction;
22 import bak.pcj.util.Exceptions;
23
24 /**
25  * This class represents an abstract base for implementing
26  * maps from object values to double values. All operations that can be implemented
27  * using iterators
28  * are implemented as such. In most cases, this is
29  * hardly an efficient solution, and at least some of those
30  * methods should be overridden by sub-classes.
31  *
32  * @author Søren Bak
33  * @version 1.1 21-08-2003 19:33
34  * @since 1.1
35  */

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

176     public String JavaDoc toString() {
177         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
178         s.append('[');
179         ObjectKeyDoubleMapIterator i = entries();
180         while (i.hasNext()) {
181             if (s.length() > 1)
182                 s.append(',');
183             i.next();
184             s.append(String.valueOf(i.getKey()));
185             s.append("->");
186             s.append(String.valueOf(i.getValue()));
187         }
188         s.append(']');
189         return s.toString();
190     }
191
192     /**
193      * Does nothing. Sub-classes may provide an implementation to
194      * minimize memory usage, but this is not required since many
195      * implementations will always have minimal memory usage.
196      */

197     public void trimToSize()
198     { }
199
200 }
Popular Tags