1 19 20 package de.gulden.util; 21 22 import de.gulden.util.OrderedHashMap.OrderedHashMapEntry; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.*; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Map.Entry; 31 import java.util.Set ; 32 33 39 public class OrderedHashMap extends HashMap { 40 41 45 48 protected ArrayListSet keyList; 49 50 53 protected ArrayListSet valueList; 54 55 56 60 63 public OrderedHashMap() { 64 super(); 65 keyList=new ArrayListSet(); 66 valueList=new ArrayListSet(); 67 } 68 69 70 74 public Object 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 put(Object key, Object value) { 87 int i=keyList.indexOf(key); 88 if (i!=-1) { 89 keyList.remove(i); 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 map) { 98 super.putAll(map); 99 Collection mapKeys=map.keySet(); 101 Collection mapValues=map.values(); 102 keyList.removeAll(mapKeys); 103 keyList.addAll(mapKeys); 104 valueList.removeAll(mapValues); 105 valueList.addAll(mapValues); 106 } 107 108 public Object remove(Object 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 values() { 118 return valueList; 119 } 120 121 public Set keySet() { 122 return keyList; 123 } 124 125 public Set entrySet() { 126 ArrayListSet set=new ArrayListSet(); 127 for (int i=0;i<keyList.size();i++) { 128 Object key=keyList.get(i); 129 Object value=valueList.get(i); 130 set.add(new OrderedHashMapEntry(key,value)); 131 } 132 return set; 133 } 134 135 139 public void load(InputStream in) throws IOException { 140 java.io.BufferedReader r=new java.io.BufferedReader (new java.io.InputStreamReader (in)); 141 String line=r.readLine(); 142 while (line!=null) { 143 Properties p=new Properties(); p.load(new java.io.StringBufferInputStream (line)); 145 for (Enumeration e=p.keys();e.hasMoreElements();) { String key=(String )e.nextElement(); 147 String value=p.getProperty(key); 148 this.put(key,value); 149 } 150 line=r.readLine(); 151 } 152 } 153 154 158 public void save(OutputStream out) throws IOException { 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 171 177 public class OrderedHashMapEntry implements Map.Entry { 178 179 183 186 protected Object key; 187 188 191 protected Object value; 192 193 194 198 201 public OrderedHashMapEntry() { 202 } 204 205 208 public OrderedHashMapEntry(Object key, Object value) { 209 this(); 210 setKey(key); 211 setValue(value); 212 } 213 214 215 219 public boolean equals(Object o) { 220 return false; 222 } 223 224 public int hashCode() { 225 return 0; 227 } 228 229 232 public Object getKey() { 233 return key; 234 } 235 236 239 public void setKey(Object _key) { 240 key = _key; 241 } 242 243 246 public Object getValue() { 247 return value; 248 } 249 250 253 public Object setValue(Object _value) { 254 Object old=value; 255 value = _value; 256 return old; 257 } 258 259 } 261 } | Popular Tags |