KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > OrderedHashMap


1 /*
2  * Project: Gulden Utilies
3  * Class: de.gulden.util.OrderedHashMap
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the Gulden Utilities,
9  * it is not released as a seperate version.
10  *
11  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
12  *
13  * This is licensed under the GNU Lesser General Public License (LGPL)
14  * and comes with NO WARRANTY.
15  *
16  * Author: Jens Gulden
17  * Email: amoda@jensgulden.de
18  */

19
20 package de.gulden.util;
21
22 import de.gulden.util.OrderedHashMap.OrderedHashMapEntry;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.*;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Map.Entry;
31 import java.util.Set JavaDoc;
32
33 /**
34  * Class OrderedHashMap.
35  *
36  * @author Jens Gulden
37  * @version snapshot-beautyj-1.1
38  */

39 public class OrderedHashMap extends HashMap JavaDoc {
40
41     // ------------------------------------------------------------------------
42
// --- fields ---
43
// ------------------------------------------------------------------------
44

45     /**
46      * The key list.
47      */

48     protected ArrayListSet keyList;
49
50     /**
51      * The value list.
52      */

53     protected ArrayListSet valueList;
54
55
56     // ------------------------------------------------------------------------
57
// --- constructor ---
58
// ------------------------------------------------------------------------
59

60     /**
61      * Creates a new instance of OrderedHashMap.
62      */

63     public OrderedHashMap() {
64         super();
65         keyList=new ArrayListSet();
66         valueList=new ArrayListSet();
67     }
68
69
70     // ------------------------------------------------------------------------
71
// --- methods ---
72
// ------------------------------------------------------------------------
73

74     public Object JavaDoc clone() {
75         OrderedHashMap cloned=new OrderedHashMap();
76         cloned.putAll(this);
77         return cloned;
78     }
79
80     public void clear() {
81         super.clear();
82         keyList.clear();
83         valueList.clear();
84     }
85
86     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
87         int i=keyList.indexOf(key);
88         if (i!=-1) {
89             keyList.remove(i); // remove if already inside and add new at the end
90
valueList.remove(i);
91         }
92         keyList.add(key);
93         valueList.add(value);
94         return super.put(key,value);
95     }
96
97     public void putAll(Map JavaDoc map) {
98         super.putAll(map);
99         // list must be handled -after- super.putAll()
100
Collection JavaDoc mapKeys=map.keySet();
101         Collection JavaDoc mapValues=map.values();
102         keyList.removeAll(mapKeys);
103         keyList.addAll(mapKeys);
104         valueList.removeAll(mapValues);
105         valueList.addAll(mapValues);
106     }
107
108     public Object JavaDoc remove(Object JavaDoc key) {
109         int i=keyList.indexOf(key);
110         if (i!=-1) {
111             keyList.remove(i);
112             valueList.remove(i);
113         }
114         return super.remove(key);
115     }
116
117     public Collection JavaDoc values() {
118         return valueList;
119     }
120
121     public Set JavaDoc keySet() {
122         return keyList;
123     }
124
125     public Set JavaDoc entrySet() {
126         ArrayListSet set=new ArrayListSet();
127         for (int i=0;i<keyList.size();i++) {
128             Object JavaDoc key=keyList.get(i);
129             Object JavaDoc value=valueList.get(i);
130             set.add(new OrderedHashMapEntry(key,value));
131         }
132         return set;
133     }
134
135     /**
136      *
137      * @throws IOException if an i/o error occurs
138      */

139     public void load(InputStream JavaDoc in) throws IOException JavaDoc {
140         java.io.BufferedReader JavaDoc r=new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(in));
141         String JavaDoc line=r.readLine();
142         while (line!=null) {
143             Properties p=new Properties(); // use Properties' parser to handle \=
144
p.load(new java.io.StringBufferInputStream JavaDoc(line));
145             for (Enumeration e=p.keys();e.hasMoreElements();) { // should be 0..1
146
String JavaDoc key=(String JavaDoc)e.nextElement();
147                 String JavaDoc value=p.getProperty(key);
148                 this.put(key,value);
149             }
150             line=r.readLine();
151         }
152     }
153
154     /**
155      *
156      * @throws IOException if an i/o error occurs
157      */

158     public void save(OutputStream JavaDoc out) throws IOException JavaDoc {
159         for (int i=0;i<keyList.size();i++) {
160             Properties p=new Properties();
161             p.put(keyList.get(i),valueList.get(i));
162             p.store(out,"");
163         }
164     }
165
166
167     // ************************************************************************
168
// *** inner class ***
169
// ************************************************************************
170

171     /**
172      * Class OrderedHashMapEntry.
173      *
174      * @author Jens Gulden
175      * @version snapshot-beautyj-1.1
176      */

177     public class OrderedHashMapEntry implements Map.Entry JavaDoc {
178
179         // ------------------------------------------------------------------------
180
// --- fields ---
181
// ------------------------------------------------------------------------
182

183         /**
184          * The key.
185          */

186         protected Object JavaDoc key;
187
188         /**
189          * The value.
190          */

191         protected Object JavaDoc value;
192
193
194         // ------------------------------------------------------------------------
195
// --- constructors ---
196
// ------------------------------------------------------------------------
197

198         /**
199          * Creates a new instance of OrderedHashMapEntry.
200          */

201         public OrderedHashMapEntry() {
202             //nop
203
}
204
205         /**
206          * Creates a new instance of OrderedHashMapEntry.
207          */

208         public OrderedHashMapEntry(Object JavaDoc key, Object JavaDoc value) {
209             this();
210             setKey(key);
211             setValue(value);
212         }
213
214
215         // ------------------------------------------------------------------------
216
// --- methods ---
217
// ------------------------------------------------------------------------
218

219         public boolean equals(Object JavaDoc o) {
220             // your code here
221
return false;
222         }
223
224         public int hashCode() {
225             // your code here
226
return 0;
227         }
228
229         /**
230          * Returns the key.
231          */

232         public Object JavaDoc getKey() {
233             return key;
234         }
235
236         /**
237          * Sets the key.
238          */

239         public void setKey(Object JavaDoc _key) {
240             key = _key;
241         }
242
243         /**
244          * Returns the value.
245          */

246         public Object JavaDoc getValue() {
247             return value;
248         }
249
250         /**
251          * Sets the value.
252          */

253         public Object JavaDoc setValue(Object JavaDoc _value) {
254             Object JavaDoc old=value;
255             value = _value;
256             return old;
257         }
258
259     } // end OrderedHashMapEntry
260

261 } // end OrderedHashMap
262
Popular Tags