You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/06/22 21:48:48 UTC
svn commit: r1686921 - in /commons/proper/collections/trunk/src:
main/java/org/apache/commons/collections4/
test/java/org/apache/commons/collections4/
Author: tn
Date: Mon Jun 22 19:48:47 2015
New Revision: 1686921
URL: http://svn.apache.org/r1686921
Log:
Add indexOf methods to IterableUtils and IteratorUtils, replace last occurrence of ArrayStack with ArrayDeque.
Modified:
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/MapUtils.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/IterableUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java?rev=1686921&r1=1686920&r2=1686921&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 22 19:48:47 2015
@@ -613,6 +613,22 @@ public class IterableUtils {
}
/**
+ * Returns the index of the first element in the specified iterable that
+ * matches the given predicate.
+ * <p>
+ * A <code>null</code> or empty iterable returns -1.
+ *
+ * @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 index of the first element which matches the predicate or -1 if none matches
+ * @throws NullPointerException if predicate is null
+ */
+ public static <E> int indexOf(final Iterable<E> iterable, final Predicate<? super E> predicate) {
+ return IteratorUtils.indexOf(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.
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=1686921&r1=1686920&r2=1686921&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 22 19:48:47 2015
@@ -1282,31 +1282,48 @@ public class IteratorUtils {
}
/**
- * Answers true if a predicate is true for any element of the iterator.
+ * Returns the index of the first element in the specified iterator that
+ * matches the given predicate.
* <p>
- * A <code>null</code> or empty iterator returns false.
+ * A <code>null</code> or empty iterator returns -1.
*
- * @param <E> the type of object the {@link Iterator} contains
- * @param iterator the {@link Iterator} to use, may be 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 true if any element of the collection matches the predicate, false otherwise
+ * @return the index of the first element which matches the predicate or -1 if none matches
* @throws NullPointerException if predicate is null
* @since 4.1
*/
- public static <E> boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate) {
+ public static <E> int indexOf(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()) {
+ for(int index = 0; iterator.hasNext(); index++) {
final E element = iterator.next();
if (predicate.evaluate(element)) {
- return true;
+ return index;
}
}
}
- return false;
+ return -1;
+ }
+
+ /**
+ * Answers true if a predicate is true for any element of the iterator.
+ * <p>
+ * A <code>null</code> or empty iterator returns false.
+ *
+ * @param <E> the type of object the {@link Iterator} contains
+ * @param iterator the {@link Iterator} to use, may be null
+ * @param predicate the predicate to use, may not be null
+ * @return true if any element of the collection matches the predicate, false otherwise
+ * @throws NullPointerException if predicate is null
+ * @since 4.1
+ */
+ public static <E> boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate) {
+ return indexOf(iterator, predicate) != -1;
}
/**
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java?rev=1686921&r1=1686920&r2=1686921&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java Mon Jun 22 19:48:47 2015
@@ -19,8 +19,10 @@ package org.apache.commons.collections4;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.text.ParseException;
+import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
+import java.util.Deque;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
@@ -918,7 +920,7 @@ public class MapUtils {
* @throws NullPointerException if the stream is <code>null</code>
*/
public static void verbosePrint(final PrintStream out, final Object label, final Map<?, ?> map) {
- verbosePrintInternal(out, label, map, new ArrayStack<Map<?, ?>>(), false);
+ verbosePrintInternal(out, label, map, new ArrayDeque<Map<?, ?>>(), false);
}
/**
@@ -940,7 +942,7 @@ public class MapUtils {
* @throws NullPointerException if the stream is <code>null</code>
*/
public static void debugPrint(final PrintStream out, final Object label, final Map<?, ?> map) {
- verbosePrintInternal(out, label, map, new ArrayStack<Map<?, ?>>(), true);
+ verbosePrintInternal(out, label, map, new ArrayDeque<Map<?, ?>>(), true);
}
// Implementation methods
@@ -969,7 +971,7 @@ public class MapUtils {
* @throws NullPointerException if the stream is <code>null</code>
*/
private static void verbosePrintInternal(final PrintStream out, final Object label, final Map<?, ?> map,
- final ArrayStack<Map<?, ?>> lineage, final boolean debug) {
+ final Deque<Map<?, ?>> lineage, final boolean debug) {
printIndent(out, lineage.size());
if (map == null) {
@@ -988,7 +990,7 @@ public class MapUtils {
printIndent(out, lineage.size());
out.println("{");
- lineage.push(map);
+ lineage.addLast(map);
for (final Map.Entry<?, ?> entry : map.entrySet()) {
final Object childKey = entry.getKey();
@@ -1005,7 +1007,9 @@ public class MapUtils {
out.print(childKey);
out.print(" = ");
- final int lineageIndex = lineage.indexOf(childValue);
+ final int lineageIndex =
+ IterableUtils.indexOf(lineage,
+ PredicateUtils.equalPredicate(childValue));
if (lineageIndex == -1) {
out.print(childValue);
} else if (lineage.size() - 1 == lineageIndex) {
@@ -1026,7 +1030,7 @@ public class MapUtils {
}
}
- lineage.pop();
+ lineage.removeLast();
printIndent(out, lineage.size());
out.println(debug ? "} " + map.getClass().getName() : "}");
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=1686921&r1=1686920&r2=1686921&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 22 19:48:47 2015
@@ -293,6 +293,23 @@ public class IterableUtilsTest {
}
@Test
+ public void indexOf() {
+ Predicate<Number> testPredicate = equalPredicate((Number) 4);
+ int index = IterableUtils.indexOf(iterableA, testPredicate);
+ assertEquals(6, index);
+ testPredicate = equalPredicate((Number) 45);
+ index = IterableUtils.indexOf(iterableA, testPredicate);
+ assertEquals(-1, index);
+ assertEquals(-1, IterableUtils.indexOf(null, testPredicate));
+ try {
+ assertNull(IterableUtils.indexOf(iterableA, null));
+ fail("expecting NullPointerException");
+ } catch (final NullPointerException npe) {
+ // expected
+ }
+ }
+
+ @Test
public void countMatches() {
assertEquals(4, IterableUtils.countMatches(iterableB, EQUALS_TWO));
assertEquals(0, IterableUtils.countMatches(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=1686921&r1=1686920&r2=1686921&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 22 19:48:47 2015
@@ -16,9 +16,16 @@
*/
package org.apache.commons.collections4;
-import static org.apache.commons.collections4.functors.EqualPredicate.*;
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.*;
+import static org.apache.commons.collections4.functors.EqualPredicate.equalPredicate;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+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 java.util.ArrayList;
import java.util.Arrays;
@@ -64,7 +71,7 @@ public class IteratorUtilsTest {
private Iterable<Integer> iterableA = null;
- private Collection<Integer> emptyCollection = new ArrayList<Integer>(1);
+ private final Collection<Integer> emptyCollection = new ArrayList<Integer>(1);
@Before
public void setUp() {
@@ -908,9 +915,11 @@ public class IteratorUtilsTest {
*/
private NodeList createNodeList(final Node[] nodes) {
return new NodeList() {
+ @Override
public Node item(final int index) {
return nodes[index];
}
+ @Override
public int getLength() {
return nodes.length;
}
@@ -957,8 +966,8 @@ public class IteratorUtilsTest {
final Comparator<Integer> reverseComparator =
ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator());
- Collections.reverse((List<Integer>) collectionOdd);
- Collections.reverse((List<Integer>) collectionEven);
+ Collections.reverse(collectionOdd);
+ Collections.reverse(collectionEven);
Collections.reverse(combinedList);
it = IteratorUtils.collatedIterator(reverseComparator,
@@ -1043,6 +1052,23 @@ public class IteratorUtilsTest {
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
+ }
+ }
+
+ @Test
+ public void indexOf() {
+ Predicate<Number> testPredicate = equalPredicate((Number) 4);
+ int index = IteratorUtils.indexOf(iterableA.iterator(), testPredicate);
+ assertEquals(6, index);
+ testPredicate = equalPredicate((Number) 45);
+ index = IteratorUtils.indexOf(iterableA.iterator(), testPredicate);
+ assertEquals(-1, index);
+ assertEquals(-1, IteratorUtils.indexOf(null, testPredicate));
+ try {
+ assertNull(IteratorUtils.indexOf(iterableA.iterator(), null));
+ fail("expecting NullPointerException");
+ } catch (final NullPointerException npe) {
+ // expected
}
}