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.DefaultShortHashFunction;
022: import bak.pcj.hash.DefaultShortHashFunction;
023: import bak.pcj.util.Exceptions;
024:
025: /**
026: * This class represents an abstract base for implementing
027: * maps from short values to short 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 AbstractShortKeyShortMap implements
038: ShortKeyShortMap {
039:
040: /** Default constructor to be invoked by sub-classes. */
041: protected AbstractShortKeyShortMap() {
042: }
043:
044: public void clear() {
045: ShortKeyShortMapIterator i = entries();
046: while (i.hasNext()) {
047: i.next();
048: i.remove();
049: }
050: }
051:
052: public short remove(short key) {
053: ShortKeyShortMapIterator i = entries();
054: while (i.hasNext()) {
055: i.next();
056: if (i.getKey() == key) {
057: short value = i.getValue();
058: i.remove();
059: return value;
060: }
061: }
062: return MapDefaults.defaultShort();
063: }
064:
065: public void putAll(ShortKeyShortMap map) {
066: ShortKeyShortMapIterator i = map.entries();
067: while (i.hasNext()) {
068: i.next();
069: put(i.getKey(), i.getValue());
070: }
071: }
072:
073: public boolean containsKey(short key) {
074: ShortKeyShortMapIterator 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 short get(short key) {
084: ShortKeyShortMapIterator i = entries();
085: while (i.hasNext()) {
086: i.next();
087: if (i.getKey() == key)
088: return i.getValue();
089: }
090: return MapDefaults.defaultShort();
091: }
092:
093: public boolean containsValue(short value) {
094: ShortKeyShortMapIterator 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 ShortKeyShortMap))
105: return false;
106: ShortKeyShortMap map = (ShortKeyShortMap) obj;
107: if (size() != map.size())
108: return false;
109: ShortKeyShortMapIterator i = entries();
110: while (i.hasNext()) {
111: i.next();
112: short 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: ShortKeyShortMapIterator i = entries();
122: while (i.hasNext()) {
123: i.next();
124: h += (DefaultShortHashFunction.INSTANCE.hash(i.getKey()) ^ DefaultShortHashFunction.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: ShortKeyShortMapIterator i = entries();
137: while (i.hasNext()) {
138: i.next();
139: size++;
140: }
141: return size;
142: }
143:
144: public short tget(short key) {
145: short value = get(key);
146: if (value == MapDefaults.defaultShort())
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: ShortKeyShortMapIterator 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: }
|