001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002, 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.map;
020:
021: import bak.pcj.hash.DefaultLongHashFunction;
022: import bak.pcj.hash.DefaultDoubleHashFunction;
023: import bak.pcj.util.Exceptions;
024:
025: /**
026: * This class represents an abstract base for implementing
027: * maps from long values to double values. All operations that can be implemented
028: * using iterators
029: * are implemented as such. In most cases, this is
030: * hardly an efficient solution, and at least some of those
031: * methods should be overridden by sub-classes.
032: *
033: * @author Søren Bak
034: * @version 1.4 21-08-2003 19:34
035: * @since 1.0
036: */
037: public abstract class AbstractLongKeyDoubleMap implements
038: LongKeyDoubleMap {
039:
040: /** Default constructor to be invoked by sub-classes. */
041: protected AbstractLongKeyDoubleMap() {
042: }
043:
044: public void clear() {
045: LongKeyDoubleMapIterator i = entries();
046: while (i.hasNext()) {
047: i.next();
048: i.remove();
049: }
050: }
051:
052: public double remove(long key) {
053: LongKeyDoubleMapIterator i = entries();
054: while (i.hasNext()) {
055: i.next();
056: if (i.getKey() == key) {
057: double value = i.getValue();
058: i.remove();
059: return value;
060: }
061: }
062: return MapDefaults.defaultDouble();
063: }
064:
065: public void putAll(LongKeyDoubleMap map) {
066: LongKeyDoubleMapIterator i = map.entries();
067: while (i.hasNext()) {
068: i.next();
069: put(i.getKey(), i.getValue());
070: }
071: }
072:
073: public boolean containsKey(long key) {
074: LongKeyDoubleMapIterator i = entries();
075: while (i.hasNext()) {
076: i.next();
077: if (i.getKey() == key)
078: return true;
079: }
080: return false;
081: }
082:
083: public double get(long key) {
084: LongKeyDoubleMapIterator i = entries();
085: while (i.hasNext()) {
086: i.next();
087: if (i.getKey() == key)
088: return i.getValue();
089: }
090: return MapDefaults.defaultDouble();
091: }
092:
093: public boolean containsValue(double value) {
094: LongKeyDoubleMapIterator i = entries();
095: while (i.hasNext()) {
096: i.next();
097: if (i.getValue() == value)
098: return true;
099: }
100: return false;
101: }
102:
103: public boolean equals(Object obj) {
104: if (!(obj instanceof LongKeyDoubleMap))
105: return false;
106: LongKeyDoubleMap map = (LongKeyDoubleMap) obj;
107: if (size() != map.size())
108: return false;
109: LongKeyDoubleMapIterator i = entries();
110: while (i.hasNext()) {
111: i.next();
112: long k = i.getKey();
113: if (!map.containsKey(k) || map.lget() != i.getValue())
114: return false;
115: }
116: return true;
117: }
118:
119: public int hashCode() {
120: int h = 0;
121: LongKeyDoubleMapIterator i = entries();
122: while (i.hasNext()) {
123: i.next();
124: h += (DefaultLongHashFunction.INSTANCE.hash(i.getKey()) ^ DefaultDoubleHashFunction.INSTANCE
125: .hash(i.getValue()));
126: }
127: return h;
128: }
129:
130: public boolean isEmpty() {
131: return size() == 0;
132: }
133:
134: public int size() {
135: int size = 0;
136: LongKeyDoubleMapIterator i = entries();
137: while (i.hasNext()) {
138: i.next();
139: size++;
140: }
141: return size;
142: }
143:
144: public double tget(long key) {
145: double value = get(key);
146: if (value == MapDefaults.defaultDouble())
147: if (!containsKey(key))
148: Exceptions.noSuchMapping(String.valueOf(key));
149: return value;
150: }
151:
152: /**
153: * Returns a string representation of this map.
154: *
155: * @return a string representation of this map.
156: */
157: public String toString() {
158: StringBuffer s = new StringBuffer();
159: s.append('[');
160: LongKeyDoubleMapIterator i = entries();
161: while (i.hasNext()) {
162: if (s.length() > 1)
163: s.append(',');
164: i.next();
165: s.append(String.valueOf(i.getKey()));
166: s.append("->");
167: s.append(String.valueOf(i.getValue()));
168: }
169: s.append(']');
170: return s.toString();
171: }
172:
173: /**
174: * Does nothing. Sub-classes may provide an implementation to
175: * minimize memory usage, but this is not required since many
176: * implementations will always have minimal memory usage.
177: */
178: public void trimToSize() {
179: }
180:
181: }
|