All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jagg.util.PropertiesComparator Maven / Gradle / Ivy

Go to download

jAgg is a Java 5.0 API that supports “group by” operations on Lists of Java objects: aggregate operations such as count, sum, max, min, avg, and many more. It also allows custom aggregate operations.

The newest version!
package net.sf.jagg.util;

import java.util.Comparator;
import java.util.List;

import net.sf.jagg.exception.ExpectedComparableException;
import net.sf.jagg.Aggregator;

/**
 * This class represents a flexible Comparator that is capable of
 * comparing two objects based on a dynamic list of properties of the objects
 * of type T.  This class is used internally by
 * Aggregation to sort a List<T> according to
 * a specified list of properties.
 *
 * @author Randy Gettman
 * @since 0.1.0
 */
public class PropertiesComparator implements Comparator
{
   private List myProperties;
   private int mySize;

   /**
    * Construct a PropertiesComparator that pays attention to the
    * given List of properties in the generic type T.
    * All properties must be Comparable.
    *
    * @param properties A List<String> of properties.
    */
   public PropertiesComparator(List properties)
   {
      myProperties = properties;
      mySize = properties.size();
   }

   /**
    * Returns the properties that this PropertiesComparator
    * compares.
    * @return A List of property strings.
    * @since 0.9.0
    */
   public List getProperties()
   {
      return myProperties;
   }

   /**
    * 

Compares the given objects to determine order. Fulfills the * Comparator contract by returning a negative integer, 0, or a * positive integer if o1 is less than, equal to, or greater * than o2.

*

Nulls properties compare equal to each other, and a null property * compares greater than a non-null property.

* * @param o1 The left-hand-side object to compare. * @param o2 The right-hand-side object to compare. * @return A negative integer, 0, or a positive integer if o1 * is less than, equal to, or greater than o2. * @throws ExpectedComparableException If any property specified in the * constructor doesn't correspond to a no-argument "get<Property>" * getter method in T, or if the property's type is not * Comparable. */ @SuppressWarnings("unchecked") public int compare(T o1, T o2) throws ExpectedComparableException { int comp; for (int i = 0; i < mySize; i++) { String property = myProperties.get(i); Comparable value1 = (Comparable) Aggregator.getValueFromProperty(o1, property); Comparable value2 = (Comparable) Aggregator.getValueFromProperty(o2, property); try { if (value1 == null) { if (value2 == null) comp = 0; else comp = 1; } else { if (value2 == null) comp = -1; else comp = value1.compareTo(value2); } if (comp != 0) return comp; } catch (ClassCastException e) { throw new ExpectedComparableException("Property \"" + property + "\" needs to be Comparable."); } } return 0; } /** * Indicates whether the given PropertiesComparator is equal to * this PropertiesComparator. All property names must match in * order. * * @param obj The other PropertiesComparator. */ public boolean equals(Object obj) { if (obj instanceof PropertiesComparator) { PropertiesComparator otherComp = (PropertiesComparator) obj; if (mySize != otherComp.mySize) return false; for (int i = 0; i < mySize; i++) { if (!myProperties.get(i).equals(otherComp.myProperties.get(i))) return false; } return true; } return false; } /** * Returns a hash code. * @return A hash code. */ public int hashCode() { int hc = 0; for (String property : myProperties) { hc = 31 * hc + property.hashCode(); } return hc; } /** * Returns the string representation. * @return The string representation. */ public String toString() { StringBuilder buf = new StringBuilder(); buf.append("PropertiesComparator("); for (int i = 0; i < myProperties.size(); i++) { if (i != 0) buf.append(", "); buf.append(myProperties.get(i)); } buf.append(")"); return buf.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy