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

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

public abstract class Ordering<T>
extends java.lang.Object
implements java.util.Comparator<T>, java.io.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(java.util.List<? extends T> sortedList, T key)
          Searches list for key using the binary search algorithm.
static
<T> Ordering<T>
forComparator(java.util.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(java.lang.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(java.lang.Iterable<E> iterable)
          Returns the smallest of the specified values according to this ordering.
 Ordering<T> reverseOrder()
          Returns the ordering that is the reverse of this ordering.
 void sort(java.util.List<? extends T> list)
          Sorts list according to this ordering.
<E extends T>
java.util.List<E>
sortedCopy(java.lang.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

forComparator

public static <T> Ordering<T> forComparator(java.util.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(java.util.List<? extends T> sortedList,
                        T key)
Searches list 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(java.util.List<? extends T> list)
Sorts list according to this ordering.

Parameters:
list - the list to be sorted

sortedCopy

public <E extends T> java.util.List<E> sortedCopy(java.lang.Iterable<E> iterable)
Returns a copy of the given iterable sorted by this ordering. The input is not modified.

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. If you have no preference on these issues, these two alternatives are equivalent, so you can choose for performance factors.

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(java.lang.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:
java.util.NoSuchElementException - if iterable is empty
java.lang.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:
java.lang.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:
java.lang.ClassCastException - if the parameters are not mutually comparable under this ordering.

min

public <E extends T> E min(java.lang.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:
java.util.NoSuchElementException - if iterable is empty
java.lang.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:
java.lang.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:
java.lang.ClassCastException - if the parameters are not mutually comparable under this ordering.


Copyright © 2008 Google. All Rights Reserved.