Allow passing a null array to CollectionUtil.addAll() which behaves like an

empty array.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1953 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-10-17 19:21:36 +00:00
parent 13b914952d
commit 26c16e8ad4
@@ -32,8 +32,8 @@ import java.util.List;
public class CollectionUtil public class CollectionUtil
{ {
/** /**
* Adds all items returned by the enumeration to the supplied * Adds all items returned by the enumeration to the supplied collection
* collection and returns the supplied collection. * and returns the supplied collection.
*/ */
public static <E, T extends Collection<E>> T addAll ( public static <E, T extends Collection<E>> T addAll (
T col, Enumeration<E> enm) T col, Enumeration<E> enm)
@@ -45,8 +45,8 @@ public class CollectionUtil
} }
/** /**
* Adds all items returned by the iterator to the supplied collection * Adds all items returned by the iterator to the supplied collection and
* and returns the supplied collection. * returns the supplied collection.
*/ */
public static <E, T extends Collection<E>> T addAll ( public static <E, T extends Collection<E>> T addAll (
T col, Iterator<E> iter) T col, Iterator<E> iter)
@@ -58,13 +58,16 @@ public class CollectionUtil
} }
/** /**
* Adds all items in the given object array to the supplied * Adds all items in the given object array to the supplied collection and
* collection and returns the supplied collection. * returns the supplied collection. If the supplied array is null, nothing
* is added to the collection.
*/ */
public static <E, T extends Collection<E>> T addAll (T col, E[] values) public static <E, T extends Collection<E>> T addAll (T col, E[] values)
{ {
for (int ii = 0; ii < values.length; ii++) { if (values != null) {
col.add(values[ii]); for (int ii = 0; ii < values.length; ii++) {
col.add(values[ii]);
}
} }
return col; return col;
} }
@@ -72,18 +75,16 @@ public class CollectionUtil
/** /**
* Returns a list containing a random selection of elements from the * Returns a list containing a random selection of elements from the
* specified collection. The total number of elements selected will be * specified collection. The total number of elements selected will be
* equal to <code>count</code>, each element in the source collection * equal to <code>count</code>, each element in the source collection will
* will be selected with equal probability and no element will be * be selected with equal probability and no element will be included more
* included more than once. The elements in the random subset will * than once. The elements in the random subset will always be included in
* always be included in the order they are returned from the source * the order they are returned from the source collection's iterator.
* collection's iterator.
* *
* <p> Algorithm courtesy of William R. Mahoney, published in * <p> Algorithm courtesy of William R. Mahoney, published in
* <cite>Dr. Dobbs Journal, February 2002</cite>. * <cite>Dr. Dobbs Journal, February 2002</cite>.
* *
* @exception IllegalArgumentException thrown if the size of the * @exception IllegalArgumentException thrown if the size of the collection
* collection is smaller than the number of elements requested for the * is smaller than the number of elements requested for the random subset.
* random subset.
*/ */
public static <T> List<T> selectRandomSubset (Collection<T> col, int count) public static <T> List<T> selectRandomSubset (Collection<T> col, int count)
{ {
@@ -101,10 +102,10 @@ public class CollectionUtil
for (int k = 0; iter.hasNext(); k++) { for (int k = 0; iter.hasNext(); k++) {
T elem = iter.next(); T elem = iter.next();
// the probability that an element is select for inclusion in // the probability that an element is select for inclusion in our
// our random subset is proportional to the number of elements // random subset is proportional to the number of elements
// remaining to be checked for inclusion divided by the number // remaining to be checked for inclusion divided by the number of
// of elements remaining to be included // elements remaining to be included
float limit = ((float)(count - s)) / ((float)(csize - k)); float limit = ((float)(count - s)) / ((float)(csize - k));
// include the record if our random value is below the limit // include the record if our random value is below the limit