1 17 18 21 package org.quartz.utils; 22 23 import java.lang.reflect.Array ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 38 public class DirtyFlagMap implements Map , Cloneable , java.io.Serializable { 39 40 47 private static final long serialVersionUID = 1433884852607126222L; 48 49 private boolean dirty = false; 50 private Map map; 51 52 59 60 67 public DirtyFlagMap() { 68 map = new HashMap (); 69 } 70 71 79 public DirtyFlagMap(int initialCapacity) { 80 map = new HashMap (initialCapacity); 81 } 82 83 91 public DirtyFlagMap(int initialCapacity, float loadFactor) { 92 map = new HashMap (initialCapacity, loadFactor); 93 } 94 95 102 103 108 public void clearDirtyFlag() { 109 dirty = false; 110 } 111 112 117 public boolean isDirty() { 118 return dirty; 119 } 120 121 126 public Map getWrappedMap() { 127 return map; 128 } 129 130 public void clear() { 131 if (map.isEmpty() == false) { 132 dirty = true; 133 } 134 135 map.clear(); 136 } 137 138 public boolean containsKey(Object key) { 139 return map.containsKey(key); 140 } 141 142 public boolean containsValue(Object val) { 143 return map.containsValue(val); 144 } 145 146 public Set entrySet() { 147 return new DirtyFlagMapEntrySet(map.entrySet()); 148 } 149 150 public boolean equals(Object obj) { 151 if (obj == null || !(obj instanceof DirtyFlagMap)) { 152 return false; 153 } 154 155 return map.equals(((DirtyFlagMap) obj).getWrappedMap()); 156 } 157 158 public Object get(Object key) { 159 return map.get(key); 160 } 161 162 public boolean isEmpty() { 163 return map.isEmpty(); 164 } 165 166 public Set keySet() { 167 return new DirtyFlagSet(map.keySet()); 168 } 169 170 public Object put(Object key, Object val) { 171 dirty = true; 172 173 return map.put(key, val); 174 } 175 176 public void putAll(Map t) { 177 if (!t.isEmpty()) { 178 dirty = true; 179 } 180 181 map.putAll(t); 182 } 183 184 public Object remove(Object key) { 185 Object obj = map.remove(key); 186 187 if (obj != null) { 188 dirty = true; 189 } 190 191 return obj; 192 } 193 194 public int size() { 195 return map.size(); 196 } 197 198 public Collection values() { 199 return new DirtyFlagCollection(map.values()); 200 } 201 202 public Object clone() { 203 DirtyFlagMap copy; 204 try { 205 copy = (DirtyFlagMap) super.clone(); 206 if (map instanceof HashMap ) { 207 copy.map = (Map )((HashMap )map).clone(); 208 } 209 } catch (CloneNotSupportedException ex) { 210 throw new IncompatibleClassChangeError ("Not Cloneable."); 211 } 212 213 return copy; 214 } 215 216 220 private class DirtyFlagCollection implements Collection { 221 private Collection collection; 222 223 public DirtyFlagCollection(Collection c) { 224 collection = c; 225 } 226 227 protected Collection getWrappedCollection() { 228 return collection; 229 } 230 231 public Iterator iterator() { 232 return new DirtyFlagIterator(collection.iterator()); 233 } 234 235 public boolean remove(Object o) { 236 boolean removed = collection.remove(o); 237 if (removed) { 238 dirty = true; 239 } 240 return removed; 241 } 242 243 public boolean removeAll(Collection c) { 244 boolean changed = collection.removeAll(c); 245 if (changed) { 246 dirty = true; 247 } 248 return changed; 249 } 250 251 public boolean retainAll(Collection c) { 252 boolean changed = collection.retainAll(c); 253 if (changed) { 254 dirty = true; 255 } 256 return changed; 257 } 258 259 public void clear() { 260 if (collection.isEmpty() == false) { 261 dirty = true; 262 } 263 collection.clear(); 264 } 265 266 public int size() { return collection.size(); } 268 public boolean isEmpty() { return collection.isEmpty(); } 269 public boolean contains(Object o) { return collection.contains(o); } 270 public boolean add(Object o) { return add(o); } public boolean addAll(Collection c) { return collection.addAll(c); } public boolean containsAll(Collection c) { return collection.containsAll(c); } 273 public Object [] toArray() { return collection.toArray(); } 274 public Object [] toArray(Object [] array) { return collection.toArray(array); } 275 } 276 277 281 private class DirtyFlagSet extends DirtyFlagCollection implements Set { 282 public DirtyFlagSet(Set set) { 283 super(set); 284 } 285 286 protected Set getWrappedSet() { 287 return (Set )getWrappedCollection(); 288 } 289 } 290 291 295 private class DirtyFlagIterator implements Iterator { 296 private Iterator iterator; 297 298 public DirtyFlagIterator(Iterator iterator) { 299 this.iterator = iterator; 300 } 301 302 public void remove() { 303 dirty = true; 304 iterator.remove(); 305 } 306 307 public boolean hasNext() { return iterator.hasNext(); } 309 public Object next() { return iterator.next(); } 310 } 311 312 317 private class DirtyFlagMapEntrySet extends DirtyFlagSet { 318 319 public DirtyFlagMapEntrySet(Set set) { 320 super(set); 321 } 322 323 public Iterator iterator() { 324 return new DirtyFlagMapEntryIterator(getWrappedSet().iterator()); 325 } 326 327 public Object [] toArray() { 328 return toArray(new Object [super.size()]); 329 } 330 331 public Object [] toArray(Object [] array) { 332 if (array.getClass().getComponentType().isAssignableFrom(Map.Entry .class) == false) { 333 throw new IllegalArgumentException ("Array must be of type assignable from Map.Entry"); 334 } 335 336 int size = super.size(); 337 338 Object [] result = 339 (array.length < size) ? 340 (Object [])Array.newInstance(array.getClass().getComponentType(), size) : array; 341 342 Iterator entryIter = iterator(); for (int i = 0; i < size; i++) { 344 result[i] = entryIter.next(); 345 } 346 347 if (result.length > size) { 348 result[size] = null; 349 } 350 351 return result; 352 } 353 } 354 355 359 private class DirtyFlagMapEntryIterator extends DirtyFlagIterator { 360 public DirtyFlagMapEntryIterator(Iterator iterator) { 361 super(iterator); 362 } 363 364 public Object next() { 365 return new DirtyFlagMapEntry((Map.Entry )super.next()); 366 } 367 } 368 369 373 private class DirtyFlagMapEntry implements Map.Entry { 374 private Map.Entry entry; 375 376 public DirtyFlagMapEntry(Map.Entry entry) { 377 this.entry = entry; 378 } 379 380 public Object setValue(Object o) { 381 dirty = true; 382 return entry.setValue(o); 383 } 384 385 public Object getKey() { return entry.getKey(); } 387 public Object getValue() { return entry.getValue(); } 388 } 389 } | Popular Tags |