Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-244

[collections] Add a multi property beancomparator

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 2.0.0
    • Component/s: Bean-Collections
    • Labels:
      None
    • Environment:

      Operating System: Windows XP
      Platform: All

    • Bugzilla Id:
      39008

      Description

      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 (), 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 .

      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.equals(comp.comparatorList.get))

      { 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); }

      }
      =============

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mathus.b@mipih.fr Baptiste MATHUS
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: