1 5 package com.tc.util.concurrent; 6 7 import com.tc.exception.ImplementMe; 8 9 import java.util.Collection ; 10 import java.util.Hashtable ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 35 public class CopyOnWriteArrayMap extends Hashtable { 36 37 private volatile Object _values[] = new Object [0]; 38 39 public CopyOnWriteArrayMap() { 40 super(); 41 } 42 43 public CopyOnWriteArrayMap(int initialCapacity, float loadFactor) { 44 super(initialCapacity, loadFactor); 45 } 46 47 public CopyOnWriteArrayMap(int initialCapacity) { 48 super(initialCapacity); 49 } 50 51 public CopyOnWriteArrayMap(Map t) { 52 super(t); 53 } 54 55 public synchronized void clear() { 56 super.clear(); 57 _values = new Object [0]; 58 } 59 60 public Set entrySet() { 61 throw new ImplementMe("This is not implemented yet due to lack to time." 62 + " Support for remove needs to be provided by wrapping super iterators with my own iterators"); 63 } 64 65 public Set keySet() { 66 throw new ImplementMe("This is not implemented yet due to lack to time." 67 + " Support for remove needs to be provided by wrapping super iterators with my own iterators"); 68 } 69 70 public synchronized Object put(Object key, Object value) { 71 Object old = super.put(key, value); 72 if (old == null) { 73 Object [] old_values = _values; 74 _values = new Object [old_values.length + 1]; 75 System.arraycopy(old_values, 0, _values, 0, old_values.length); 76 _values[old_values.length] = value; 77 } else { 78 Object [] old_values = _values; 79 int length = old_values.length; 80 _values = new Object [length]; 82 for (int i = 0; i < length; i++) { 83 _values[i] = (old == old_values[i] ? value : old_values[i]); 84 } 85 } 86 return old; 87 } 88 89 public synchronized void putAll(Map t) { 90 super.putAll(t); 92 } 93 94 public synchronized Object remove(Object key) { 95 Object old = super.remove(key); 96 if (old != null) { 97 Object [] old_values = _values; 98 int length = old_values.length; 99 _values = new Object [length - 1]; 100 int i = 0; 101 for (int j = 0; j < length; j++) { 102 if (old != old_values[j]) { 103 _values[i++] = old_values[j]; 104 } 105 } 106 } 107 return old; 108 } 109 110 public Collection values() { 111 throw new ImplementMe("This is not implemented yet due to lack to time." 112 + " Support for remove needs to be provided by wrapping super iterators with my own iterators"); 113 } 114 115 public synchronized Object [] valuesArray() { 116 return _values; 117 } 118 } 119 | Popular Tags |