Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 554FB93EA for ; Sun, 15 Jan 2012 19:24:07 +0000 (UTC) Received: (qmail 61909 invoked by uid 500); 15 Jan 2012 19:24:06 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 61708 invoked by uid 500); 15 Jan 2012 19:24:06 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 61700 invoked by uid 99); 15 Jan 2012 19:24:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 15 Jan 2012 19:24:05 +0000 X-ASF-Spam-Status: No, hits=2.2 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [209.85.214.171] (HELO mail-tul01m020-f171.google.com) (209.85.214.171) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 15 Jan 2012 19:24:01 +0000 Received: by obbwc7 with SMTP id wc7so9637411obb.30 for ; Sun, 15 Jan 2012 11:23:40 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.76.134 with SMTP id k6mr8436133obw.10.1326655420520; Sun, 15 Jan 2012 11:23:40 -0800 (PST) Received: by 10.182.21.135 with HTTP; Sun, 15 Jan 2012 11:23:40 -0800 (PST) Received: by 10.182.21.135 with HTTP; Sun, 15 Jan 2012 11:23:40 -0800 (PST) In-Reply-To: <4F12BF7B.3010809@systemoutprintln.de> References: <4F12BF7B.3010809@systemoutprintln.de> Date: Sun, 15 Jan 2012 11:23:40 -0800 Message-ID: Subject: Re: [collections] suggestion for improvement: QueryableCollections From: James Ring To: Commons Developers List Content-Type: multipart/alternative; boundary=f46d044787a5145a3804b69609f0 --f46d044787a5145a3804b69609f0 Content-Type: text/plain; charset=ISO-8859-1 Google Guava has all this and more, and it doesn't require an extension to ArrayList, any Iterable will do. http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html On Jan 15, 2012 3:59 AM, "Benedikt Ritter" wrote: > Hi, > > I have written a small extension for java.util.ArrayList, that allows for > qerrying Lists using (generic) predicates. In addition to that, I adapted > org.springframework.data.jpa.**domain.Specifications class as a util to > link predicates to queries. Here is how it works: > > QueryableList qList = new QueryableArrayList()**; > qList.addAll(getCustomers()); > qList.getAll(new Predicate() { > > 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 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 predicate); > public Iterator matchIterator(Predicate predicate); > public boolean retainAllMatches(Predicate predicate); > public boolean removeAllMatches(Predicate predicate); > > ...and common list methods: > > public int indexOfMatch(Predicate predicate); > public int lastIndexOfMatch(Predicate 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 > contribution: > - 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 statements. > - 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: > HEAD: https://github.com/britter/**QueryableCollections > v0.1.0: https://github.com/britter/**QueryableCollections/tree/** > stable-0.1.0 > You can download a build from my blog: > http://www.systemoutprintln.**de/wp-content/uploads/** > collections-0.1.0.jar > > I'm really exited to hear what you guys think of QueryableCollections. > Regards > Benedikt Ritter > > ------------------------------**------------------------------**--------- > To unsubscribe, e-mail: dev-unsubscribe@commons.**apache.org > For additional commands, e-mail: dev-help@commons.apache.org > > --f46d044787a5145a3804b69609f0--