Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 45591 invoked from network); 6 Nov 2006 05:25:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 6 Nov 2006 05:25:03 -0000 Received: (qmail 75685 invoked by uid 500); 6 Nov 2006 05:25:10 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 75611 invoked by uid 500); 6 Nov 2006 05:25:10 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 75600 invoked by uid 99); 6 Nov 2006 05:25:10 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 05 Nov 2006 21:25:10 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO brutus.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 05 Nov 2006 21:24:58 -0800 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 3A0387142FA for ; Sun, 5 Nov 2006 21:24:38 -0800 (PST) Message-ID: <4260232.1162790678235.JavaMail.jira@brutus> Date: Sun, 5 Nov 2006 21:24:38 -0800 (PST) From: "Niall Pemberton (JIRA)" To: commons-dev@jakarta.apache.org Subject: [jira] Updated: (BEANUTILS-244) [collections] Add a multi property beancomparator MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org [ http://issues.apache.org/jira/browse/BEANUTILS-244?page=all ] Niall Pemberton updated BEANUTILS-244: -------------------------------------- Bugzilla Id: (was: 39008) Component/s: Bean-Collections > [collections] Add a multi property beancomparator > ------------------------------------------------- > > Key: BEANUTILS-244 > URL: http://issues.apache.org/jira/browse/BEANUTILS-244 > Project: Commons BeanUtils > Issue Type: New Feature > Components: Bean-Collections > Environment: Operating System: Windows XP > Platform: All > Reporter: Baptiste MATHUS > Priority: Minor > > Hi, > I recently needed to be able to sort a list of beans on many properties. So I > thought I would try and pick one of the collections comparators. > I had to to the sorting close to the sql way : be able to sort on n properties, > some ascending, some descending. > I haven't found what I'm looking for (:p), but I found some comparators in the > commons I used to do this : I used BeanComparator, NullComparator and > ComparatorChain to create a class : MultiPropertyBeanComparator. > Is there already something in one of the commons package that could be used > instead of it ? > If not, I'd be glad to contribute the small piece of code if wanted. It has > dependencies against commons-beanutils (BeanComparator, which is moving from one > package to another at the moment, no ?) and commons-lang > (StringUtils.isBlank()). I think some things might not satisfactory for > everybody, but hey, could still be improved without problems, that's not big > work :p. > As adviced on the user mailing list, I'm posting the code on this bugzilla, so > as maybe one of the coder could take a look at it. > Here it is : > ======================================= > import java.io.Serializable; > import java.util.ArrayList; > import java.util.Comparator; > import java.util.List; > import org.apache.commons.beanutils.BeanComparator; > import org.apache.commons.collections.ComparatorUtils; > import org.apache.commons.collections.comparators.NullComparator; > import org.apache.commons.collections.comparators.ReverseComparator; > import org.apache.commons.lang.StringUtils; > /** > * This comparator lets you sort a list using a list of properties. You can > specify ascending or > * descending order on these properties. > *

> * For example, if you want to sort with natural order a list of beans with > properties firstname, > * nickname and address, sorting the firstname descending, you can do it this way: > *

> * List l = ... > * ... > * MultiPropertyBeanComparator multiPropBeanComp = new > MultiPropertyBeanComparator(); > * multiPropBeanComp.append("firstname", > true).append("nickname").append("address"); > * Collections.sort(l,multiPropBeanComp); > * > * @author Baptiste MATHUS > */ > public class MultiPropertyBeanComparator implements Comparator, Serializable > { > private static final long serialVersionUID = -1431852774261001458L; > private List comparatorList = new ArrayList(); > /** > * Use this method to add a comparator to the list. > * > * @param property > * the property on which to apply the given comparator. > * @param comparator > * the comparator to be added. If null, natural order will be used. > * @param reverse > *

> * must be true if the given comparator must be used in opposite > order to sort. For > * example, if the comparator is designed to sort in ascending > order, put this > * parameter to true if you want descending order. > *

> *

> * If the comparator is null, then the reversed natural order is used. > *

> */ > public MultiPropertyBeanComparator append(String property, Comparator comparator, > boolean reverse) > { > if (StringUtils.isBlank(property)) > { > throw new IllegalArgumentException("The given property is blank"); > } > // If the comparator is null, then compare only on the given property > // with a natural sort. > if (comparator == null) > { > comparator = new BeanComparator(property, new NullComparator(false)); > } > // Else : compare on the property, but with given comparator. > else > { > comparator = new BeanComparator(property, comparator); > } > // Here, the comparator cannot be null anymore, so reverse it if > // necessary. > if (reverse) > { > comparator = new ReverseComparator(comparator); > } > comparatorList.add(comparator); > return this; > } > public MultiPropertyBeanComparator append(String property, Comparator c) > { > return append(property, c, false); > } > public MultiPropertyBeanComparator append(String property) > { > return append(property, null, false); > } > public MultiPropertyBeanComparator append(String property, boolean reverse) > { > return append(property, null, reverse); > } > /** > * Use this method to clear the > */ > public void clear() > { > comparatorList.clear(); > } > /** > * Considered to be equal when all properties and comparators equal. > * > * @see java.lang.Object#equals(java.lang.Object) > * @overrides > */ > public boolean equals(Object obj) > { > MultiPropertyBeanComparator comp = (MultiPropertyBeanComparator) obj; > if (this.comparatorList.size() != comp.comparatorList.size()) > { > return false; > } > for (int i = 0; i < comparatorList.size(); ++i) > { > if (!this.comparatorList.get(i).equals(comp.comparatorList.get(i))) > { > return false; > } > } > return true; > } > /** > * @see Comparator#compare(T, T) > * @overrides > */ > public int compare(Object arg0, Object arg1) > { > return getComparator().compare(arg0, arg1); > } > private Comparator getComparator() > { > return ComparatorUtils.chainedComparator(comparatorList); > } > } > ============= -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org