1 9 package javolution.util; 10 11 import java.io.IOException ; 12 13 import j2me.io.ObjectInputStream; 14 import j2me.io.ObjectOutputStream; 15 import j2me.util.Collection; 16 import j2me.util.Set; 17 import javolution.lang.Reusable; 18 19 36 public class FastSet extends FastCollection implements Set, Reusable { 37 38 41 private static final Factory FACTORY = new Factory() { 42 43 public Object create() { 44 return new FastSet(); 45 } 46 47 public void cleanup(Object obj) { 48 ((FastSet) obj).reset(); 49 } 50 }; 51 52 55 private transient FastMap _map; 56 57 60 public FastSet() { 61 this(new FastMap()); 62 } 63 64 72 public FastSet(String id) { 73 this(new FastMap(id)); 74 } 75 76 83 public FastSet(int capacity) { 84 this(new FastMap(capacity)); 85 } 86 87 93 public FastSet(Set elements) { 94 this(new FastMap(elements.size())); 95 addAll(elements); 96 } 97 98 103 private FastSet(FastMap map) { 104 _map = map; 105 } 106 107 114 public static FastSet newInstance() { 115 return (FastSet) FACTORY.object(); 116 } 117 118 123 public static void recycle(FastSet instance) { 124 FACTORY.recycle(instance); 125 } 126 127 132 public final int size() { 133 return _map.size(); 134 } 135 136 144 public final boolean add(Object value) { 145 return _map.put(value, value) == null; 146 } 147 148 public Collectionunmodifiable() { 150 return (Collection) super.unmodifiable(); 151 } 152 153 public final void clear() { 155 _map.clear(); 156 } 157 158 public final boolean contains(Object o) { 160 return _map.containsKey(o); 161 } 162 163 public final boolean remove(Object o) { 165 return _map.remove(o) != null; 166 } 167 168 174 public FastSet setValueComparator(FastComparator comparator) { 175 _map.setKeyComparator(comparator); 176 return this; 177 } 178 179 public FastComparator getValueComparator() { 181 return _map.getKeyComparator(); 182 } 183 184 public void reset() { 186 super.reset(); 187 _map.reset(); 188 } 189 190 private void readObject(ObjectInputStream stream) throws IOException , 192 ClassNotFoundException { 193 FastComparator cmp = (FastComparator) stream.readObject(); 194 final int size = stream.readInt(); 195 _map = new FastMap(size); 196 this.setValueComparator(cmp); 197 for (int i = size; i-- != 0;) { 198 Object key = stream.readObject(); 199 _map.put(key, key); 200 } 201 } 202 203 private void writeObject(ObjectOutputStream stream) throws IOException { 205 stream.writeObject(getValueComparator()); 206 stream.writeInt(size()); 207 for (FastMap.Entry e = _map.head(), end = _map.tail(); 208 (e = (FastMap.Entry) e.getNext()) != end;) { 209 stream.writeObject(e.getKey()); 210 } 211 } 212 213 public final Record head() { 215 return _map.head(); 216 } 217 218 public final Record tail() { 220 return _map.tail(); 221 } 222 223 public final Object valueOf(Record record) { 225 return (Object ) ((FastMap.Entry) record).getKey(); 226 } 227 228 public final void delete(Record record) { 230 _map.remove(((FastMap.Entry) record).getKey()); 231 } 232 233 private static final long serialVersionUID = 1L; 234 } | Popular Tags |