1 47 48 package org.ozoneDB.collections; 49 50 import java.io.Serializable ; 51 import java.util.Collection ; 52 import java.util.Comparator ; 53 import java.util.Iterator ; 54 import java.util.Set ; 55 import java.util.SortedMap ; 56 import java.util.SortedSet ; 57 import java.util.TreeMap ; 58 import java.util.TreeSet ; 59 60 97 public abstract class BaseTreeSetImpl extends AbstractOzoneSet implements BaseTreeSet, Cloneable , Serializable { 99 100 private static final long serialVersionUID = 1L; 101 102 105 protected SortedMap map; 108 private transient Object ctorParam; 109 110 117 public BaseTreeSetImpl() { 118 ctorParam = null; 119 } 120 121 125 protected BaseTreeSetImpl(int ctorWithoutCreatingNewBackingMap) { 126 ctorParam = System.out; 128 } 129 130 137 public BaseTreeSetImpl(Comparator comparator) { 138 ctorParam = comparator; 139 } 140 141 153 public BaseTreeSetImpl(Collection collection) { 154 ctorParam = collection; 155 } 156 157 166 public BaseTreeSetImpl(SortedSet sortedSet) { 167 ctorParam = sortedSet; 168 } 169 170 public void onCreate() { 171 if (ctorParam == null) { 172 map = newBackingMap(); 173 } else if (ctorParam instanceof Comparator ) { 174 map = newBackingMap((Comparator ) ctorParam); 175 } else if (ctorParam instanceof OzoneSortedSet) { 176 OzoneSortedSet sortedSet = (OzoneSortedSet) ctorParam; 177 map = newBackingMap(sortedSet.comparator()); 178 Iterator i = sortedSet._org_ozoneDB_internalIterator(); 179 ((BaseTreeMap) map)._org_ozoneDB_putKeysLinear(i, sortedSet.size()); 180 } else if (ctorParam instanceof SortedSet ) { 181 SortedSet sortedSet = (SortedSet ) ctorParam; 182 map = newBackingMap(sortedSet.comparator()); 183 Iterator i = sortedSet.iterator(); 184 ((BaseTreeMap) map)._org_ozoneDB_putKeysLinear(i, sortedSet.size()); 185 } else if (ctorParam instanceof Collection ) { 186 map = newBackingMap(); 187 addAll((Collection ) ctorParam); 188 } 189 190 map.keySet(); 193 } 194 195 203 public boolean add(Object obj) { 204 return map.put(obj, "") == null; 205 } 206 207 216 public boolean addAll(Collection c) { 217 boolean result = false; 218 int pos = c.size(); 219 Iterator itr; 220 if (c instanceof OzoneCollection) { 221 itr = ((OzoneCollection) c)._org_ozoneDB_internalIterator(); 222 } else { 223 itr = c.iterator(); 224 } 225 while (--pos >= 0) { 226 result |= (map.put(itr.next(), "") == null); 227 } 228 return result; 229 } 230 231 234 public void clear() { 235 map.clear(); 236 } 237 238 243 public Comparator comparator() { 244 return map.comparator(); 245 } 246 247 255 public boolean contains(Object obj) { 256 return map.containsKey(obj); 257 } 258 259 265 public Object first() { 266 return map.firstKey(); 267 } 268 269 274 public boolean isEmpty() { 275 return map.isEmpty(); 276 } 277 278 284 public Iterator iterator() { 285 return map.keySet().iterator(); 286 } 287 288 public Iterator _org_ozoneDB_internalIterator() { 289 return ((OzoneSet) map.keySet())._org_ozoneDB_internalIterator(); 290 } 291 292 298 public Object last() { 299 return map.lastKey(); 300 } 301 302 310 public boolean remove(Object obj) { 311 return map.remove(obj) != null; 312 } 313 314 319 public int size() { 320 return map.size(); 321 } 322 323 339 public Collection getClientCollection() { 340 return getClientTreeSet(); 341 } 342 343 354 public Set getClientSet() { 355 return getClientTreeSet(); 356 } 357 358 369 public SortedSet getClientSortedSet() { 370 return getClientTreeSet(); 371 } 372 373 378 public OzoneSortedSet ozoneHeadSet(Object toElement) { 379 return (OzoneSortedSet) headSet(toElement); 380 } 381 382 387 public OzoneSortedSet ozoneSubSet(Object fromElement, Object toElement) { 388 return (OzoneSortedSet) subSet(fromElement, toElement); 389 } 390 391 396 public OzoneSortedSet ozoneTailSet(Object toElement) { 397 return (OzoneSortedSet) tailSet(toElement); 398 } 399 400 411 public TreeSet getClientTreeSet() { 412 TreeSet result = new TreeSet (comparator()); 416 for (Iterator i = _org_ozoneDB_internalIterator(); i.hasNext(); ) { 417 result.add(i.next()); 418 } 419 return result; 420 } 421 422 443 public String toString() { 444 return (map == null) ? "[]" : super.toString(); 445 } 446 447 protected abstract SortedMap newBackingMap(); 448 449 protected abstract SortedMap newBackingMap(Comparator comparator); 450 451 } | Popular Tags |