diff --git a/src/java/com/samskivert/util/ArrayIntSet.java b/src/java/com/samskivert/util/ArrayIntSet.java index 60bf6f4f..e1509170 100644 --- a/src/java/com/samskivert/util/ArrayIntSet.java +++ b/src/java/com/samskivert/util/ArrayIntSet.java @@ -285,39 +285,43 @@ public class ArrayIntSet extends AbstractSet // documentation inherited from interface public boolean containsAll (Collection c) { - Iterator iter = c.iterator(); - while (iter.hasNext()) { - if (!contains(iter.next())) { - return false; + if (c instanceof Interable) { + Interator inter = ((Interable) c).interator(); + while (inter.hasNext()) { + if (!contains(inter.nextInt())) { + return false; + } } + return true; + + } else { + return super.containsAll(c); } - return true; } // documentation inherited from interface public boolean addAll (Collection c) { - boolean modified = false; - if (c instanceof ArrayIntSet) { - ArrayIntSet other = (ArrayIntSet)c; - for (int ii = 0; ii < other._size; ii++) { - modified = (add(other._values[ii]) || modified); + if (c instanceof Interable) { + Interator inter = ((Interable) c).interator(); + boolean modified = false; + while (inter.hasNext()) { + if (add(other._values[ii])) { + modified = true; + } } + return modified; } else { - Iterator iter = c.iterator(); - while (iter.hasNext()) { - modified = (add(iter.next()) || modified); - } + return super.addAll(c); } - return modified; } // documentation inherited from interface public boolean retainAll (Collection c) { - if (c instanceof ArrayIntSet) { - ArrayIntSet other = (ArrayIntSet)c; + if (c instanceof IntSet) { + IntSet other = (IntSet)c; int removals = 0; // go through our array sliding all elements in the union @@ -337,7 +341,7 @@ public class ArrayIntSet extends AbstractSet return (removals > 0); } else { - throw new UnsupportedOperationException(); + return super.retainAll(c); } } @@ -355,6 +359,8 @@ public class ArrayIntSet extends AbstractSet ArrayIntSet other = (ArrayIntSet)o; if (other._size == _size) { for (int ii = 0; ii < _size; ii++) { + // we can't use Arrays.equals() because we only want to + // compare the first _size values if (_values[ii] != other._values[ii]) { return false; } @@ -381,8 +387,7 @@ public class ArrayIntSet extends AbstractSet { try { ArrayIntSet nset = (ArrayIntSet)super.clone(); - nset._values = new int[_values.length]; - System.arraycopy(_values, 0, nset._values, 0, _size); + nset._values = _values.clone(); return nset; } catch (CloneNotSupportedException cnse) { diff --git a/src/java/com/samskivert/util/IntSet.java b/src/java/com/samskivert/util/IntSet.java index 677278db..d74beaa2 100644 --- a/src/java/com/samskivert/util/IntSet.java +++ b/src/java/com/samskivert/util/IntSet.java @@ -29,7 +29,7 @@ import java.util.Set; * provides all of the standard methods (in which Integer * objects will be converted to ints). */ -public interface IntSet extends Set +public interface IntSet extends Set, Interable { /** * Returns true if this set contains the specified element. diff --git a/src/java/com/samskivert/util/Interable.java b/src/java/com/samskivert/util/Interable.java new file mode 100644 index 00000000..5fe55e52 --- /dev/null +++ b/src/java/com/samskivert/util/Interable.java @@ -0,0 +1,30 @@ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2006 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +/** + * An interface similar to java.util.Iterable. + */ +public interface Interable +{ + /** + * Return an Interator over the ints in this object. + */ + public Interator interator (); +}