commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1683009 - in /commons/proper/collections/trunk/src: main/java/org/apache/commons/collections4/ test/java/org/apache/commons/collections4/
Date Mon, 01 Jun 2015 21:53:01 GMT
Author: tn
Date: Mon Jun  1 21:53:01 2015
New Revision: 1683009

URL: http://svn.apache.org/r1683009
Log:
[COLLECTIONS-551] First bunch of methods which take an Iterable/Iterator/Enumeration as input
shall be moved to the appropriate Util classes. Mark existing methods in CollectionUtils as
deprecated.

Modified:
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
Mon Jun  1 21:53:01 2015
@@ -806,7 +806,7 @@ public class CollectionUtils {
      * @param transformer  the transformer to perform, may be null
      */
     public static <C> void transform(final Collection<C> collection,
-            final Transformer<? super C, ? extends C> transformer) {
+                                     final Transformer<? super C, ? extends C> transformer)
{
 
         if (collection != null && transformer != null) {
             if (collection instanceof List<?>) {
@@ -832,17 +832,11 @@ public class CollectionUtils {
      * @param input  the {@link Iterable} to get the input from, may be null
      * @param predicate  the predicate to use, may be null
      * @return the number of matches for the predicate in the collection
+     * @deprecated since 4.1, use {@link IterableUtils#frequency(Iterable, Predicate)} instead
      */
+    @Deprecated
     public static <C> int countMatches(final Iterable<C> input, final Predicate<?
super C> predicate) {
-        int count = 0;
-        if (input != null && predicate != null) {
-            for (final C o : input) {
-                if (predicate.evaluate(o)) {
-                    count++;
-                }
-            }
-        }
-        return count;
+        return predicate == null ? 0 : (int) IterableUtils.frequency(input, predicate);
     }
 
     /**
@@ -855,16 +849,11 @@ public class CollectionUtils {
      * @param input  the {@link Iterable} to get the input from, may be null
      * @param predicate  the predicate to use, may be null
      * @return true if at least one element of the collection matches the predicate
+     * @deprecated since 4.1, use {@link IterableUtils#matchesAny(Iterable, Predicate)} instead
      */
+    @Deprecated
     public static <C> boolean exists(final Iterable<C> input, final Predicate<?
super C> predicate) {
-        if (input != null && predicate != null) {
-            for (final C o : input) {
-                if (predicate.evaluate(o)) {
-                    return true;
-                }
-            }
-        }
-        return false;
+        return predicate == null ? false : IterableUtils.matchesAny(input, predicate);
     }
 
     /**
@@ -880,20 +869,11 @@ public class CollectionUtils {
      * @return true if every element of the collection matches the predicate or if the
      * collection is empty, false otherwise
      * @since 4.0
+     * @deprecated since 4.1, use {@link IterableUtils#matchesAll(Iterable, Predicate)} instead
      */
+    @Deprecated
     public static <C> boolean matchesAll(final Iterable<C> input, final Predicate<?
super C> predicate) {
-        if (predicate == null) {
-            return false;
-        }
-
-        if (input != null) {
-            for (final C o : input) {
-                if (!predicate.evaluate(o)) {
-                    return false;
-                }
-            }
-        }
-        return true;
+        return predicate == null ? false : IterableUtils.matchesAll(input, predicate);
     }
 
     /**
@@ -909,7 +889,7 @@ public class CollectionUtils {
      * @throws NullPointerException if the input collection is null
      */
     public static <O> Collection<O> select(final Iterable<? extends O>
inputCollection,
-            final Predicate<? super O> predicate) {
+                                           final Predicate<? super O> predicate) {
         final Collection<O> answer = inputCollection instanceof Collection<?>
?
                 new ArrayList<O>(((Collection<?>) inputCollection).size()) :
new ArrayList<O>();
         return select(inputCollection, predicate, answer);
@@ -957,7 +937,7 @@ public class CollectionUtils {
      * @throws NullPointerException if the input collection is null
      */
     public static <O> Collection<O> selectRejected(final Iterable<? extends
O> inputCollection,
-            final Predicate<? super O> predicate) {
+                                                   final Predicate<? super O> predicate)
{
         final Collection<O> answer = inputCollection instanceof Collection<?>
?
                 new ArrayList<O>(((Collection<?>) inputCollection).size()) :
new ArrayList<O>();
         return selectRejected(inputCollection, predicate, answer);
@@ -1020,7 +1000,7 @@ public class CollectionUtils {
      * @since 4.1
      */
     public static <O> List<List<O>> partition(final Iterable<? extends
O> inputCollection,
-            final Predicate<? super O> predicate) {
+                                              final Predicate<? super O> predicate)
{
 
         @SuppressWarnings({ "unchecked", "rawtypes" }) // safe
         final Factory<List<O>> factory = FactoryUtils.instantiateFactory((Class)
ArrayList.class);
@@ -1103,7 +1083,7 @@ public class CollectionUtils {
      * @since 4.1
      */
     public static <O> List<List<O>> partition(final Iterable<? extends
O> inputCollection,
-            final Predicate<? super O>... predicates) {
+                                              final Predicate<? super O>... predicates)
{
 
         @SuppressWarnings({ "unchecked", "rawtypes" }) // safe
         final Factory<List<O>> factory = FactoryUtils.instantiateFactory((Class)
ArrayList.class);
@@ -1392,35 +1372,6 @@ public class CollectionUtils {
     }
 
     /**
-     * Returns the <code>index</code>-th value in the {@link Enumeration}, throwing
-     * <code>IndexOutOfBoundsException</code> if there is no such element.
-     * <p>
-     * The Enumeration is advanced to <code>index</code> (or to the end, if
-     * <code>index</code> exceeds the number of entries) as a side effect of
this method.
-     *
-     * @param e  the enumeration to get a value from
-     * @param index  the index to get
-     * @param <T> the type of object in the {@link Enumeration}
-     * @return the object at the specified index
-     * @throws IndexOutOfBoundsException if the index is invalid
-     * @throws IllegalArgumentException if the object type is invalid
-     * @since 4.1
-     */
-    public static <T> T get(final Enumeration<T> e, final int index) {
-        int i = index;
-        checkIndexBounds(i);
-        while (e.hasMoreElements()) {
-            i--;
-            if (i == -1) {
-                return e.nextElement();
-            } else {
-                e.nextElement();
-            }
-        }
-        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
-    }
-
-    /**
      * Ensures an index is not negative.
      * @param index the index to check.
      * @throws IndexOutOfBoundsException if the index is negative.
@@ -1487,18 +1438,21 @@ public class CollectionUtils {
         if (object instanceof Map<?,?>) {
             final Map<?, ?> map = (Map<?, ?>) object;
             final Iterator<?> iterator = map.entrySet().iterator();
-            return get(iterator, i);
+            return IteratorUtils.get(iterator, i);
         } else if (object instanceof Object[]) {
             return ((Object[]) object)[i];
         } else if (object instanceof Iterator<?>) {
             final Iterator<?> it = (Iterator<?>) object;
-            return get(it, i);
+            return IteratorUtils.get(it, i);
+        } else if (object instanceof Iterable<?>) {
+            final Iterable<?> iterable = (Iterable<?>) object;
+            return IterableUtils.get(iterable, i);
         } else if (object instanceof Collection<?>) {
             final Iterator<?> iterator = ((Collection<?>) object).iterator();
-            return get(iterator, i);
+            return IteratorUtils.get(iterator, i);
         } else if (object instanceof Enumeration<?>) {
             final Enumeration<?> it = (Enumeration<?>) object;
-            return get(it, i);
+            return EnumerationUtils.get(it, i);
         } else if (object == null) {
             throw new IllegalArgumentException("Unsupported object type: null");
         } else {
@@ -1552,14 +1506,12 @@ public class CollectionUtils {
             total = ((Map<?, ?>) object).size();
         } else if (object instanceof Collection<?>) {
             total = ((Collection<?>) object).size();
+        } else if (object instanceof Iterable<?>) {
+            total = IterableUtils.size((Iterable<?>) object);
         } else if (object instanceof Object[]) {
             total = ((Object[]) object).length;
         } else if (object instanceof Iterator<?>) {
-            final Iterator<?> it = (Iterator<?>) object;
-            while (it.hasNext()) {
-                total++;
-                it.next();
-            }
+            total = IteratorUtils.size((Iterator<?>) object);
         } else if (object instanceof Enumeration<?>) {
             final Enumeration<?> it = (Enumeration<?>) object;
             while (it.hasMoreElements()) {
@@ -1601,6 +1553,8 @@ public class CollectionUtils {
             return true;
         } else if (object instanceof Collection<?>) {
             return ((Collection<?>) object).isEmpty();
+        } else if (object instanceof Iterable<?>) {
+            return IterableUtils.isEmpty((Iterable<?>) object);
         } else if (object instanceof Map<?, ?>) {
             return ((Map<?, ?>) object).isEmpty();
         } else if (object instanceof Object[]) {
@@ -2031,7 +1985,7 @@ public class CollectionUtils {
      * @since 4.1
      */
     public static <E> boolean contains(final Collection<? extends E> collection,
final E object,
-            final Equator<? super E> equator) {
+                                       final Equator<? super E> equator) {
         for (final E obj : collection) {
             if (equator.equate(obj, object)) {
                 return true;

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/EnumerationUtils.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
Mon Jun  1 21:53:01 2015
@@ -37,6 +37,35 @@ public class EnumerationUtils {
     private EnumerationUtils() {}
 
     /**
+     * Returns the <code>index</code>-th value in the {@link Enumeration}, throwing
+     * <code>IndexOutOfBoundsException</code> if there is no such element.
+     * <p>
+     * The Enumeration is advanced to <code>index</code> (or to the end, if
+     * <code>index</code> exceeds the number of entries) as a side effect of
this method.
+     *
+     * @param e  the enumeration to get a value from
+     * @param index  the index to get
+     * @param <T> the type of object in the {@link Enumeration}
+     * @return the object at the specified index
+     * @throws IndexOutOfBoundsException if the index is invalid
+     * @throws IllegalArgumentException if the object type is invalid
+     * @since 4.1
+     */
+    public static <T> T get(final Enumeration<T> e, final int index) {
+        int i = index;
+        CollectionUtils.checkIndexBounds(i);
+        while (e.hasMoreElements()) {
+            i--;
+            if (i == -1) {
+                return e.nextElement();
+            } else {
+                e.nextElement();
+            }
+        }
+        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
+    }
+
+    /**
      * Creates a list based on an enumeration.
      *
      * <p>As the enumeration is traversed, an ArrayList of its values is

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
Mon Jun  1 21:53:01 2015
@@ -28,6 +28,11 @@ import org.apache.commons.collections4.i
 
 /**
  * Provides utility methods and decorators for {@link Iterable} instances.
+ * <p>
+ * <b>Note</b>: by design, all provided utility methods will treat a {@code null}
+ * {@link Iterable} parameters the same way as an empty iterable. All other required
+ * parameters which are null, e.g. a {@link Predicate}, will result in a
+ * {@link NullPointerException}.
  *
  * @since 4.1
  * @version $Id$
@@ -488,6 +493,32 @@ public class IterableUtils {
     }
 
     /**
+     * Counts the number of elements in the input iterable that match the predicate.
+     * <p>
+     * A <code>null</code> iterable matches no elements.
+     *
+     * @param <E>  the type of object the {@link Iterable} contains
+     * @param input  the {@link Iterable} to get the input from, may be null
+     * @param predicate  the predicate to use, may not be null
+     * @return the number of matches for the predicate in the collection
+     * @throws NullPointerException if predicate is null
+     */
+    public static <E> long frequency(final Iterable<E> input, final Predicate<?
super E> predicate) {
+        if (predicate == null) {
+            throw new NullPointerException("Predicate must not be null.");
+        }
+        long count = 0;
+        if (input != null) {
+            for (final E o : input) {
+                if (predicate.evaluate(o)) {
+                    count++;
+                }
+            }
+        }
+        return count;
+    }
+
+    /**
      * Answers true if the provided iterable is empty.
      * <p>
      * A <code>null</code> iterable returns true.

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
Mon Jun  1 21:53:01 2015
@@ -927,7 +927,8 @@ public class IteratorUtils {
      * @throws IllegalArgumentException if any iterator is null
      * @since 4.1
      */
-    public static <E> ZippingIterator<E> zippingIterator(final Iterator<?
extends E> a, final Iterator<? extends E> b) {
+    public static <E> ZippingIterator<E> zippingIterator(final Iterator<?
extends E> a,
+                                                         final Iterator<? extends E>
b) {
         return new ZippingIterator<E>(a, b);
     }
 
@@ -1176,8 +1177,8 @@ public class IteratorUtils {
         if (obj instanceof Iterator) {
             return (Iterator<?>) obj;
         }
-        if (obj instanceof Collection) {
-            return ((Collection<?>) obj).iterator();
+        if (obj instanceof Iterable) {
+            return ((Iterable<?>) obj).iterator();
         }
         if (obj instanceof Object[]) {
             return new ObjectArrayIterator<Object>((Object[]) obj);

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
Mon Jun  1 21:53:01 2015
@@ -785,6 +785,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test
+    @Deprecated
     public void getFromIterator() throws Exception {
         // Iterator, entry exists
         Iterator<Integer> iterator = iterableA.iterator();
@@ -803,6 +804,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test
+    @Deprecated
     public void getFromEnumeration() throws Exception {
         // Enumeration, entry exists
         final Vector<String> vector = new Vector<String>();
@@ -824,6 +826,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test(expected = IndexOutOfBoundsException.class)
+    @Deprecated
     public void getFromIterable() throws Exception {
         // Collection, entry exists
         final Bag<String> bag = new HashBag<String>();
@@ -849,7 +852,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test(expected = IndexOutOfBoundsException.class)
-    public void getFromPrimativeArray() throws Exception {
+    public void getFromPrimitiveArray() throws Exception {
         // Primitive array, entry exists
         final int[] array = new int[2];
         array[0] = 10;
@@ -1126,6 +1129,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test
+    @Deprecated
     public void countMatches() {
         assertEquals(4, CollectionUtils.countMatches(iterableB, EQUALS_TWO));
         assertEquals(0, CollectionUtils.countMatches(iterableA, null));
@@ -1134,6 +1138,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test
+    @Deprecated
     public void exists() {
         final List<Integer> list = new ArrayList<Integer>();
         assertFalse(CollectionUtils.exists(null, null));
@@ -1834,6 +1839,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test
+    @Deprecated
     public void testMatchesAll() {
         assertFalse(CollectionUtils.matchesAll(null, null));
         assertFalse(CollectionUtils.matchesAll(collectionA, null));

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
Mon Jun  1 21:53:01 2015
@@ -16,26 +16,29 @@
  */
 package org.apache.commons.collections4;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.StringTokenizer;
+import java.util.Vector;
 
-import junit.framework.Test;
+import org.junit.Test;
 
 /**
  * Tests EnumerationUtils.
  *
  * @version $Id$
  */
-public class EnumerationUtilsTest extends BulkTest {
-
-    public EnumerationUtilsTest(final String name) {
-        super(name);
-    }
+public class EnumerationUtilsTest {
 
     public static final String TO_LIST_FIXTURE = "this is a test";
 
+    @Test
     public void testToListWithStringTokenizer() {
         final List<String> expectedList1 = new ArrayList<String>();
         final StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE);
@@ -53,6 +56,7 @@ public class EnumerationUtilsTest extend
         assertEquals(expectedList2, actualList);
     }
 
+    @Test
     public void testToListWithHashtable() {
         final Hashtable<String, Integer> expected = new Hashtable<String, Integer>();
         expected.put("one", Integer.valueOf(1));
@@ -83,8 +87,25 @@ public class EnumerationUtilsTest extend
         assertTrue(actualKeyList.containsAll(expectedKeyList));
     }
 
-    public static Test suite() {
-        return BulkTest.makeSuite(EnumerationUtilsTest.class);
+    @Test
+    public void getFromEnumeration() throws Exception {
+        // Enumeration, entry exists
+        final Vector<String> vector = new Vector<String>();
+        vector.addElement("zero");
+        vector.addElement("one");
+        Enumeration<String> en = vector.elements();
+        assertEquals("zero", EnumerationUtils.get(en, 0));
+        en = vector.elements();
+        assertEquals("one", EnumerationUtils.get(en, 1));
+
+        // Enumerator, non-existent entry
+        try {
+            EnumerationUtils.get(en, 3);
+            fail("Expecting IndexOutOfBoundsException.");
+        } catch (final IndexOutOfBoundsException e) {
+            // expected
+        }
+        assertTrue(!en.hasMoreElements());
     }
 
 }

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
Mon Jun  1 21:53:01 2015
@@ -16,10 +16,20 @@
  */
 package org.apache.commons.collections4;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;
 
-import junit.framework.Test;
+import org.apache.commons.collections4.bag.HashBag;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Tests for IterableUtils.
@@ -27,22 +37,24 @@ import junit.framework.Test;
  * @since 4.1
  * @version $Id$
  */
-public class IterableUtilsTest extends BulkTest {
+public class IterableUtilsTest {
 
     /**
      * Iterable of {@link Integer}s
      */
     private Iterable<Integer> iterableA = null;
-    
-    public IterableUtilsTest(final String name) {
-        super(name);
-    }
-    
-    public static Test suite() {
-        return BulkTest.makeSuite(IterableUtilsTest.class);
-    }
-    
-    @Override
+
+    /**
+     * Iterable of {@link Long}s
+     */
+    private Iterable<Long> iterableB = null;
+
+    /**
+     * An empty Iterable.
+     */
+    private Iterable<Integer> emptyIterable = null;
+
+    @Before
     public void setUp() {
         List<Integer> listA = new ArrayList<Integer>();
         listA.add(1);
@@ -56,8 +68,125 @@ public class IterableUtilsTest extends B
         listA.add(4);
         listA.add(4);
         iterableA = listA;
+
+        Collection<Long> collectionB = new LinkedList<Long>();
+        collectionB.add(5L);
+        collectionB.add(4L);
+        collectionB.add(4L);
+        collectionB.add(3L);
+        collectionB.add(3L);
+        collectionB.add(3L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        iterableB = collectionB;
+
+        emptyIterable = Collections.emptyList();
+    }
+
+    private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() {
+        public boolean evaluate(final Number input) {
+            return input.intValue() == 2;
+        }
+    };
+
+    // -----------------------------------------------------------------------
+    @Test
+    public void frequency() {
+        assertEquals(4, IterableUtils.frequency(iterableB, EQUALS_TWO));
+        assertEquals(0, IterableUtils.frequency(null, EQUALS_TWO));
+
+        try {
+            assertEquals(0, IterableUtils.frequency(iterableA, null));
+            fail("predicate must not be null");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+
+        try {
+            assertEquals(0, IterableUtils.frequency(null, null));
+            fail("predicate must not be null");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public void matchesAny() {
+        final List<Integer> list = new ArrayList<Integer>();
+        
+        try {
+            assertFalse(IterableUtils.matchesAny(null, null));
+            fail("predicate must not be null");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+
+        try {
+            assertFalse(IterableUtils.matchesAny(list, null));
+            fail("predicate must not be null");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+
+        assertFalse(IterableUtils.matchesAny(null, EQUALS_TWO));
+        assertFalse(IterableUtils.matchesAny(list, EQUALS_TWO));
+        list.add(1);
+        list.add(3);
+        list.add(4);
+        assertFalse(IterableUtils.matchesAny(list, EQUALS_TWO));
+
+        list.add(2);
+        assertEquals(true, IterableUtils.matchesAny(list, EQUALS_TWO));
     }
-    
+
+    @Test
+    public void matchesAll() {
+        try {
+            assertFalse(IterableUtils.matchesAll(null, null));
+            fail("predicate must not be null");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+
+        try {
+            assertFalse(IterableUtils.matchesAll(iterableA, null));
+            fail("predicate must not be null");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+
+        Predicate<Integer> lessThanFive = new Predicate<Integer>() {
+            public boolean evaluate(Integer object) {
+                return object < 5;
+            }
+        };
+        assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive));
+
+        Predicate<Integer> lessThanFour = new Predicate<Integer>() {
+            public boolean evaluate(Integer object) {
+                return object < 4;
+            }
+        };
+        assertFalse(IterableUtils.matchesAll(iterableA, lessThanFour));
+
+        assertTrue(IterableUtils.matchesAll(null, lessThanFour));
+        assertTrue(IterableUtils.matchesAll(emptyIterable, lessThanFour));
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void getFromIterable() throws Exception {
+        // Collection, entry exists
+        final Bag<String> bag = new HashBag<String>();
+        bag.add("element", 1);
+        assertEquals("element", IterableUtils.get(bag, 0));
+
+        // Collection, non-existent entry
+        IterableUtils.get(bag, 1);
+    }
+
+    @Test
     public void testToString() {
         String result = IterableUtils.toString(iterableA);
         assertEquals("[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]", result);
@@ -91,7 +220,8 @@ public class IterableUtilsTest extends B
         });
         assertEquals("[]", result);
     }
-    
+
+    @Test
     public void testToStringDelimiter() {
         
         Transformer<Integer, String> transformer = new Transformer<Integer, String>()
{
@@ -127,7 +257,8 @@ public class IterableUtilsTest extends B
         result = IterableUtils.toString(new ArrayList<Integer>(), transformer, "",
"", "");
         assertEquals("", result);
     }
-    
+
+    @Test
     public void testToStringWithNullArguments() {
         String result = IterableUtils.toString(null, new Transformer<Integer, String>()
{
             public String transform(Integer input) {

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java?rev=1683009&r1=1683008&r2=1683009&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
Mon Jun  1 21:53:01 2015
@@ -16,6 +16,12 @@
  */
 package org.apache.commons.collections4;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -23,13 +29,13 @@ import java.util.List;
 import java.util.ListIterator;
 import java.util.NoSuchElementException;
 
-import junit.framework.Test;
-
 import org.apache.commons.collections4.iterators.EmptyIterator;
 import org.apache.commons.collections4.iterators.EmptyListIterator;
 import org.apache.commons.collections4.iterators.EmptyMapIterator;
 import org.apache.commons.collections4.iterators.EmptyOrderedIterator;
 import org.apache.commons.collections4.iterators.EmptyOrderedMapIterator;
+import org.junit.Before;
+import org.junit.Test;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
@@ -42,16 +48,33 @@ import static org.easymock.EasyMock.repl
  *
  * @version $Id$
  */
-public class IteratorUtilsTest extends BulkTest {
+public class IteratorUtilsTest {
 
-    public IteratorUtilsTest(final String name) {
-        super(name);
-    }
+    /**
+     * Collection of {@link Integer}s
+     */
+    private List<Integer> collectionA = null;
+
+    private Iterable<Integer> iterableA = null;
 
-    public static Test suite() {
-        return BulkTest.makeSuite(IteratorUtilsTest.class);
+    @Before
+    public void setUp() {
+        collectionA = new ArrayList<Integer>();
+        collectionA.add(1);
+        collectionA.add(2);
+        collectionA.add(2);
+        collectionA.add(3);
+        collectionA.add(3);
+        collectionA.add(3);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionA.add(4);
+
+        iterableA = collectionA;
     }
 
+    @Test
     public void testAsIterable() {
         final List<Integer> list = new ArrayList<Integer>();
         list.add(Integer.valueOf(0));
@@ -72,6 +95,7 @@ public class IteratorUtilsTest extends B
         assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext());
     }
 
+    @Test
     public void testAsIterableNull() {
         try {
             IteratorUtils.asIterable(null);
@@ -81,6 +105,7 @@ public class IteratorUtilsTest extends B
         }
     }
 
+    @Test
     public void testAsMultipleIterable() {
         final List<Integer> list = new ArrayList<Integer>();
         list.add(Integer.valueOf(0));
@@ -107,6 +132,7 @@ public class IteratorUtilsTest extends B
         assertTrue(expected > 0);
     }
 
+    @Test
     public void testAsMultipleIterableNull() {
         try {
             IteratorUtils.asMultipleUseIterable(null);
@@ -116,6 +142,7 @@ public class IteratorUtilsTest extends B
         }
     }
 
+    @Test
     public void testToList() {
         final List<Object> list = new ArrayList<Object>();
         list.add(Integer.valueOf(1));
@@ -125,6 +152,7 @@ public class IteratorUtilsTest extends B
         assertEquals(list, result);
     }
 
+    @Test
     public void testToArray() {
         final List<Object> list = new ArrayList<Object>();
         list.add(Integer.valueOf(1));
@@ -134,6 +162,7 @@ public class IteratorUtilsTest extends B
         assertEquals(list, Arrays.asList(result));
     }
 
+    @Test
     public void testToArray2() {
         final List<String> list = new ArrayList<String>();
         list.add("One");
@@ -143,6 +172,7 @@ public class IteratorUtilsTest extends B
         assertEquals(list, Arrays.asList(result));
     }
 
+    @Test
     public void testArrayIterator() {
         final Object[] objArray = {"a", "b", "c"};
         ResettableIterator<Object> iterator = IteratorUtils.arrayIterator(objArray);
@@ -263,6 +293,7 @@ public class IteratorUtilsTest extends B
         }
     }
 
+    @Test
     public void testArrayListIterator() {
         final Object[] objArray = {"a", "b", "c", "d"};
         ResettableListIterator<Object> iterator = IteratorUtils.arrayListIterator(objArray);
@@ -454,6 +485,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test empty iterator
      */
+    @Test
     public void testEmptyIterator() {
         assertSame(EmptyIterator.INSTANCE, IteratorUtils.EMPTY_ITERATOR);
         assertSame(EmptyIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_ITERATOR);
@@ -480,6 +512,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test empty list iterator
      */
+    @Test
     public void testEmptyListIterator() {
         assertSame(EmptyListIterator.INSTANCE, IteratorUtils.EMPTY_LIST_ITERATOR);
         assertSame(EmptyListIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_LIST_ITERATOR);
@@ -520,6 +553,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test empty map iterator
      */
+    @Test
     @SuppressWarnings("unchecked")
     public void testEmptyMapIterator() {
         assertSame(EmptyMapIterator.INSTANCE, IteratorUtils.EMPTY_MAP_ITERATOR);
@@ -559,6 +593,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test empty map iterator
      */
+    @Test
     @SuppressWarnings("unchecked")
     public void testEmptyOrderedIterator() {
         assertSame(EmptyOrderedIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_ITERATOR);
@@ -590,6 +625,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test empty map iterator
      */
+    @Test
     @SuppressWarnings("unchecked")
     public void testEmptyOrderedMapIterator() {
         assertSame(EmptyOrderedMapIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR);
@@ -633,6 +669,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test next() and hasNext() for an immutable Iterator.
      */
+    @Test
     public void testUnmodifiableIteratorIteration() {
         final Iterator<String> iterator = getImmutableIterator();
 
@@ -659,6 +696,7 @@ public class IteratorUtilsTest extends B
      * Test next(), hasNext(), previous() and hasPrevious() for an immutable
      * ListIterator.
      */
+    @Test
     public void testUnmodifiableListIteratorIteration() {
         final ListIterator<String> listIterator = getImmutableListIterator();
 
@@ -709,6 +747,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test remove() for an immutable Iterator.
      */
+    @Test
     public void testUnmodifiableIteratorImmutability() {
         final Iterator<String> iterator = getImmutableIterator();
 
@@ -735,6 +774,7 @@ public class IteratorUtilsTest extends B
     /**
      * Test remove() for an immutable ListIterator.
      */
+    @Test
     public void testUnmodifiableListIteratorImmutability() {
         final ListIterator<String> listIterator = getImmutableListIterator();
 
@@ -792,6 +832,7 @@ public class IteratorUtilsTest extends B
     /**
      * Tests method nodeListIterator(NodeList)
      */
+    @Test
     public void testNodeListIterator() {
         final Node[] nodes = createNodes();
         final NodeList nodeList = createNodeList(nodes);
@@ -812,6 +853,7 @@ public class IteratorUtilsTest extends B
     /**
      * Tests method nodeListIterator(Node)
      */
+    @Test
     public void testNodeIterator() {
         final Node[] nodes = createNodes();
         final NodeList nodeList = createNodeList(nodes);
@@ -863,4 +905,22 @@ public class IteratorUtilsTest extends B
         };
     }
 
+    @Test
+    public void getFromIterator() throws Exception {
+        // Iterator, entry exists
+        Iterator<Integer> iterator = iterableA.iterator();
+        assertEquals(1, (int) IteratorUtils.get(iterator, 0));
+        iterator = iterableA.iterator();
+        assertEquals(2, (int) IteratorUtils.get(iterator, 1));
+
+        // Iterator, non-existent entry
+        try {
+            IteratorUtils.get(iterator, 10);
+            fail("Expecting IndexOutOfBoundsException.");
+        } catch (final IndexOutOfBoundsException e) {
+            // expected
+        }
+        assertTrue(!iterator.hasNext());
+    }
+
 }



Mime
View raw message