@GwtCompatible public abstract class ComparisonChain extends Object
public int compareTo(Foo that) { return ComparisonChain.start() .compare(this.aString, that.aString) .compare(this.anInt, that.anInt) .compare(this.anEnum, that.anEnum, Ordering.natural().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.
Performance note: Even though the ComparisonChain caller always invokes its compare methods unconditionally, the ComparisonChain implementation stops calling its inputs' compareTo and compare methods as soon as one of them returns a nonzero result. This optimization is typically important only in the presence of expensive compareTo and compare implementations.
See the Guava User Guide article on ComparisonChain.
| Modifier and Type | Method and Description |
|---|---|
abstract ComparisonChain |
compare(Comparable
Compares two comparable objects as specified by
Comparable,
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,
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,
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,
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,
if the result of this comparison chain has not already been determined.
|
abstract <T> ComparisonChain |
compare(T left, T right, Comparator
Compares two objects using a comparator,
if the result of this comparison chain has not already been determined.
|
abstract ComparisonChain |
compareFalseFirst(boolean left, boolean right)
Compares two
boolean values, considering
false to be less than
true,
if the result of this comparison chain has not already been determined.
|
abstract ComparisonChain |
compareTrueFirst(boolean left, boolean right)
Compares two
boolean values, considering
true to be less than
false,
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.
|
public static ComparisonChainstart()
public abstract ComparisonChaincompare(Comparable <?> left, Comparable <?> right)
Comparable.compareTo(T) ,
if the result of this comparison chain has not already been determined.
public abstract <T> ComparisonChaincompare(T left, T right, Comparator <T> comparator)
public abstract ComparisonChaincompare(int left, int right)
int values as specified by
Ints.compare(int, int) ,
if the result of this comparison chain has not already been determined.
public abstract ComparisonChaincompare(long left, long right)
long values as specified by
Longs.compare(long, long) ,
if the result of this comparison chain has not already been determined.
public abstract ComparisonChaincompare(float left, float right)
float values as specified by
Float.compare(float, float) ,
if the result of this comparison chain has not already been determined.
public abstract ComparisonChaincompare(double left, double right)
double values as specified by
Double.compare(double, double) ,
if the result of this comparison chain has not already been determined.
public abstract ComparisonChaincompareTrueFirst(boolean left, boolean right)
boolean values, considering
true to be less than
false,
if the result of this comparison chain has not already been determined.
public abstract ComparisonChaincompareFalseFirst(boolean left, boolean right)
boolean values, considering
false to be less than
true,
if the result of this comparison chain has not already been determined.
compare since 2.0)
public abstract int result()