KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > collections > ListedHashTree


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/collections/ListedHashTree.java,v 1.8 2004/03/24 15:23:37 mstover1 Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.collections;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * ListedHashTree is a different implementation of the {@link HashTree}
33  * collection class. In the ListedHashTree, the order in which values are added
34  * is preserved (not to be confused with {@link SortedHashTree}, which sorts
35  * the order of the values using the compare() function). Any listing of nodes
36  * or iteration through the list of nodes of a ListedHashTree will be given in
37  * the order in which the nodes were added to the tree.
38  *
39  * @see HashTree
40  * @author mstover1 at apache.org
41  * @version $Revision: 1.8 $
42  */

43 public class ListedHashTree extends HashTree implements Serializable JavaDoc, Cloneable JavaDoc
44 {
45     private List JavaDoc order;
46
47     public ListedHashTree()
48     {
49         data = new HashMap JavaDoc();
50         order = new LinkedList JavaDoc();
51     }
52
53     public Object JavaDoc clone()
54     {
55         ListedHashTree newTree = new ListedHashTree();
56         cloneTree(newTree);
57         return newTree;
58     }
59
60     public ListedHashTree(Object JavaDoc key)
61     {
62         data = new HashMap JavaDoc();
63         order = new LinkedList JavaDoc();
64         data.put(key, new ListedHashTree());
65         order.add(key);
66     }
67
68     public ListedHashTree(Collection JavaDoc keys)
69     {
70         data = new HashMap JavaDoc();
71         order = new LinkedList JavaDoc();
72         Iterator JavaDoc it = keys.iterator();
73         while (it.hasNext())
74         {
75             Object JavaDoc temp = it.next();
76             data.put(temp, new ListedHashTree());
77             order.add(temp);
78         }
79     }
80
81     public ListedHashTree(Object JavaDoc[] keys)
82     {
83         data = new HashMap JavaDoc();
84         order = new LinkedList JavaDoc();
85         for (int x = 0; x < keys.length; x++)
86         {
87             data.put(keys[x], new ListedHashTree());
88             order.add(keys[x]);
89         }
90     }
91
92     public void set(Object JavaDoc key, Object JavaDoc value)
93     {
94         if (!data.containsKey(key))
95         {
96             order.add(key);
97         }
98         super.set(key, value);
99     }
100
101     public void set(Object JavaDoc key, HashTree t)
102     {
103         if (!data.containsKey(key))
104         {
105             order.add(key);
106         }
107         super.set(key, t);
108     }
109
110     public void set(Object JavaDoc key, Object JavaDoc[] values)
111     {
112         if (!data.containsKey(key))
113         {
114             order.add(key);
115         }
116         super.set(key, values);
117     }
118
119     public void set(Object JavaDoc key, Collection JavaDoc values)
120     {
121         if (!data.containsKey(key))
122         {
123             order.add(key);
124         }
125         super.set(key, values);
126     }
127
128     public void replace(Object JavaDoc currentKey, Object JavaDoc newKey)
129     {
130         HashTree tree = getTree(currentKey);
131         data.remove(currentKey);
132         data.put(newKey, tree);
133         order.set(order.indexOf(currentKey), newKey);
134     }
135
136     public HashTree createNewTree()
137     {
138         return new ListedHashTree();
139     }
140
141     public HashTree createNewTree(Object JavaDoc key)
142     {
143         return new ListedHashTree(key);
144     }
145
146     public HashTree createNewTree(Collection JavaDoc values)
147     {
148         return new ListedHashTree(values);
149     }
150
151     public HashTree add(Object JavaDoc key)
152     {
153         if (!data.containsKey(key))
154         {
155             HashTree newTree = createNewTree();
156             data.put(key, newTree);
157             order.add(key);
158             return newTree;
159         }
160         else
161         {
162             return getTree(key);
163         }
164     }
165
166     public Collection JavaDoc list()
167     {
168         return order;
169     }
170
171     public Object JavaDoc remove(Object JavaDoc key)
172     {
173         order.remove(key);
174         return data.remove(key);
175     }
176
177     public Object JavaDoc[] getArray()
178     {
179         return order.toArray();
180     }
181
182     // Make sure the hashCode depends on the order as well
183
public int hashCode()
184     {
185         int hc = 17;
186         hc = hc * 37 + (order == null ? 0 : order.hashCode());
187         hc = hc * 37 + super.hashCode();
188         return hc;
189     }
190
191     public boolean equals(Object JavaDoc o)
192     {
193         if (!(o instanceof ListedHashTree)) return false;
194         ListedHashTree lht = (ListedHashTree) o;
195         return (super.equals(lht) && order.equals(lht.order));
196         
197 // boolean flag = true;
198
// if (o instanceof ListedHashTree)
199
// {
200
// ListedHashTree oo = (ListedHashTree) o;
201
// Iterator it = order.iterator();
202
// Iterator it2 = oo.order.iterator();
203
// if (size() != oo.size())
204
// {
205
// flag = false;
206
// }
207
// while (it.hasNext() && it2.hasNext() && flag)
208
// {
209
// if (!it.next().equals(it2.next()))
210
// {
211
// flag = false;
212
// }
213
// }
214
// if (flag)
215
// {
216
// it = order.iterator();
217
// while (it.hasNext() && flag)
218
// {
219
// Object temp = it.next();
220
// flag = get(temp).equals(oo.get(temp));
221
// }
222
// }
223
// }
224
// else
225
// {
226
// flag = false;
227
// }
228
// return flag;
229
}
230
231     public Set JavaDoc keySet()
232     {
233         return data.keySet();
234     }
235
236     public int size()
237     {
238         return data.size();
239     }
240
241     void readObject(ObjectInputStream JavaDoc ois)
242         throws ClassNotFoundException JavaDoc, IOException JavaDoc
243     {
244         ois.defaultReadObject();
245     }
246
247     void writeObject(ObjectOutputStream JavaDoc oos) throws IOException JavaDoc
248     {
249         oos.defaultWriteObject();
250     }
251
252     public static class Test extends junit.framework.TestCase
253     {
254         public Test(String JavaDoc name)
255         {
256             super(name);
257         }
258
259         public void testAddObjectAndTree() throws Exception JavaDoc
260         {
261             ListedHashTree tree = new ListedHashTree("key");
262             ListedHashTree newTree = new ListedHashTree("value");
263             tree.add("key", newTree);
264             assertEquals(tree.list().size(), 1);
265             assertEquals("key", tree.getArray()[0]);
266             assertEquals(1, tree.getTree("key").list().size());
267             assertEquals(0, tree.getTree("key").getTree("value").size());
268             assertEquals(tree.getTree("key").getArray()[0], "value");
269             assertNotNull(tree.getTree("key").get("value"));
270         }
271         
272         public void testEqualsAndHashCode() throws Exception JavaDoc
273         {
274             ListedHashTree tree1 = new ListedHashTree("abcd");
275             ListedHashTree tree2 = new ListedHashTree("abcd");
276             ListedHashTree tree3 = new ListedHashTree("abcde");
277             ListedHashTree tree4 = new ListedHashTree("abcde");
278             
279             assertTrue(tree1.equals(tree1));
280             assertTrue(tree1.equals(tree2));
281             assertTrue(tree2.equals(tree1));
282             assertTrue(tree2.equals(tree2));
283             assertTrue(tree1.hashCode()==tree2.hashCode());
284
285             assertTrue(tree3.equals(tree3));
286             assertTrue(tree3.equals(tree4));
287             assertTrue(tree4.equals(tree3));
288             assertTrue(tree4.equals(tree4));
289             assertTrue(tree3.hashCode()==tree4.hashCode());
290
291             assertNotSame(tree1,tree2);
292             assertNotSame(tree1,tree3);
293             assertFalse(tree1.equals(tree3));
294             assertFalse(tree3.equals(tree1));
295             assertFalse(tree1.equals(tree4));
296             assertFalse(tree4.equals(tree1));
297
298             assertFalse(tree2.equals(tree3));
299             assertFalse(tree3.equals(tree2));
300             assertFalse(tree2.equals(tree4));
301             assertFalse(tree4.equals(tree2));
302             
303             tree1.add("abcd",tree3);
304             assertFalse(tree1.equals(tree2));
305             assertFalse(tree2.equals(tree1));
306             
307             tree2.add("abcd",tree4);
308             assertTrue(tree1.equals(tree2));
309             assertTrue(tree2.equals(tree1));
310             assertTrue(tree1.hashCode()==tree2.hashCode());
311             
312             tree1.add("a1");
313             tree1.add("a2");
314             //tree1.add("a3");
315
tree2.add("a2");
316             tree2.add("a1");
317
318             assertFalse(tree1.equals(tree2));
319             assertFalse(tree2.equals(tree1));
320             if (tree1.hashCode()==tree2.hashCode())
321             {
322                 //This is not a requirement
323
System.out.println("WARN: unequal ListedHashTrees should not have equal hashcodes");
324
325             }
326
327             tree4.add("abcdef");
328             assertFalse(tree3.equals(tree4));
329             assertFalse(tree4.equals(tree3));
330         }
331     }
332     /* (non-Javadoc)
333      * @see java.util.Map#clear()
334      */

335     public void clear() {
336         super.clear();
337         order.clear();
338     }
339 }
340
Popular Tags