Class Ordering<T>

java.lang.Object
org.glassfish.jersey.internal.guava.Ordering<T>
All Implemented Interfaces:
Comparator<T>

public abstract class Ordering<T> extends Object implements Comparator<T>
A comparator, with additional methods to support common operations. This is an "enriched" version of Comparator, in the same sense that FluentIterable is an enriched Iterable.

The common ways to get an instance of Ordering are:

Then you can use the chaining methods to get an altered version of that Ordering, including:

  • reverse()
  • #compound(Comparator)
  • #onResultOf(Function)
  • nullsFirst() / nullsLast()

Finally, use the resulting Ordering anywhere a Comparator is required, or use any of its special operations, such as:

  • #immutableSortedCopy
  • #isOrdered / #isStrictlyOrdered
  • min(java.util.Iterator<E>) / max(java.util.Iterator<E>)

Except as noted, the orderings returned by the factory methods of this class are serializable if and only if the provided instances that back them are. For example, if ordering and function can themselves be serialized, then ordering.onResultOf(function) can as well.

See the Guava User Guide article on Ordering.

Since:
2.0 (imported from Google Collections Library)
Author:
Jesse Wilson, Kevin Bourrillion
  • Method Details

    • natural

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

      The type specification is <C extends Comparable>, instead of the technically correct <C extends Comparable<? super C>>, to support legacy types from before Java 5.

    • from

      public static <T> Ordering<T> from(Comparator<T> comparator)
      Returns an ordering based on an existing comparator instance. Note that it is unnecessary to create a new anonymous inner class implementing Comparator just to pass it in here. Instead, simply subclass Ordering and implement its compare method directly.
      Parameters:
      comparator - the comparator that defines the order
      Returns:
      comparator itself if it is already an Ordering; otherwise an ordering that wraps that comparator
    • reverse

      public <S extends T> Ordering<S> reverse()
      Returns the reverse of this ordering; the Ordering equivalent to Collections.reverseOrder(Comparator).
    • compare

      public abstract int compare(T left, T right)
      Specified by:
      compare in interface Comparator<T>