com.google.common.collect
Class Ordering<T>

java.lang.Object
  extended by com.google.common.collect.Ordering<T>
All Implemented Interfaces:
Serializable, Comparator<T>

public abstract class Ordering<T>
extends Object
implements Comparator<T>, Serializable

A base class for Serializable comparators that provides convenience methods for common uses.

Author:
Jesse Wilson
See Also:
Serialized Form

Constructor Summary
Ordering()
           
 
Method Summary
 int binarySearch(List<? extends T> sortedList, T key)
          Searches sortedList for key using the binary search algorithm.
static
<T> Ordering<T>
forComparator(Comparator<T> comparator)
          Returns an ordering for comparator.
<E extends T>
E
max(E a, E b)
          Returns the larger of the two values according to this ordering.
<E extends T>
E
max(E a, E b, E c, E... rest)
          Returns the largest of the specified values according to this ordering.
<E extends T>
E
max(Iterable<E> iterable)
          Returns the largest of the specified values according to this ordering.
<E extends T>
E
min(E a, E b)
          Returns the smaller of the two values according to this ordering.
<E extends T>
E
min(E a, E b, E c, E... rest)
          Returns the smallest of the specified values according to this ordering.
<E extends T>
E
min(Iterable<E> iterable)
          Returns the smallest of the specified values according to this ordering.
static
<T extends Comparable>
Ordering<T>
natural()
          Returns an ordering that uses the natural order of the values.
 Ordering<T> nullsFirst()
          Returns an ordering that treats null as less than all other values and uses this ordering to compare non-null values.
 Ordering<T> nullsLast()
          Returns an ordering that treats null as greater than all other values and uses this ordering to compare non-null values.
 Ordering<T> reverseOrder()
          Returns the ordering that is the reverse of this ordering.
 void sort(List<? extends T> list)
          Sorts list according to this ordering.
<E extends T>
List<E>
sortedCopy(Iterable<E> iterable)
          Returns a copy of the given iterable sorted by this ordering.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
compare, equals
 

Constructor Detail

Ordering

public Ordering()
Method Detail

natural

public static <T extends Comparable> Ordering<T> natural()
Returns an ordering that uses the natural order of the values. The ordering throws a NullPointerException when passed a null parameter.

The type specification is <T extends Comparable>, instead of the more specific <T extends Comparable<? super T>>, to support classes defined without generics.


forComparator

public static <T> Ordering<T> forComparator(Comparator<T> comparator)
Returns an ordering for comparator.

Parameters:
comparator - the comparator that defines the order

reverseOrder

public Ordering<T> reverseOrder()
Returns the ordering that is the reverse of this ordering.


binarySearch

public int binarySearch(List<? extends T> sortedList,
                        T key)
Searches sortedList for key using the binary search algorithm. The list must be sorted using this ordering.

Parameters:
sortedList - the list to be searched
key - the key to be searched for

sort

public void sort(List<? extends T> list)
Sorts list according to this ordering.

Parameters:
list - the list to be sorted

sortedCopy

public <E extends T> List<E> sortedCopy(Iterable<E> iterable)
Returns a copy of the given iterable sorted by this ordering. The input is not modified. The returned list is modifiable, serializable, and has random access.

Unlike Sets.newTreeSet(Comparator, Iterable), this method does not collapse elements that compare as zero, and the resulting collection does not maintain its own sort order.

Parameters:
iterable - the elements to be copied and sorted
Returns:
a new list containing the given elements in sorted order

max

public <E extends T> E max(Iterable<E> iterable)
Returns the largest of the specified values according to this ordering. If there are multiple largest values, the first of those is returned.

Parameters:
iterable - the iterable whose maximum element is to be determined
Throws:
NoSuchElementException - if iterable is empty
ClassCastException - if the parameters are not mutually comparable under this ordering.

max

public <E extends T> E max(E a,
                           E b,
                           E c,
                           E... rest)
Returns the largest of the specified values according to this ordering. If there are multiple largest values, the first of those is returned.

Parameters:
a - value to compare, returned if greater than or equal to the rest.
b - value to compare
c - value to compare
rest - values to compare
Throws:
ClassCastException - if the parameters are not mutually comparable under this ordering.

max

public <E extends T> E max(E a,
                           E b)
Returns the larger of the two values according to this ordering. If the values compare as 0, the first is returned.

Parameters:
a - value to compare, returned if greater than or equal to b.
b - value to compare.
Throws:
ClassCastException - if the parameters are not mutually comparable under this ordering.

min

public <E extends T> E min(Iterable<E> iterable)
Returns the smallest of the specified values according to this ordering. If there are multiple smallest values, the first of those is returned.

Parameters:
iterable - the iterable whose minimum element is to be determined
Throws:
NoSuchElementException - if iterable is empty
ClassCastException - if the parameters are not mutually comparable under this ordering.

min

public <E extends T> E min(E a,
                           E b,
                           E c,
                           E... rest)
Returns the smallest of the specified values according to this ordering. If there are multiple smallest values, the first of those is returned.

Parameters:
a - value to compare, returned if less than or equal to the rest.
b - value to compare
c - value to compare
rest - values to compare
Throws:
ClassCastException - if the parameters are not mutually comparable under this ordering.

min

public <E extends T> E min(E a,
                           E b)
Returns the smaller of the two values according to this ordering. If the values compare as 0, the first is returned.

Parameters:
a - value to compare, returned if less than or equal to b.
b - value to compare.
Throws:
ClassCastException - if the parameters are not mutually comparable under this ordering.

nullsFirst

public Ordering<T> nullsFirst()
Returns an ordering that treats null as less than all other values and uses this ordering to compare non-null values.


nullsLast

public Ordering<T> nullsLast()
Returns an ordering that treats null as greater than all other values and uses this ordering to compare non-null values.