
net.sf.jagg.util.PropertiesComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jagg-core Show documentation
Show all versions of jagg-core Show documentation
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
. * * @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 ifComparator
that is capable of * comparing two objects based on a dynamic list of properties of the objects * of typeT
. This class is used internally by *Aggregation
to sort aList<T>
according to * a specified list of properties. * * @author Randy Gettman * @since 0.1.0 */ public class PropertiesComparatorimplements Comparator { private List myProperties; private int mySize; /** * Construct a PropertiesComparator
that pays attention to the * givenList
of properties in the generic typeT
. * All properties must beComparable
. * * @param properties AList<String>
of properties. */ public PropertiesComparator(Listproperties) { myProperties = properties; mySize = properties.size(); } /** * Returns the properties that this PropertiesComparator
* compares. * @return AList
of property strings. * @since 0.9.0 */ public ListgetProperties() { return myProperties; } /** * Compares the given objects to determine order. Fulfills the *
*Comparator
contract by returning a negative integer, 0, or a * positive integer ifo1
is less than, equal to, or greater * thano2
.Nulls properties compare equal to each other, and a null property * compares greater than a non-null property
o1
* is less than, equal to, or greater thano2
. * @throws ExpectedComparableException If any property specified in the * constructor doesn't correspond to a no-argument "get<Property>" * getter method inT
, 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 givenPropertiesComparator
is equal to * thisPropertiesComparator
. All property names must match in * order. * * @param obj The otherPropertiesComparator
. */ 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