I'm writing a method which filters a specific Collection with a "Predicate" and returns a new Collection containing only filtered elements (those for whom Predicate returns true).
Something like this :
public <T> Collection<T> filter(Collection<T> collection, Closure<T> predicate);
I know that, in Java, I can't just create a new Collection()
at runtime, because of type-erasure.
I also know the "work-around" by passing an extra-argument to the method to call T.newInstance().
This would look like :
public <T> Collection<T> filter(Class<? extends Collection<T>> collectionToInstanciate, Collection<T> collection, Closure<T> predicate) {
// create the new Collection
Collection<T> container = collectionToInstanciate.newInstance();
// and then add only filtered items
Iterator<T> iter = collection.iterator();
while (iter.hasNext()) {
T obj = iter.next();
// if Predicate.invoke() returns true, then keep element, otherwise skip it
if (predicate.invoke(obj)) {
container.add(obj);
}
}
return container;
}
But how should I call my method ?
For instance, if I want only odd numbers of a List of Integers, I'd like to do :
// instanciate ArrayList<Integer> = [1, 2, 3, 4, 5]
ArrayList<Integer> array = ...;
// return a new LinkedList<Integer> with only odd numbers
filter(LinkedList<Integer>.class, array, new Closure<Integer>() {
public Boolean invoke(Integer arg_p) {
return (arg_p % 2 == 0);
}
});
// should return [2, 4] as a LinkedList<Integer>
The problem is that
LinkedList<Integer>.class
doesn't compile.
How should I declare that to correctly instanciate a LinkedList in the filter() method ?
Regards,