KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > util > SimpleMap


1 /* SimpleMap Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * 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 import java.util.AbstractMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * This is a very simple map, using a set as backing.
28  * The default backing set is a simple set, but you can specify any other
29  * set of Map.Entry in the constructor.
30  */

31 public class SimpleMap extends AbstractMap JavaDoc {
32     private Set JavaDoc backing;
33
34     public SimpleMap() {
35     backing = new SimpleSet();
36     }
37     
38     public SimpleMap(int initialCapacity) {
39     backing = new SimpleSet(initialCapacity);
40     }
41
42     public SimpleMap(Set JavaDoc fromSet) {
43     backing = fromSet;
44     }
45     
46     public Set JavaDoc entrySet() {
47     return backing;
48     }
49
50     public static class SimpleEntry implements Map.Entry JavaDoc {
51     Object JavaDoc key;
52     Object JavaDoc value;
53
54     public SimpleEntry(Object JavaDoc key, Object JavaDoc value) {
55         this.key = key;
56         this.value = value;
57     }
58
59     public Object JavaDoc getKey() {
60         return key;
61     }
62
63     public Object JavaDoc getValue() {
64         return value;
65     }
66
67     public Object JavaDoc setValue(Object JavaDoc newValue) {
68         Object JavaDoc old = value;
69         value = newValue;
70         return old;
71     }
72     
73     public int hashCode() {
74         return key.hashCode() ^ value.hashCode();
75     }
76
77     public boolean equals(Object JavaDoc o) {
78         if (o instanceof Map.Entry JavaDoc) {
79         Map.Entry JavaDoc e = (Map.Entry JavaDoc) o;
80         return key.equals(e.getKey()) && value.equals(e.getValue());
81         }
82         return false;
83     }
84     }
85
86     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
87     for (Iterator JavaDoc i = backing.iterator();
88          i.hasNext(); ) {
89         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
90         if (key.equals(entry.getKey()))
91         return entry.setValue(value);
92     }
93     backing.add(new SimpleEntry(key, value));
94     return null;
95     }
96 }
97
98
99
Popular Tags