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 com.uwyn.rife.pcj.map;
020:
021: import com.uwyn.rife.pcj.hash.DefaultIntHashFunction;
022: import com.uwyn.rife.pcj.util.Exceptions;
023:
024: /**
025: * This class represents an abstract base for implementing
026: * maps from int values to int values. All operations that can be implemented
027: * using iterators
028: * are implemented as such. In most cases, this is
029: * hardly an efficient solution, and at least some of those
030: * methods should be overridden by sub-classes.
031: *
032: * @author Søren Bak
033: * @version 1.4 21-08-2003 19:34
034: * @since 1.0
035: */
036: public abstract class AbstractIntKeyIntMap implements IntKeyIntMap {
037:
038: /** Default constructor to be invoked by sub-classes. */
039: protected AbstractIntKeyIntMap() {
040: }
041:
042: public void clear() {
043: IntKeyIntMapIterator i = entries();
044: while (i.hasNext()) {
045: i.next();
046: i.remove();
047: }
048: }
049:
050: public int remove(int key) {
051: IntKeyIntMapIterator i = entries();
052: while (i.hasNext()) {
053: i.next();
054: if (i.getKey() == key) {
055: int value = i.getValue();
056: i.remove();
057: return value;
058: }
059: }
060: return MapDefaults.defaultInt();
061: }
062:
063: public void putAll(IntKeyIntMap map) {
064: IntKeyIntMapIterator i = map.entries();
065: while (i.hasNext()) {
066: i.next();
067: put(i.getKey(), i.getValue());
068: }
069: }
070:
071: public boolean containsKey(int key) {
072: IntKeyIntMapIterator i = entries();
073: while (i.hasNext()) {
074: i.next();
075: if (i.getKey() == key)
076: return true;
077: }
078: return false;
079: }
080:
081: public int get(int key) {
082: IntKeyIntMapIterator i = entries();
083: while (i.hasNext()) {
084: i.next();
085: if (i.getKey() == key)
086: return i.getValue();
087: }
088: return MapDefaults.defaultInt();
089: }
090:
091: public boolean containsValue(int value) {
092: IntKeyIntMapIterator i = entries();
093: while (i.hasNext()) {
094: i.next();
095: if (i.getValue() == value)
096: return true;
097: }
098: return false;
099: }
100:
101: public boolean equals(Object obj) {
102: if (!(obj instanceof IntKeyIntMap))
103: return false;
104: IntKeyIntMap map = (IntKeyIntMap) obj;
105: if (size() != map.size())
106: return false;
107: IntKeyIntMapIterator i = entries();
108: while (i.hasNext()) {
109: i.next();
110: int 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: IntKeyIntMapIterator i = entries();
120: while (i.hasNext()) {
121: i.next();
122: h += (DefaultIntHashFunction.INSTANCE.hash(i.getKey()) ^ DefaultIntHashFunction.INSTANCE
123: .hash(i.getValue()));
124: }
125: return h;
126: }
127:
128: public boolean isEmpty() {
129: return size() == 0;
130: }
131:
132: public int size() {
133: int size = 0;
134: IntKeyIntMapIterator i = entries();
135: while (i.hasNext()) {
136: i.next();
137: size++;
138: }
139: return size;
140: }
141:
142: public int tget(int key) {
143: int value = get(key);
144: if (value == MapDefaults.defaultInt())
145: if (!containsKey(key))
146: Exceptions.noSuchMapping(String.valueOf(key));
147: return value;
148: }
149:
150: /**
151: * Returns a string representation of this map.
152: *
153: * @return a string representation of this map.
154: */
155: public String toString() {
156: StringBuffer s = new StringBuffer();
157: s.append('[');
158: IntKeyIntMapIterator i = entries();
159: while (i.hasNext()) {
160: if (s.length() > 1)
161: s.append(',');
162: i.next();
163: s.append(String.valueOf(i.getKey()));
164: s.append("->");
165: s.append(String.valueOf(i.getValue()));
166: }
167: s.append(']');
168: return s.toString();
169: }
170:
171: /**
172: * Does nothing. Sub-classes may provide an implementation to
173: * minimize memory usage, but this is not required since many
174: * implementations will always have minimal memory usage.
175: */
176: public void trimToSize() {
177: }
178:
179: }
|