Avoid generic return parameter, specify the actual interface being used to avoid compilation problems.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1648910 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-01-01 17:26:55 +00:00
parent 67c51ed7ef
commit 9acc3e824e
2 changed files with 16 additions and 16 deletions

View File

@ -995,9 +995,9 @@ public class CollectionUtils {
* Partitions all elements from inputCollection into separate output collections, * Partitions all elements from inputCollection into separate output collections,
* based on the evaluation of the given predicate. * based on the evaluation of the given predicate.
* <p> * <p>
* For each predicate, the returned list will contain a collection holding * For each predicate, the result will contain a list holding all elements of the
* all elements of the input collection matching the predicate. The last collection * input collection matching the predicate. The last list will hold all elements
* contained in the list will hold all elements which didn't match any predicate: * which didn't match any predicate:
* <pre> * <pre>
* [C1, R] = partition(I, P1) with * [C1, R] = partition(I, P1) with
* I = input collection * I = input collection
@ -1014,17 +1014,16 @@ public class CollectionUtils {
* will result in the following output: [[1, 2], [3, 4, 5]]. * will result in the following output: [[1, 2], [3, 4, 5]].
* *
* @param <O> the type of object the {@link Iterable} contains * @param <O> the type of object the {@link Iterable} contains
* @param <R> the type of the output {@link Collection}
* @param inputCollection the collection to get the input from, may be null * @param inputCollection the collection to get the input from, may be null
* @param predicate the predicate to use, may be null * @param predicate the predicate to use, may be null
* @return a list containing the output collections * @return a list containing the output collections
* @since 4.1 * @since 4.1
*/ */
public static <O, R extends Collection<O>> List<R> partition(final Iterable<? extends O> inputCollection, public static <O> List<List<O>> partition(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate) { final Predicate<? super O> predicate) {
@SuppressWarnings("unchecked") // safe @SuppressWarnings({ "unchecked", "rawtypes" }) // safe
final Factory<R> factory = (Factory<R>) FactoryUtils.instantiateFactory(ArrayList.class); final Factory<List<O>> factory = FactoryUtils.instantiateFactory((Class) ArrayList.class);
@SuppressWarnings("unchecked") // safe @SuppressWarnings("unchecked") // safe
final Predicate<? super O>[] predicates = new Predicate[] { predicate }; final Predicate<? super O>[] predicates = new Predicate[] { predicate };
return partition(inputCollection, factory, predicates); return partition(inputCollection, factory, predicates);
@ -1074,9 +1073,9 @@ public class CollectionUtils {
* Partitions all elements from inputCollection into separate output collections, * Partitions all elements from inputCollection into separate output collections,
* based on the evaluation of the given predicates. * based on the evaluation of the given predicates.
* <p> * <p>
* For each predicate, the returned list will contain a collection holding * For each predicate, the result will contain a list holding all elements of the
* all elements of the input collection matching the predicate. The last collection * input collection matching the predicate. The last list will hold all elements
* contained in the list will hold all elements which didn't match any predicate: * which didn't match any predicate:
* <pre> * <pre>
* [C1, C2, R] = partition(I, P1, P2) with * [C1, C2, R] = partition(I, P1, P2) with
* I = input collection * I = input collection
@ -1098,17 +1097,16 @@ public class CollectionUtils {
* and [x &lt; 5] will result in the following output: [[1, 2], [3, 4], [5]]. * and [x &lt; 5] will result in the following output: [[1, 2], [3, 4], [5]].
* *
* @param <O> the type of object the {@link Iterable} contains * @param <O> the type of object the {@link Iterable} contains
* @param <R> the type of the output {@link Collection}
* @param inputCollection the collection to get the input from, may be null * @param inputCollection the collection to get the input from, may be null
* @param predicates the predicates to use, may be null * @param predicates the predicates to use, may be null
* @return a list containing the output collections * @return a list containing the output collections
* @since 4.1 * @since 4.1
*/ */
public static <O, R extends Collection<O>> List<R> partition(final Iterable<? extends O> inputCollection, public static <O> List<List<O>> partition(final Iterable<? extends O> inputCollection,
final Predicate<? super O>... predicates) { final Predicate<? super O>... predicates) {
@SuppressWarnings("unchecked") // safe @SuppressWarnings({ "unchecked", "rawtypes" }) // safe
final Factory<R> factory = (Factory<R>) FactoryUtils.instantiateFactory(ArrayList.class); final Factory<List<O>> factory = FactoryUtils.instantiateFactory((Class) ArrayList.class);
return partition(inputCollection, factory, predicates); return partition(inputCollection, factory, predicates);
} }

View File

@ -1185,6 +1185,7 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(output1.contains(4L)); assertTrue(output1.contains(4L));
} }
@SuppressWarnings("unchecked")
@Test @Test
public void partition() { public void partition() {
List<Integer> input = new ArrayList<Integer>(); List<Integer> input = new ArrayList<Integer>();
@ -1192,7 +1193,7 @@ public class CollectionUtilsTest extends MockTestCase {
input.add(2); input.add(2);
input.add(3); input.add(3);
input.add(4); input.add(4);
List<Collection<Integer>> partitions = CollectionUtils.partition(input, EQUALS_TWO); List<List<Integer>> partitions = CollectionUtils.partition(input, EQUALS_TWO);
assertEquals(2, partitions.size()); assertEquals(2, partitions.size());
// first partition contains 2 // first partition contains 2
@ -1248,7 +1249,8 @@ public class CollectionUtilsTest extends MockTestCase {
input.add(2); input.add(2);
input.add(3); input.add(3);
input.add(4); input.add(4);
List<Collection<Integer>> partitions = CollectionUtils.partition(input, EQUALS_TWO, EVEN); @SuppressWarnings("unchecked")
List<List<Integer>> partitions = CollectionUtils.partition(input, EQUALS_TWO, EVEN);
// first partition contains 2 // first partition contains 2
Collection<Integer> partition = partitions.get(0); Collection<Integer> partition = partitions.get(0);