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/08 21:38:26 UTC
svn commit: r1684252 - in /commons/proper/collections/trunk/src:
main/java/org/apache/commons/collections4/
test/java/org/apache/commons/collections4/
Author: tn
Date: Mon Jun 8 19:38:26 2015
New Revision: 1684252
URL: http://svn.apache.org/r1684252
Log:
Complete unit tests for FluentIterable, fix exception for toArray.
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.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/FluentIterableTest.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java?rev=1684252&r1=1684251&r2=1684252&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java Mon Jun 8 19:38:26 2015
@@ -195,7 +195,7 @@ public class FluentIterable<E> implement
* @see {@link org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
*/
public FluentIterable<E> collate(final Iterable<? extends E> other) {
- return of(IterableUtils.collatedIterable(null, iterable, other));
+ return of(IterableUtils.collatedIterable(iterable, other));
}
/**
@@ -361,6 +361,7 @@ public class FluentIterable<E> implement
// ----------------------------------------------------------------------
/** {@inheritDoc} */
+ @Override
public Iterator<E> iterator() {
return iterable.iterator();
}
@@ -477,7 +478,7 @@ public class FluentIterable<E> implement
*
* @param arrayClass the class of array to create
* @return an array of the iterable contents
- * @throws ClassCastException if arrayClass is invalid
+ * @throws ArrayStoreException if arrayClass is invalid
*/
public E[] toArray(final Class<E> arrayClass) {
return IteratorUtils.toArray(iterator(), arrayClass);
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=1684252&r1=1684251&r2=1684252&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 8 19:38:26 2015
@@ -1098,7 +1098,7 @@ public class IteratorUtils {
* @param arrayClass the class of array to create
* @return an array of the iterator contents
* @throws NullPointerException if iterator parameter or arrayClass is null
- * @throws ClassCastException if the arrayClass is invalid
+ * @throws ArrayStoreException if the arrayClass is invalid
*/
public static <E> E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass) {
if (iterator == null) {
@@ -1108,7 +1108,7 @@ public class IteratorUtils {
throw new NullPointerException("Array class must not be null");
}
final List<E> list = toList(iterator, 100);
- @SuppressWarnings("unchecked") // as per Javadoc, will throw CCE if class is wrong
+ @SuppressWarnings("unchecked")
final E[] array = (E[]) Array.newInstance(arrayClass, list.size());
return list.toArray(array);
}
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java?rev=1684252&r1=1684251&r2=1684252&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java Mon Jun 8 19:38:26 2015
@@ -16,15 +16,23 @@
*/
package org.apache.commons.collections4;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+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.Collection;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Test;
@@ -97,6 +105,7 @@ public class FluentIterableTest {
}
private static Predicate<Number> EVEN = new Predicate<Number>() {
+ @Override
public boolean evaluate(final Number input) {
return input.intValue() % 2 == 0;
}
@@ -170,6 +179,25 @@ public class FluentIterableTest {
}
@Test
+ public void collateWithComparator() {
+ List<Integer> result =
+ FluentIterable
+ .of(iterableOdd)
+ .collate(iterableEven, ComparatorUtils.<Integer>naturalComparator())
+ .toList();
+
+ List<Integer> combinedList = new ArrayList<Integer>();
+ CollectionUtils.addAll(combinedList, iterableOdd);
+ CollectionUtils.addAll(combinedList, iterableEven);
+ Collections.sort(combinedList);
+ assertEquals(combinedList, result);
+
+ // null comparator is equivalent to natural ordering
+ result = FluentIterable.of(iterableOdd).collate(iterableEven, null).toList();
+ assertEquals(combinedList, result);
+ }
+
+ @Test
public void filter() {
Predicate<Integer> smallerThan3 = new Predicate<Integer>() {
@Override
@@ -194,6 +222,31 @@ public class FluentIterableTest {
}
@Test
+ public void forEach() {
+ final AtomicInteger sum = new AtomicInteger(0);
+ Closure<Integer> closure = new Closure<Integer>() {
+ @Override
+ public void execute(Integer input) {
+ sum.addAndGet(input);
+ }
+ };
+
+ FluentIterable.of(iterableA).forEach(closure);
+ int expectedSum = 0;
+ for (Integer i : iterableA) {
+ expectedSum += i;
+ }
+ assertEquals(expectedSum, sum.get());
+
+ try {
+ FluentIterable.of(iterableA).forEach(null);
+ fail("expecting NullPointerException");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+ }
+
+ @Test
public void limit() {
List<Integer> result = FluentIterable.of(iterableA).limit(3).toList();
assertEquals(3, result.size());
@@ -313,6 +366,7 @@ public class FluentIterableTest {
assertSame(iterable1, iterable2);
}
+ @SuppressWarnings("unchecked")
@Test
public void zip() {
List<Integer> result = FluentIterable.of(iterableOdd).zip(iterableEven).toList();
@@ -329,12 +383,25 @@ public class FluentIterableTest {
// expected
}
- result = FluentIterable.of(Arrays.asList(1, 4, 7)).zip(Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9)).toList();
+ result = FluentIterable
+ .of(Arrays.asList(1, 4, 7))
+ .zip(Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9))
+ .toList();
combinedList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
assertEquals(combinedList, result);
}
@Test
+ public void asEnumeration() {
+ Enumeration<Long> enumeration = FluentIterable.of(iterableB).asEnumeration();
+ List<Long> result = EnumerationUtils.toList(enumeration);
+ assertEquals(iterableB, result);
+
+ enumeration = FluentIterable.<Long>empty().asEnumeration();
+ assertFalse(enumeration.hasMoreElements());
+ }
+
+ @Test
public void allMatch() {
assertTrue(FluentIterable.of(iterableEven).allMatch(EVEN));
assertFalse(FluentIterable.of(iterableOdd).allMatch(EVEN));
@@ -381,6 +448,21 @@ public class FluentIterableTest {
}
@Test
+ public void eval() {
+ List<Integer> listNumbers = new ArrayList<Integer>();
+ listNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
+ FluentIterable<Integer> iterable = FluentIterable.of(listNumbers).filter(EVEN);
+ FluentIterable<Integer> materialized = iterable.eval();
+
+ listNumbers.addAll(Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20));
+ assertEquals(5, materialized.size());
+ assertEquals(10, iterable.size());
+
+ assertEquals(Arrays.asList(2, 4, 6, 8, 10), materialized.toList());
+ assertEquals(Arrays.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20), iterable.toList());
+ }
+
+ @Test
public void contains() {
assertTrue(FluentIterable.of(iterableEven).contains(2));
assertFalse(FluentIterable.of(iterableEven).contains(1));
@@ -418,6 +500,15 @@ public class FluentIterableTest {
}
@Test
+ public void iterator() {
+ Iterator<Integer> iterator = FluentIterable.of(iterableA).iterator();
+ assertTrue(iterator.hasNext());
+
+ iterator = FluentIterable.<Integer>empty().iterator();
+ assertFalse(iterator.hasNext());
+ }
+
+ @Test
public void get() {
assertEquals(2, FluentIterable.of(iterableEven).get(0).intValue());
@@ -435,4 +526,28 @@ public class FluentIterableTest {
// expected
}
}
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Test
+ public void toArray() {
+ Long[] arr = new Long[] {1L, 2L, 3L, 4L, 5L};
+ Long[] result = FluentIterable.of(arr).toArray(Long.class);
+ assertNotNull(result);
+ assertArrayEquals(arr, result);
+
+ try {
+ FluentIterable.of(arr).toArray((Class) String.class);
+ } catch (ArrayStoreException ase) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testToString() {
+ String result = FluentIterable.of(iterableA).toString();
+ assertEquals(iterableA.toString(), result);
+
+ result = FluentIterable.empty().toString();
+ assertEquals("[]", result);
+ }
}
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=1684252&r1=1684251&r2=1684252&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 8 19:38:26 2015
@@ -16,8 +16,13 @@
*/
package org.apache.commons.collections4;
-import static org.apache.commons.collections4.functors.EqualPredicate.*;
-import static org.junit.Assert.*;
+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 java.util.ArrayList;
import java.util.Arrays;
@@ -88,12 +93,14 @@ public class IterableUtilsTest {
}
private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() {
+ @Override
public boolean evaluate(final Number input) {
return input.intValue() == 2;
}
};
private static Predicate<Number> EVEN = new Predicate<Number>() {
+ @Override
public boolean evaluate(final Number input) {
return input.intValue() % 2 == 0;
}
@@ -177,10 +184,12 @@ public class IterableUtilsTest {
final Equator<String> secondLetterEquator = new Equator<String>() {
+ @Override
public boolean equate(String o1, String o2) {
return o1.charAt(1) == o2.charAt(1);
}
+ @Override
public int hash(String o) {
return o.charAt(1);
}
@@ -349,6 +358,7 @@ public class IterableUtilsTest {
}
Predicate<Integer> lessThanFive = new Predicate<Integer>() {
+ @Override
public boolean evaluate(Integer object) {
return object < 5;
}
@@ -356,6 +366,7 @@ public class IterableUtilsTest {
assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive));
Predicate<Integer> lessThanFour = new Predicate<Integer>() {
+ @Override
public boolean evaluate(Integer object) {
return object < 4;
}
@@ -446,7 +457,7 @@ public class IterableUtilsTest {
// expected
}
}
-
+
@Test
public void testToString() {
String result = IterableUtils.toString(iterableA);
@@ -459,6 +470,7 @@ public class IterableUtilsTest {
assertEquals("[]", result);
result = IterableUtils.toString(iterableA, new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
return new Integer(input * 2).toString();
}
@@ -466,6 +478,7 @@ public class IterableUtilsTest {
assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result);
result = IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
fail("not supposed to reach here");
return "";
@@ -474,6 +487,7 @@ public class IterableUtilsTest {
assertEquals("[]", result);
result = IterableUtils.toString(null, new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
fail("not supposed to reach here");
return "";
@@ -486,6 +500,7 @@ public class IterableUtilsTest {
public void testToStringDelimiter() {
Transformer<Integer, String> transformer = new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
return new Integer(input * 2).toString();
}
@@ -522,6 +537,7 @@ public class IterableUtilsTest {
@Test
public void testToStringWithNullArguments() {
String result = IterableUtils.toString(null, new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
fail("not supposed to reach here");
return "";
@@ -538,6 +554,7 @@ public class IterableUtilsTest {
try {
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
fail("not supposed to reach here");
return "";
@@ -550,6 +567,7 @@ public class IterableUtilsTest {
try {
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
fail("not supposed to reach here");
return "";
@@ -562,6 +580,7 @@ public class IterableUtilsTest {
try {
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ @Override
public String transform(Integer input) {
fail("not supposed to reach here");
return "";