commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1683492 - in /commons/proper/collections/trunk/src: main/java/org/apache/commons/collections4/ main/java/org/apache/commons/collections4/functors/ test/java/org/apache/commons/collections4/
Date Thu, 04 Jun 2015 08:52:55 GMT
Author: tn
Date: Thu Jun  4 08:52:55 2015
New Revision: 1683492

URL: http://svn.apache.org/r1683492
Log:
[COLLECTIONS-551] Move more methods from CollectionUtils.

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/IterableUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.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/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=1683492&r1=1683491&r2=1683492&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
Thu Jun  4 08:52:55 2015
@@ -650,16 +650,11 @@ public class CollectionUtils {
      * @param collection  the collection to search, may be null
      * @param predicate  the predicate to use, may be null
      * @return the first element of the collection which matches the predicate or null if
none could be found
+     * @deprecated since 4.1, use {@link IterableUtils#find(Iterable, Predicate)} instead
      */
+    @Deprecated
     public static <T> T find(final Iterable<T> collection, final Predicate<?
super T> predicate) {
-        if (collection != null && predicate != null) {
-            for (final T item : collection) {
-                if (predicate.evaluate(item)) {
-                    return item;
-                }
-            }
-        }
-        return null;
+        return predicate != null ? IterableUtils.find(collection, predicate) : null;
     }
 
     /**
@@ -672,12 +667,12 @@ public class CollectionUtils {
      * @param collection  the collection to get the input from, may be null
      * @param closure  the closure to perform, may be null
      * @return closure
+     * @deprecated since 4.1, use {@link IterableUtils#apply(Iterable, Closure)} instead
      */
+    @Deprecated
     public static <T, C extends Closure<? super T>> C forAllDo(final Iterable<T>
collection, final C closure) {
-        if (collection != null && closure != null) {
-            for (final T element : collection) {
-                closure.execute(element);
-            }
+        if (closure != null) {
+            IterableUtils.apply(collection, closure);
         }
         return closure;
     }
@@ -693,12 +688,12 @@ public class CollectionUtils {
      * @param closure  the closure to perform, may be null
      * @return closure
      * @since 4.0
+     * @deprecated since 4.1, use {@link IteratorUtils#apply(Iterator, Closure)} instead
      */
+    @Deprecated
     public static <T, C extends Closure<? super T>> C forAllDo(final Iterator<T>
iterator, final C closure) {
-        if (iterator != null && closure != null) {
-            while (iterator.hasNext()) {
-                closure.execute(iterator.next());
-            }
+        if (closure != null) {
+            IteratorUtils.apply(iterator, closure);
         }
         return closure;
     }
@@ -1968,32 +1963,6 @@ public class CollectionUtils {
         return list;
     }
 
-    /**
-     * This method checks, if any of the elements in <code>collection</code>
is
-     * equal to <code>object</code>. Object equality is tested with an
-     * <code>equator</code> unlike <code>collection.contains(object)</code>
-     * which uses {@link Object#equals(Object)}.
-     *
-     * @param <E> the type of object the {@link Collection} contains
-     * @param collection the collection from which items are compared
-     * @param object the object to compare with the collection's entries
-     * @param equator the equator to use to check, if the item if equal to any
-     *        of the collection's entries.
-     * @return true if <code>object</code> is in <code>collection</code>
-     *         according to <code>equator</code>
-     * @throws NullPointerException if any parameter is null
-     * @since 4.1
-     */
-    public static <E> boolean contains(final Collection<? extends E> collection,
final E object,
-                                       final Equator<? super E> equator) {
-        for (final E obj : collection) {
-            if (equator.equate(obj, object)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     //-----------------------------------------------------------------------
     /**
      * Returns a synchronized collection backed by the given collection.

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=1683492&r1=1683491&r2=1683492&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
Thu Jun  4 08:52:55 2015
@@ -21,6 +21,7 @@ import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.collections4.functors.EqualPredicate;
 import org.apache.commons.collections4.iterators.LazyIteratorChain;
 import org.apache.commons.collections4.iterators.ReverseListIterator;
 import org.apache.commons.collections4.iterators.UniqueFilterIterator;
@@ -462,6 +463,21 @@ public class IterableUtils {
     }
 
     /**
+     * Finds the first element in the given iterable which matches the given predicate.
+     * <p>
+     * A <code>null</code> or empty iterator returns null.
+     *
+     * @param <E>  the element type
+     * @param iterable  the iterable to search, may be null
+     * @param predicate  the predicate to use, may not be null
+     * @return the first element of the iterable which matches the predicate or null if none
could be found
+     * @throws NullPointerException if predicate is null
+     */
+    public static <E> E find(final Iterable<E> iterable, final Predicate<?
super E> predicate) {
+        return IteratorUtils.find(emptyIteratorIfNull(iterable), predicate);
+    }
+
+    /**
      * Answers true if a predicate is true for every element of an iterable.
      * <p>
      * A <code>null</code> or empty iterable returns true.
@@ -553,6 +569,31 @@ public class IterableUtils {
     }
 
     /**
+     * Checks if the object is contained in the given iterable. Object equality
+     * is tested with an {@code equator} unlike {@link #contains(Iterable, Object)}
+     * which uses {@link Object#equals(Object)}.
+     * <p>
+     * A <code>null</code> or empty iterable returns false.
+     * A <code>null</code> object will not be passed to the equator, instead
a
+     * {@link org.apache.commons.collections4.functors.NullPredicate NullPredicate}
+     * will be used.
+     *
+     * @param <E>  the type of object the {@link Iterable} contains
+     * @param iterable  the iterable to check, may be null
+     * @param object  the object to check
+     * @param equator  the equator to use to check, may not be null
+     * @return true if the object is contained in the iterable, false otherwise
+     * @throws NullPointerException if equator is null
+     */
+    public static <E> boolean contains(final Iterable<? extends E> iterable,
final E object,
+                                       final Equator<? super E> equator) {
+        if (equator == null) {
+            throw new NullPointerException("Equator must not be null.");
+        }
+        return matchesAny(iterable, EqualPredicate.equalPredicate(object, equator));
+    }
+
+    /**
      * Returns the <code>index</code>-th value in the <code>iterable</code>'s
{@link Iterator}, throwing
      * <code>IndexOutOfBoundsException</code> if there is no such element.
      * <p>

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=1683492&r1=1683491&r2=1683492&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
Thu Jun  4 08:52:55 2015
@@ -1246,6 +1246,34 @@ public class IteratorUtils {
     }
 
     /**
+     * Finds the first element in the given iterator which matches the given predicate.
+     * <p>
+     * A <code>null</code> or empty iterator returns null.
+     *
+     * @param <E>  the element type
+     * @param iterator  the iterator to search, may be null
+     * @param predicate  the predicate to use, may not be null
+     * @return the first element of the iterator which matches the predicate or null if none
could be found
+     * @throws NullPointerException if predicate is null
+     * @since 4.1
+     */
+    public static <E> E find(final Iterator<E> iterator, final Predicate<?
super E> predicate) {
+        if (predicate == null) {
+            throw new NullPointerException("Predicate must not be null");
+        }
+
+        if (iterator != null) {
+            while (iterator.hasNext()) {
+                final E element = iterator.next();
+                if (predicate.evaluate(element)) {
+                    return element;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
      * Answers true if a predicate is true for any element of the iterator.
      * <p>
      * A <code>null</code> or empty iterator returns false.

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java?rev=1683492&r1=1683491&r2=1683492&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java
Thu Jun  4 08:52:55 2015
@@ -16,7 +16,7 @@
  */
 package org.apache.commons.collections4.functors;
 
-import static org.apache.commons.collections4.functors.NullPredicate.nullPredicate;
+import static org.apache.commons.collections4.functors.NullPredicate.*;
 
 import java.io.Serializable;
 
@@ -47,7 +47,6 @@ public final class EqualPredicate<T> imp
      * @param <T> the type that the predicate queries
      * @param object  the object to compare to
      * @return the predicate
-     * @throws IllegalArgumentException if the predicate is null
      */
     public static <T> Predicate<T> equalPredicate(final T object) {
         if (object == null) {
@@ -63,7 +62,6 @@ public final class EqualPredicate<T> imp
      * @param object  the object to compare to
      * @param equator  the equator to use for comparison
      * @return the predicate
-     * @throws IllegalArgumentException if the predicate is null
      * @since 4.0
      */
     public static <T> Predicate<T> equalPredicate(final T object, final Equator<T>
equator) {

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=1683492&r1=1683491&r2=1683492&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
Thu Jun  4 08:52:55 2015
@@ -16,13 +16,8 @@
  */
 package org.apache.commons.collections4;
 
-import static org.apache.commons.collections4.functors.EqualPredicate.equalPredicate;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.apache.commons.collections4.functors.EqualPredicate.*;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -612,6 +607,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test
+    @Deprecated
     public void find() {
         Predicate<Number> testPredicate = equalPredicate((Number) 4);
         Integer test = CollectionUtils.find(collectionA, testPredicate);
@@ -625,6 +621,7 @@ public class CollectionUtilsTest extends
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Test
+    @Deprecated
     public void forAllDoCollection() {
         final Closure<List<? extends Number>> testClosure = ClosureUtils.invokerClosure("clear");
         final Collection<List<? extends Number>> col = new ArrayList<List<?
extends Number>>();
@@ -645,6 +642,7 @@ public class CollectionUtilsTest extends
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Test
+    @Deprecated
     public void forAllDoIterator() {
         final Closure<List<? extends Number>> testClosure = ClosureUtils.invokerClosure("clear");
         final Collection<List<? extends Number>> col = new ArrayList<List<?
extends Number>>();
@@ -664,6 +662,7 @@ public class CollectionUtilsTest extends
     }
 
     @Test(expected = FunctorException.class)
+    @Deprecated
     public void forAllDoFailure() {
         final Closure<String> testClosure = ClosureUtils.invokerClosure("clear");
         final Collection<String> col = new ArrayList<String>();
@@ -1701,9 +1700,9 @@ public class CollectionUtilsTest extends
     @Test
     public void getIterator() {
         final Iterator<Integer> it = collectionA.iterator();
-        assertEquals(Integer.valueOf(2), CollectionUtils.get(it, 2));
+        assertEquals(Integer.valueOf(2), CollectionUtils.get((Object) it, 2));
         assertTrue(it.hasNext());
-        assertEquals(Integer.valueOf(4), CollectionUtils.get(it, 6));
+        assertEquals(Integer.valueOf(4), CollectionUtils.get((Object) it, 6));
         assertFalse(it.hasNext());
     }
 
@@ -1963,40 +1962,4 @@ public class CollectionUtilsTest extends
         } // this is what we want
     }
 
-    @Test
-    public void testContainsWithEquator() {
-        final List<String> base = new ArrayList<String>();
-        base.add("AC");
-        base.add("BB");
-        base.add("CA");
-
-        final Equator<String> secondLetterEquator = new Equator<String>() {
-
-            public boolean equate(String o1, String o2) {
-                return o1.charAt(1) == o2.charAt(1);
-            }
-
-            public int hash(String o) {
-                return o.charAt(1);
-            }
-
-        };
-
-        assertFalse(base.contains("CC"));
-        assertTrue(CollectionUtils.contains(base, "AC", secondLetterEquator));
-        assertTrue(CollectionUtils.contains(base, "CC", secondLetterEquator));
-        assertFalse(CollectionUtils.contains(base, "CX", secondLetterEquator));
-
-        try {
-            CollectionUtils.contains(null, null, secondLetterEquator);
-            fail("expecting NullPointerException");
-        } catch (final NullPointerException npe) {
-        } // this is what we want
-
-        try {
-            CollectionUtils.contains(base, "AC", null);
-            fail("expecting NullPointerException");
-        } catch (final NullPointerException npe) {
-        } // this is what we want
-      }
 }

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=1683492&r1=1683491&r2=1683492&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
Thu Jun  4 08:52:55 2015
@@ -16,10 +16,8 @@
  */
 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 static org.apache.commons.collections4.functors.EqualPredicate.*;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -56,18 +54,18 @@ public class IterableUtilsTest {
 
     @Before
     public void setUp() {
-        List<Integer> listA = new ArrayList<Integer>();
-        listA.add(1);
-        listA.add(2);
-        listA.add(2);
-        listA.add(3);
-        listA.add(3);
-        listA.add(3);
-        listA.add(4);
-        listA.add(4);
-        listA.add(4);
-        listA.add(4);
-        iterableA = listA;
+        Collection<Integer> 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;
 
         Collection<Long> collectionB = new LinkedList<Long>();
         collectionB.add(5L);
@@ -93,6 +91,91 @@ public class IterableUtilsTest {
 
     // -----------------------------------------------------------------------
     @Test
+    public void apply() {
+        final List<Integer> listA = new ArrayList<Integer>();
+        listA.add(1);
+
+        final List<Integer> listB = new ArrayList<Integer>();
+        listB.add(2);
+
+        final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear");
+        final Collection<List<Integer>> col = new ArrayList<List<Integer>>();
+        col.add(listA);
+        col.add(listB);
+        IterableUtils.apply(col, testClosure);
+        assertTrue(listA.isEmpty() && listB.isEmpty());
+        try {
+            IterableUtils.apply(col, null);
+            fail("expecting NullPointerException");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        IterableUtils.apply(null, testClosure);
+
+        // null should be OK
+        col.add(null);
+        IterableUtils.apply(col, testClosure);
+    }
+
+    @Test(expected = FunctorException.class)
+    public void applyFailure() {
+        final Closure<String> testClosure = ClosureUtils.invokerClosure("clear");
+        final Collection<String> col = new ArrayList<String>();
+        col.add("x");
+        IterableUtils.apply(col, testClosure);
+    }
+
+    @Test
+    public void containsWithEquator() {
+        final List<String> base = new ArrayList<String>();
+        base.add("AC");
+        base.add("BB");
+        base.add("CA");
+
+        final Equator<String> secondLetterEquator = new Equator<String>() {
+
+            public boolean equate(String o1, String o2) {
+                return o1.charAt(1) == o2.charAt(1);
+            }
+
+            public int hash(String o) {
+                return o.charAt(1);
+            }
+
+        };
+
+        assertFalse(base.contains("CC"));
+        assertTrue(IterableUtils.contains(base, "AC", secondLetterEquator));
+        assertTrue(IterableUtils.contains(base, "CC", secondLetterEquator));
+        assertFalse(IterableUtils.contains(base, "CX", secondLetterEquator));
+        assertFalse(IterableUtils.contains(null, null, secondLetterEquator));
+
+        try {
+            IterableUtils.contains(base, "AC", null);
+            fail("expecting NullPointerException");
+        } catch (final NullPointerException npe) {
+        } // this is what we want
+    }
+
+    @Test
+    public void find() {
+        Predicate<Number> testPredicate = equalPredicate((Number) 4);
+        Integer test = IterableUtils.find(iterableA, testPredicate);
+        assertTrue(test.equals(4));
+        testPredicate = equalPredicate((Number) 45);
+        test = IterableUtils.find(iterableA, testPredicate);
+        assertTrue(test == null);
+        assertNull(IterableUtils.find(null,testPredicate));
+        try {
+            assertNull(IterableUtils.find(iterableA, null));
+            fail("expecting NullPointerException");
+        } catch (final NullPointerException npe) {
+            // expected
+        }
+    }
+
+    @Test
     public void frequency() {
         assertEquals(4, IterableUtils.frequency(iterableB, EQUALS_TWO));
         assertEquals(0, IterableUtils.frequency(null, EQUALS_TWO));

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=1683492&r1=1683491&r2=1683492&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
Thu Jun  4 08:52:55 2015
@@ -16,14 +16,13 @@
  */
 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 static org.apache.commons.collections4.functors.EqualPredicate.*;
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
@@ -39,10 +38,6 @@ import org.junit.Test;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-
 /**
  * Tests for IteratorUtils.
  *
@@ -905,6 +900,52 @@ public class IteratorUtilsTest {
         };
     }
 
+    // -----------------------------------------------------------------------
+    @Test
+    public void apply() {
+        final List<Integer> listA = new ArrayList<Integer>();
+        listA.add(1);
+
+        final List<Integer> listB = new ArrayList<Integer>();
+        listB.add(2);
+
+        final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear");
+        final Collection<List<Integer>> col = new ArrayList<List<Integer>>();
+        col.add(listA);
+        col.add(listB);
+        IteratorUtils.apply(col.iterator(), testClosure);
+        assertTrue(listA.isEmpty() && listB.isEmpty());
+        try {
+            IteratorUtils.apply(col.iterator(), null);
+            fail("expecting NullPointerException");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        IteratorUtils.apply(null, testClosure);
+
+        // null should be OK
+        col.add(null);
+        IteratorUtils.apply(col.iterator(), testClosure);
+    }
+
+    @Test
+    public void find() {
+        Predicate<Number> testPredicate = equalPredicate((Number) 4);
+        Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate);
+        assertTrue(test.equals(4));
+        testPredicate = equalPredicate((Number) 45);
+        test = IteratorUtils.find(iterableA.iterator(), testPredicate);
+        assertTrue(test == null);
+        assertNull(IteratorUtils.find(null,testPredicate));
+        try {
+            assertNull(IteratorUtils.find(iterableA.iterator(), null));
+            fail("expecting NullPointerException");
+        } catch (final NullPointerException npe) {
+            // expected
+        }
+    }
+
     @Test
     public void getFromIterator() throws Exception {
         // Iterator, entry exists



Mime
View raw message