net.sf.jett.util
Class OrderByComparator<T>

java.lang.Object
  extended by net.sf.jett.util.OrderByComparator<T>
All Implemented Interfaces:
java.util.Comparator<T>

public class OrderByComparator<T>
extends java.lang.Object
implements java.util.Comparator<T>

An OrderByComparator is a Comparator that is capable of comparing two objects based on a dynamic list of properties of the objects of type T. It can sort any of its properties ascending or descending, and for any of its properties, it can place nulls first or last. Like SQL, this will default to ascending. Nulls default to last if ascending, and first if descending.

This is based on jAgg's PropertiesComparator, which as of the time of creation of this class always does ascending, nulls last.

Since:
0.3.0
Author:
Randy Gettman

Field Summary
static java.lang.String ASC
          Sort ascending (default).
static java.lang.String DESC
          Sort descending.
static java.lang.String FIRST
          Sort nulls first (default if descending order).
static java.lang.String LAST
          Sort nulls last (default if ascending order).
static java.lang.String NULLS
          Use this to indicate which sequence nulls should be ordered.
static int NULLS_FIRST
          Constant to order nulls first.
static int NULLS_LAST
          Constant to order nulls last.
static int ORDER_ASC
          Constant to order ascending.
static int ORDER_DESC
          Constant to order descending.
 
Constructor Summary
OrderByComparator(java.util.List<java.lang.String> expressions)
          Constructs an OrderByComparator based on a List of expressions, of the format "property [ASC|DESC] [NULLS FIRST|LAST]".
 
Method Summary
 int compare(T o1, T o2)
          Compares the given objects to determine order.
 boolean equals(java.lang.Object obj)
          Indicates whether the given OrderByComparator is equal to this OrderByComparator.
 java.util.List<java.lang.Integer> getNullOrderings()
          Returns a List of null orderings.
 java.util.List<java.lang.Integer> getOrderings()
          Returns a List of orderings.
 java.util.List<java.lang.String> getProperties()
          Returns a List of all properties.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ASC

public static final java.lang.String ASC
Sort ascending (default).

See Also:
Constant Field Values

DESC

public static final java.lang.String DESC
Sort descending.

See Also:
Constant Field Values

NULLS

public static final java.lang.String NULLS
Use this to indicate which sequence nulls should be ordered.

See Also:
Constant Field Values

FIRST

public static final java.lang.String FIRST
Sort nulls first (default if descending order).

See Also:
Constant Field Values

LAST

public static final java.lang.String LAST
Sort nulls last (default if ascending order).

See Also:
Constant Field Values

ORDER_ASC

public static final int ORDER_ASC
Constant to order ascending.

See Also:
Constant Field Values

ORDER_DESC

public static final int ORDER_DESC
Constant to order descending.

See Also:
Constant Field Values

NULLS_LAST

public static final int NULLS_LAST
Constant to order nulls last.

See Also:
Constant Field Values

NULLS_FIRST

public static final int NULLS_FIRST
Constant to order nulls first.

See Also:
Constant Field Values
Constructor Detail

OrderByComparator

public OrderByComparator(java.util.List<java.lang.String> expressions)
Constructs an OrderByComparator based on a List of expressions, of the format "property [ASC|DESC] [NULLS FIRST|LAST]".

Parameters:
expressions - A List of expressions.
Throws:
ParseException - If there is a problem parsing the expressions.
Method Detail

compare

public int compare(T o1,
                   T o2)
            throws java.lang.UnsupportedOperationException

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.

This compare method respects all properties, their order sequences, and their null order sequences.

Specified by:
compare in interface java.util.Comparator<T>
Parameters:
o1 - The left-hand-side object to compare.
o2 - The right-hand-side object to compare.
Returns:
A negative integer, 0, or a positive integer if o1 is less than, equal to, or greater than o2.
Throws:
java.lang.UnsupportedOperationException - 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.

equals

public boolean equals(java.lang.Object obj)
Indicates whether the given OrderByComparator is equal to this OrderByComparator. All property names must match in order, and all of the order sequences and null order sequences must match.

Specified by:
equals in interface java.util.Comparator<T>
Overrides:
equals in class java.lang.Object
Parameters:
obj - The other OrderByComparator.

getProperties

public java.util.List<java.lang.String> getProperties()
Returns a List of all properties.

Returns:
A List of all properties.

getOrderings

public java.util.List<java.lang.Integer> getOrderings()
Returns a List of orderings.

Returns:
A List of orderings.
See Also:
ORDER_ASC, ORDER_DESC

getNullOrderings

public java.util.List<java.lang.Integer> getNullOrderings()
Returns a List of null orderings.

Returns:
A List of null orderings.
See Also:
NULLS_FIRST, NULLS_LAST


Copyright © 2012-2013 Jett Team. All Rights Reserved.