You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Stephen Colebourne (JIRA)" <ji...@apache.org> on 2006/07/20 00:18:14 UTC
[jira] Moved: (BEANUTILS-244) [collections] Add a multi property
beancomparator
[ http://issues.apache.org/jira/browse/BEANUTILS-244?page=all ]
Stephen Colebourne moved COLLECTIONS-144 to BEANUTILS-244:
----------------------------------------------------------
Project: Commons BeanUtils (was: Commons Collections)
Key: BEANUTILS-244 (was: COLLECTIONS-144)
Issue Type: New Feature (was: Improvement)
> [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
> 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.
> * <p>
> * 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:
> * </p>
> * <code>List l = ...</code>
> * <code>...</code>
> * <code>MultiPropertyBeanComparator multiPropBeanComp = new
> MultiPropertyBeanComparator();</code>
> * <code>multiPropBeanComp.append("firstname",
> true).append("nickname").append("address");</code>
> * <code>Collections.sort(l,multiPropBeanComp);</code>
> *
> * @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
> * <p>
> * 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 <code>true</code> if you want descending order.
> * </p>
> * <p>
> * If the comparator is null, then the reversed natural order is used.
> * </p>
> */
> 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