From 3658a26e136b5dc8bc82d97345271ff695ebe9ca Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Wed, 2 Dec 2009 21:29:40 +0000 Subject: [PATCH] - Change of heart: Let's follow the example of EnumSet, which does not permit null (or, obviously, elements of the wrong type), but is gracious about simply returning false if an invalid Object is passed to contains() or remove(). - Implement a dumb size(). - toIntArray() impl now deals with oversize IntSets. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2657 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/AbstractIntSet.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) 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