01: /* SimpleMap Copyright (C) 1999-2002 Jochen Hoenicke.
02: *
03: * This program is free software; you can redistribute it and/or modify
04: * it under the terms of the GNU Lesser General Public License as published by
05: * the Free Software Foundation; either version 2, or (at your option)
06: * any later version.
07: *
08: * This program is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: * GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public License
14: * along with this program; see the file COPYING.LESSER. If not, write to
15: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16: *
17: * $Id: SimpleMap.java.in,v 1.1.2.1 2002/05/28 17:34:24 hoenicke Exp $
18: */
19:
20: package jode.util;
21:
22: import java.util.AbstractMap;
23: import java.util.Map;
24: import java.util.Iterator;
25: import java.util.Set;
26:
27: /**
28: * This is a very simple map, using a set as backing.
29: * The default backing set is a simple set, but you can specify any other
30: * set of Map.Entry in the constructor.
31: */
32: public class SimpleMap extends AbstractMap {
33: private Set backing;
34:
35: public SimpleMap() {
36: backing = new SimpleSet();
37: }
38:
39: public SimpleMap(int initialCapacity) {
40: backing = new SimpleSet(initialCapacity);
41: }
42:
43: public SimpleMap(Set fromSet) {
44: backing = fromSet;
45: }
46:
47: public Set entrySet() {
48: return backing;
49: }
50:
51: public static class SimpleEntry implements Map.Entry {
52: Object key;
53: Object value;
54:
55: public SimpleEntry(Object key, Object value) {
56: this .key = key;
57: this .value = value;
58: }
59:
60: public Object getKey() {
61: return key;
62: }
63:
64: public Object getValue() {
65: return value;
66: }
67:
68: public Object setValue(Object newValue) {
69: Object old = value;
70: value = newValue;
71: return old;
72: }
73:
74: public int hashCode() {
75: return key.hashCode() ^ value.hashCode();
76: }
77:
78: public boolean equals(Object o) {
79: if (o instanceof Map.Entry) {
80: Map.Entry e = (Map.Entry) o;
81: return key.equals(e.getKey())
82: && value.equals(e.getValue());
83: }
84: return false;
85: }
86: }
87:
88: public Object put(Object key, Object value) {
89: for (Iterator i = backing.iterator(); i.hasNext();) {
90: Map.Entry entry = (Map.Entry) i.next();
91: if (key.equals(entry.getKey()))
92: return entry.setValue(value);
93: }
94: backing.add(new SimpleEntry(key, value));
95: return null;
96: }
97: }
|