diff --git a/src/java/com/samskivert/util/AbstractIntSet.java b/src/java/com/samskivert/util/AbstractIntSet.java index 0fab42e5..996359f1 100644 --- a/src/java/com/samskivert/util/AbstractIntSet.java +++ b/src/java/com/samskivert/util/AbstractIntSet.java @@ -27,8 +27,8 @@ import java.util.Iterator; /** * A base class for {@link IntSet} implementations.

* - * All you really need to do is implement interable and size, - * although for performance reasons you'll probably want to override contains.

+ * All you really need to do is implement interable, but you'll almost certainly want + * to implement size and contains for enhanced performance.

* * To implement a modifiable IntSet, the programmer must additionally override this class's * add and remove methods, which will otherwise throw an @@ -46,7 +46,7 @@ public abstract class AbstractIntSet extends AbstractSet // from IntSet public boolean contains (int value) { - // abstract implementation. You should override. + // dumb implementation. You should override. for (Interator it = interator(); it.hasNext(); ) { if (it.nextInt() == value) { return true; @@ -55,6 +55,21 @@ public abstract class AbstractIntSet extends AbstractSet return false; } + /** + * {@inheritDoc} + * + *

This implementation simply counts the elements in the interator. + */ + public int size () + { + // dumb implementation. You should override. + int size = 0; + for (Interator it = interator(); (size < Integer.MAX_VALUE) && it.hasNext(); it.nextInt()) { + size++; + } + return size; + } + // from IntSet public boolean add (int value) { @@ -77,9 +92,9 @@ public abstract class AbstractIntSet extends AbstractSet public int[] toIntArray () { int[] vals = new int[size()]; - int ii=0; - for (Interator intr = interator(); intr.hasNext(); ) { - vals[ii++] = intr.nextInt(); + int ii = 0; + for (Interator it = interator(); (ii < Integer.MAX_VALUE) && it.hasNext(); ) { + vals[ii++] = it.nextInt(); } return vals; } @@ -93,8 +108,8 @@ public abstract class AbstractIntSet extends AbstractSet @Override // from AbstractSet public boolean contains (Object o) { - // let's go ahead and NPE or CCE if an Integer is not specified - return /* (o instanceof Integer) && */ contains(((Integer)o).intValue()); + // cope with null or non-Integer + return (o instanceof Integer) && contains(((Integer)o).intValue()); } @Override // from AbstractSet @@ -106,8 +121,8 @@ public abstract class AbstractIntSet extends AbstractSet @Override // from AbstractSet public boolean remove (Object o) { - // let's go ahead and NPE or CCE if an Integer is not specified - return /* (o instanceof Integer) && */ remove(((Integer)o).intValue()); + // cope with null or non-Integer + return (o instanceof Integer) && remove(((Integer)o).intValue()); } @Override // from AbstractSet