add various "count" methods to IterableTools and IteratorTools
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java
index 6a77ae7..5d0d728 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java
@@ -80,10 +80,10 @@
}
/**
- * Return whether the specified array contains a <code>null</code>.
+ * Return whether the specified iterable contains a <code>null</code>.
*/
public static boolean containsNull(Iterable<?> iterable) {
- return contains(iterable, null);
+ return IteratorTools.containsNull(iterable.iterator());
}
/**
@@ -95,6 +95,30 @@
}
/**
+ * Return the number of times the specified element occurs in the specified
+ * iterable.
+ */
+ public static int count(Iterable<?> iterable, Object value) {
+ return IteratorTools.count(iterable.iterator(), value);
+ }
+
+ /**
+ * Return the number of times the specified predicate evaluates to
+ * <code>false</code> with the elements in the specified iterable.
+ */
+ public static <E> int countFalse(Iterable<? extends E> iterable, Predicate<? super E> predicate) {
+ return IteratorTools.countFalse(iterable.iterator(), predicate);
+ }
+
+ /**
+ * Return the number of times the specified predicate evaluates to
+ * <code>true</code> with the elements in the specified iterable.
+ */
+ public static <E> int countTrue(Iterable<? extends E> iterable, Predicate<? super E> predicate) {
+ return IteratorTools.countTrue(iterable.iterator(), predicate);
+ }
+
+ /**
* Return whether the specified iterable contains all of the
* elements in the specified collection.
*/
@@ -239,10 +263,11 @@
}
/**
- * Return the first element corresponding in the specified iterable.
+ * Return the specified iterable's first element.
+ * @exception java.util.NoSuchElementException iterable is empty.
*/
- public static <E> E first(Iterable<? extends E> iterable) {
- return iterable.iterator().next();
+ public static <E> E first(Iterable<E> iterable) {
+ return IteratorTools.first(iterable.iterator());
}
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java
index 0e051f0..3c95c00 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java
@@ -71,6 +71,13 @@
}
/**
+ * Return whether the specified iterator contains a <code>null</code>.
+ */
+ public static boolean containsNull(Iterator<?> iterator) {
+ return contains(iterator, null);
+ }
+
+ /**
* Return whether the specified iterator contains the
* specified element.
*/
@@ -92,6 +99,56 @@
}
/**
+ * Return the number of times the specified element occurs in the specified
+ * iterator.
+ */
+ public static int count(Iterator<?> iterator, Object value) {
+ int count = 0;
+ if (value == null) {
+ while (iterator.hasNext()) {
+ if (iterator.next() == null) {
+ count++;
+ }
+ }
+ } else {
+ while (iterator.hasNext()) {
+ if (value.equals(iterator.next())) {
+ count++;
+ }
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Return the number of times the specified predicate evaluates to
+ * <code>false</code> with the elements in the specified iterator.
+ */
+ public static <E> int countFalse(Iterator<? extends E> iterator, Predicate<? super E> predicate) {
+ int count = 0;
+ while (iterator.hasNext()) {
+ if ( ! predicate.evaluate(iterator.next())) {
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Return the number of times the specified predicate evaluates to
+ * <code>true</code> with the elements in the specified iterator.
+ */
+ public static <E> int countTrue(Iterator<? extends E> iterator, Predicate<? super E> predicate) {
+ int count = 0;
+ while (iterator.hasNext()) {
+ if (predicate.evaluate(iterator.next())) {
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
* Return whether the specified iterator contains all of the
* elements in the specified collection.
*/
@@ -392,6 +449,14 @@
* Return the specified iterator's last element.
* @exception java.util.NoSuchElementException iterator is empty.
*/
+ public static <E> E first(Iterator<E> iterator) {
+ return iterator.next();
+ }
+
+ /**
+ * Return the specified iterator's last element.
+ * @exception java.util.NoSuchElementException iterator is empty.
+ */
public static <E> E last(Iterator<E> iterator) {
E last;
do {
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/IterableToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/IterableToolsTests.java
index 65fbaaf..9b2d52d 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/IterableToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/IterableToolsTests.java
@@ -19,10 +19,12 @@
import java.util.TreeSet;
import junit.framework.TestCase;
import org.eclipse.jpt.common.utility.internal.ClassTools;
+import org.eclipse.jpt.common.utility.internal.collection.HashBag;
import org.eclipse.jpt.common.utility.internal.comparator.ReverseComparator;
import org.eclipse.jpt.common.utility.internal.iterable.EmptyIterable;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.tests.internal.ArrayToolsTests;
+import org.eclipse.jpt.common.utility.tests.internal.collection.MapToolsTests;
@SuppressWarnings("nls")
public class IterableToolsTests
@@ -41,6 +43,47 @@
assertTrue(IterableTools.contains(iterable, null));
}
+ public void testCountIterableObject() {
+ Collection<Object> c = new HashBag<Object>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ String one = "one";
+ assertEquals(1, IterableTools.count(c, one));
+ c.add("one");
+ assertEquals(2, IterableTools.count(c, one));
+ assertEquals(0, IterableTools.count(c, null));
+ c.add(null);
+ assertEquals(1, IterableTools.count(c, null));
+ c.add(null);
+ assertEquals(2, IterableTools.count(c, null));
+ }
+
+ public void testCountFalseIteratorPredicate() {
+ Collection<String> c = new HashBag<String>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ assertEquals(4, IterableTools.countFalse(c, new MapToolsTests.StringLengthPredicate(0)));
+ assertEquals(2, IterableTools.countFalse(c, new MapToolsTests.StringLengthPredicate(3)));
+ c.add("foo");
+ assertEquals(2, IterableTools.countFalse(c, new MapToolsTests.StringLengthPredicate(3)));
+ }
+
+ public void testCountTrueIteratorPredicate() {
+ Collection<String> c = new HashBag<String>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ assertEquals(0, IterableTools.countTrue(c, new MapToolsTests.StringLengthPredicate(0)));
+ assertEquals(2, IterableTools.countTrue(c, new MapToolsTests.StringLengthPredicate(3)));
+ c.add("foo");
+ assertEquals(3, IterableTools.countTrue(c, new MapToolsTests.StringLengthPredicate(3)));
+ }
+
public void testContainsAllIterableCollection() {
Iterable<String> iterable = this.buildStringList1();
assertTrue(IterableTools.containsAll(iterable, this.buildStringList1()));
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterator/IteratorToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterator/IteratorToolsTests.java
index 435bc23..8cc0b3a 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterator/IteratorToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterator/IteratorToolsTests.java
@@ -27,6 +27,7 @@
import org.eclipse.jpt.common.utility.internal.iterator.EmptyIterator;
import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools;
import org.eclipse.jpt.common.utility.tests.internal.ArrayToolsTests;
+import org.eclipse.jpt.common.utility.tests.internal.collection.MapToolsTests;
@SuppressWarnings("nls")
public class IteratorToolsTests
@@ -61,6 +62,50 @@
}
+ // ********** count **********
+
+ public void testCountIteratorObject() {
+ Collection<Object> c = new HashBag<Object>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ String one = "one";
+ assertEquals(1, IteratorTools.count(c.iterator(), one));
+ c.add("one");
+ assertEquals(2, IteratorTools.count(c.iterator(), one));
+ assertEquals(0, IteratorTools.count(c.iterator(), null));
+ c.add(null);
+ assertEquals(1, IteratorTools.count(c.iterator(), null));
+ c.add(null);
+ assertEquals(2, IteratorTools.count(c.iterator(), null));
+ }
+
+ public void testCountFalseIteratorPredicate() {
+ Collection<String> c = new HashBag<String>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ assertEquals(4, IteratorTools.countFalse(c.iterator(), new MapToolsTests.StringLengthPredicate(0)));
+ assertEquals(2, IteratorTools.countFalse(c.iterator(), new MapToolsTests.StringLengthPredicate(3)));
+ c.add("foo");
+ assertEquals(2, IteratorTools.countFalse(c.iterator(), new MapToolsTests.StringLengthPredicate(3)));
+ }
+
+ public void testCountTrueIteratorPredicate() {
+ Collection<String> c = new HashBag<String>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ assertEquals(0, IteratorTools.countTrue(c.iterator(), new MapToolsTests.StringLengthPredicate(0)));
+ assertEquals(2, IteratorTools.countTrue(c.iterator(), new MapToolsTests.StringLengthPredicate(3)));
+ c.add("foo");
+ assertEquals(3, IteratorTools.countTrue(c.iterator(), new MapToolsTests.StringLengthPredicate(3)));
+ }
+
+
// ********** contains all **********
public void testContainsAllIteratorCollection_StringString() {
@@ -372,6 +417,28 @@
}
+ // ********** first **********
+
+ public void testFirstIterator1() {
+ List<String> list = this.buildStringList1();
+ assertEquals("zero", IteratorTools.first(list.iterator()));
+ list.add(0, null);
+ assertEquals(null, IteratorTools.first(list.iterator()));
+ }
+
+ public void testFirstIterator2() {
+ List<String> list = new ArrayList<String>();
+ boolean exCaught = false;
+ try {
+ IteratorTools.first(list.iterator());
+ fail();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+
// ********** last **********
public void testLastIterator1() {