commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benedikt Ritter <>
Subject [collections] suggestion for improvement: QueryableCollections
Date Sun, 15 Jan 2012 11:58:51 GMT

I have written a small extension for java.util.ArrayList, that allows 
for qerrying Lists using (generic) predicates. In addition to that, I 
adapted class as a 
util to link predicates to queries. Here is how it works:

QueryableList<Customer> qList = new QueryableArrayList<Customer>();
qList.getAll(new Predicate<Customer>() {

     public boolean evaluate(Customer element) {
         if (element.getLastName().startsWith("B")) {
             return true;
         } else {
             return false;

This will give you a List containing all customers, that match the given 
predicate. Using the Query class, we can link predicates via AND and OR 
or simply neglect them (the next example assumes, that we have a static 
import of the not and where method and startsWith(String) and 
bornAfter(int) are util methods, that return predicates):

List<Customer> queryResult = qList.getAll(not(startsWith("B")));
queryResult = qList.getAll(where(startsWith("B")).or(startsWith("C")));
queryResult = qList.getAll(where(startsWith("B")).and(bornAfter(1980)));

In addition to that, there are implementations of common collection 
methods using predicates:

public boolean containsMatch(Predicate<E> predicate);
public Iterator<E> matchIterator(Predicate<E> predicate);
public boolean retainAllMatches(Predicate<E> predicate);
public boolean removeAllMatches(Predicate<E> predicate);

...and common list methods:

public int indexOfMatch(Predicate<E> predicate);
public int lastIndexOfMatch(Predicate<E> predicate);

I think QueryableCollections would fit nicely into commons collections, 
because as far as I know, commons collections only offers you the 
opportunity to validate if all elements in a collection match a given 
predicate. There is no possibility to easily query for objects matching 
some criteria.
Having that said, I would like to contribute all source code of 
QueryableCollections. I am willing to make what ever changes are required.

Here are some thinks that I think will need to be adjusted before 
- swtich from my generic Predicate implementation to 
org.apache.commons.collections.Predicate, although I really would like 
to see generics in commons collections. It saves you all the instanceof 
- As far as I can see commons collections does not extend classes from 
java.util.* but decorates them. As I said QueryableArrayList is an 
extension of ArrayList. If there is a general policy of not extending 
java base classes, this would have to be changed.
- Re-Implement jUnit tests using jUnit 3.8.1 (instead of jUnit 4.1.0)
- change licence agreement von LGPL to Apache License

All source code (and example code) is available at github:
You can download a build from my blog:

I'm really exited to hear what you guys think of QueryableCollections.
Benedikt Ritter

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message