1 19 20 package jode.util; 21 import java.util.AbstractSet ; 22 import java.util.Iterator ; 23 24 public class SimpleSet extends AbstractSet implements Cloneable 25 { 26 Object [] elementObjects; 27 int count = 0; 28 29 public SimpleSet() { 30 this(2); 31 } 32 33 public SimpleSet(int initialSize) { 34 elementObjects = new Object [initialSize]; 35 } 36 37 public int size() { 38 return count; 39 } 40 41 public boolean add(Object element) { 42 if (element == null) 43 throw new NullPointerException (); 44 45 for (int i=0; i< count; i++) { 46 if (element.equals(elementObjects[i])) 47 return false; 48 } 49 50 if (count == elementObjects.length) { 51 Object [] newArray = new Object [(count+1)*3/2]; 52 System.arraycopy(elementObjects,0,newArray,0,count); 53 elementObjects = newArray; 54 } 55 elementObjects[count++] = element; 56 return true; 57 } 58 59 public Object clone() { 60 try { 61 SimpleSet other = (SimpleSet) super.clone(); 62 other.elementObjects = (Object []) elementObjects.clone(); 63 return other; 64 } catch (CloneNotSupportedException ex) { 65 throw new jode.AssertError("Clone?"); 66 } 67 } 68 69 public Iterator iterator() { 70 return new Iterator () { 71 int pos = 0; 72 73 public boolean hasNext() { 74 return pos < count; 75 } 76 77 public Object next() { 78 return elementObjects[pos++]; 79 } 80 81 public void remove() { 82 if (pos < count) 83 System.arraycopy(elementObjects, pos, 84 elementObjects, pos-1, count - pos); 85 count--; 86 pos--; 87 } 88 }; 89 } 90 } 91 | Popular Tags |