KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

174     public void trimToSize()
175     { }
176
177 }
Popular Tags