com.google.common.collect
Class ComparisonChain

java.lang.Object
  extended by com.google.common.collect.ComparisonChain

public abstract class ComparisonChain
extends Object

A utility for performing a "lazy" chained comparison statement, which performs comparisons only until it finds a nonzero result. For example:

   public int compareTo(Foo that) {
     return ComparisonChain.start()
         .compare(this.aString, that.aString)
         .compare(this.anInt, that.anInt)
         .compare(this.anEnum, that.anEnum, Ordering.nullsLast())
         .result();
   }
The value of this expression will have the same sign as the first nonzero comparison result in the chain, or will be zero if every comparison result was zero.

Once any comparison returns a nonzero value, remaining comparisons are "short-circuited".

Since:
2010.01.04 tentative
Author:
Mark Davis, Kevin Bourrillion

Method Summary
abstract  ComparisonChain compare(boolean left, boolean right)
          Compares two boolean values as specified by Booleans.compare(boolean, boolean), if the result of this comparison chain has not already been determined.
abstract  ComparisonChain compare(Comparable<?> left, Comparable<?> right)
          Compares two comparable objects as specified by Comparable.compareTo(T), if the result of this comparison chain has not already been determined.
abstract  ComparisonChain compare(double left, double right)
          Compares two double values as specified by Double.compare(double, double), if the result of this comparison chain has not already been determined.
abstract  ComparisonChain compare(float left, float right)
          Compares two float values as specified by Float.compare(float, float), if the result of this comparison chain has not already been determined.
abstract  ComparisonChain compare(int left, int right)
          Compares two int values as specified by Ints.compare(int, int), if the result of this comparison chain has not already been determined.
abstract  ComparisonChain compare(long left, long right)
          Compares two long values as specified by Longs.compare(long, long), if the result of this comparison chain has not already been determined.
abstract
<T> ComparisonChain
compare(T left, T right, Comparator<T> comparator)
          Compares two objects using a comparator, if the result of this comparison chain has not already been determined.
abstract  int result()
          Ends this comparison chain and returns its result: a value having the same sign as the first nonzero comparison result in the chain, or zero if every result was zero.
static ComparisonChain start()
          Begins a new chained comparison statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

start

public static ComparisonChain start()
Begins a new chained comparison statement. See example in the class documentation.


compare

public abstract ComparisonChain compare(Comparable<?> left,
                                        Comparable<?> right)
Compares two comparable objects as specified by Comparable.compareTo(T), if the result of this comparison chain has not already been determined.


compare

public abstract <T> ComparisonChain compare(@Nullable
                                            T left,
                                            @Nullable
                                            T right,
                                            Comparator<T> comparator)
Compares two objects using a comparator, if the result of this comparison chain has not already been determined.


compare

public abstract ComparisonChain compare(int left,
                                        int right)
Compares two int values as specified by Ints.compare(int, int), if the result of this comparison chain has not already been determined.


compare

public abstract ComparisonChain compare(long left,
                                        long right)
Compares two long values as specified by Longs.compare(long, long), if the result of this comparison chain has not already been determined.


compare

public abstract ComparisonChain compare(float left,
                                        float right)
Compares two float values as specified by Float.compare(float, float), if the result of this comparison chain has not already been determined.


compare

public abstract ComparisonChain compare(double left,
                                        double right)
Compares two double values as specified by Double.compare(double, double), if the result of this comparison chain has not already been determined.


compare

public abstract ComparisonChain compare(boolean left,
                                        boolean right)
Compares two boolean values as specified by Booleans.compare(boolean, boolean), if the result of this comparison chain has not already been determined.


result

public abstract int result()
Ends this comparison chain and returns its result: a value having the same sign as the first nonzero comparison result in the chain, or zero if every result was zero.