- 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
This commit is contained in:
ray.j.greenwell
2009-12-02 21:29:40 +00:00
parent 57dffcb1a5
commit 3658a26e13
@@ -27,8 +27,8 @@ import java.util.Iterator;
/** /**
* A base class for {@link IntSet} implementations.<p> * A base class for {@link IntSet} implementations.<p>
* *
* All you really need to do is implement <tt>interable</tt> and <tt>size</tt>, * All you really need to do is implement <tt>interable</tt>, but you'll almost certainly want
* although for performance reasons you'll probably want to override <tt>contains</tt>.<p> * to implement <tt>size</tt> and <tt>contains</tt> for enhanced performance.<p>
* *
* To implement a modifiable IntSet, the programmer must additionally override this class's * To implement a modifiable IntSet, the programmer must additionally override this class's
* <tt>add</tt> and <tt>remove</tt> methods, which will otherwise throw an * <tt>add</tt> and <tt>remove</tt> methods, which will otherwise throw an
@@ -46,7 +46,7 @@ public abstract class AbstractIntSet extends AbstractSet<Integer>
// from IntSet // from IntSet
public boolean contains (int value) public boolean contains (int value)
{ {
// abstract implementation. You should override. // dumb implementation. You should override.
for (Interator it = interator(); it.hasNext(); ) { for (Interator it = interator(); it.hasNext(); ) {
if (it.nextInt() == value) { if (it.nextInt() == value) {
return true; return true;
@@ -55,6 +55,21 @@ public abstract class AbstractIntSet extends AbstractSet<Integer>
return false; return false;
} }
/**
* {@inheritDoc}
*
* <p>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 // from IntSet
public boolean add (int value) public boolean add (int value)
{ {
@@ -77,9 +92,9 @@ public abstract class AbstractIntSet extends AbstractSet<Integer>
public int[] toIntArray () public int[] toIntArray ()
{ {
int[] vals = new int[size()]; int[] vals = new int[size()];
int ii=0; int ii = 0;
for (Interator intr = interator(); intr.hasNext(); ) { for (Interator it = interator(); (ii < Integer.MAX_VALUE) && it.hasNext(); ) {
vals[ii++] = intr.nextInt(); vals[ii++] = it.nextInt();
} }
return vals; return vals;
} }
@@ -93,8 +108,8 @@ public abstract class AbstractIntSet extends AbstractSet<Integer>
@Override // from AbstractSet<Integer> @Override // from AbstractSet<Integer>
public boolean contains (Object o) public boolean contains (Object o)
{ {
// let's go ahead and NPE or CCE if an Integer is not specified // cope with null or non-Integer
return /* (o instanceof Integer) && */ contains(((Integer)o).intValue()); return (o instanceof Integer) && contains(((Integer)o).intValue());
} }
@Override // from AbstractSet<Integer> @Override // from AbstractSet<Integer>
@@ -106,8 +121,8 @@ public abstract class AbstractIntSet extends AbstractSet<Integer>
@Override // from AbstractSet<Integer> @Override // from AbstractSet<Integer>
public boolean remove (Object o) public boolean remove (Object o)
{ {
// let's go ahead and NPE or CCE if an Integer is not specified // cope with null or non-Integer
return /* (o instanceof Integer) && */ remove(((Integer)o).intValue()); return (o instanceof Integer) && remove(((Integer)o).intValue());
} }
@Override // from AbstractSet<Integer> @Override // from AbstractSet<Integer>