1 21 package com.db4o.foundation; 22 23 26 public class SortedCollection4 { 27 28 private final Comparison4 _comparison; 29 private Tree _tree; 30 31 public SortedCollection4(Comparison4 comparison) { 32 if (null == comparison) { 33 throw new ArgumentNullException(); 34 } 35 _comparison = comparison; 36 _tree = null; 37 } 38 39 public Object singleElement() { 40 if (1 != size()) { 41 throw new IllegalStateException (); 42 } 43 return _tree.key(); 44 } 45 46 public void addAll(Iterator4 iterator) { 47 while (iterator.moveNext()) { 48 add(iterator.current()); 49 } 50 } 51 52 public void add(Object element) { 53 _tree = Tree.add(_tree, new TreeObject(element, _comparison)); 54 } 55 56 public void remove(Object element) { 57 _tree = Tree.removeLike(_tree, new TreeObject(element, _comparison)); 58 } 59 60 public Object [] toArray(final Object [] array) { 61 Tree.traverse(_tree, new Visitor4() { 62 int i = 0; 63 public void visit(Object obj) { 64 array[i++] = ((TreeObject)obj).key(); 65 } 66 }); 67 return array; 68 } 69 70 public int size() { 71 return Tree.size(_tree); 72 } 73 74 } 75 | Popular Tags |