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

View File

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