1 18 19 package org.apache.jorphan.collections; 20 import java.io.IOException ; 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectOutputStream ; 23 import java.io.Serializable ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 43 public class ListedHashTree extends HashTree implements Serializable , Cloneable 44 { 45 private List order; 46 47 public ListedHashTree() 48 { 49 data = new HashMap (); 50 order = new LinkedList (); 51 } 52 53 public Object clone() 54 { 55 ListedHashTree newTree = new ListedHashTree(); 56 cloneTree(newTree); 57 return newTree; 58 } 59 60 public ListedHashTree(Object key) 61 { 62 data = new HashMap (); 63 order = new LinkedList (); 64 data.put(key, new ListedHashTree()); 65 order.add(key); 66 } 67 68 public ListedHashTree(Collection keys) 69 { 70 data = new HashMap (); 71 order = new LinkedList (); 72 Iterator it = keys.iterator(); 73 while (it.hasNext()) 74 { 75 Object temp = it.next(); 76 data.put(temp, new ListedHashTree()); 77 order.add(temp); 78 } 79 } 80 81 public ListedHashTree(Object [] keys) 82 { 83 data = new HashMap (); 84 order = new LinkedList (); 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 key, Object 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 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 key, Object [] 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 key, Collection 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 currentKey, Object 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 key) 142 { 143 return new ListedHashTree(key); 144 } 145 146 public HashTree createNewTree(Collection values) 147 { 148 return new ListedHashTree(values); 149 } 150 151 public HashTree add(Object 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 list() 167 { 168 return order; 169 } 170 171 public Object remove(Object key) 172 { 173 order.remove(key); 174 return data.remove(key); 175 } 176 177 public Object [] getArray() 178 { 179 return order.toArray(); 180 } 181 182 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 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 } 230 231 public Set keySet() 232 { 233 return data.keySet(); 234 } 235 236 public int size() 237 { 238 return data.size(); 239 } 240 241 void readObject(ObjectInputStream ois) 242 throws ClassNotFoundException , IOException 243 { 244 ois.defaultReadObject(); 245 } 246 247 void writeObject(ObjectOutputStream oos) throws IOException 248 { 249 oos.defaultWriteObject(); 250 } 251 252 public static class Test extends junit.framework.TestCase 253 { 254 public Test(String name) 255 { 256 super(name); 257 } 258 259 public void testAddObjectAndTree() throws Exception 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 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 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 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 335 public void clear() { 336 super.clear(); 337 order.clear(); 338 } 339 } 340 | Popular Tags |