Note
Java 8+ users should
generally prefer the methods in Comparator; see below.
Example usage of ComparisonChain:
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.
Note
ComparisonChain instances are immutable. For this utility to work
correctly, calls must be chained as illustrated above.
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.
Comparator instead of ComparisonChain. The example above can be implemented like this:
import static java.util.Comparator.comparing; import static java.util.Comparator.nullsLast; import static java.util.Comparator.naturalOrder; ... private static final Comparator<Foo> COMPARATOR = comparing((Foo foo) -> foo.aString) .thenComparing(foo -> foo.anInt) .thenComparing(foo -> foo.anEnum, nullsLast(naturalOrder()));@Overridepublic int compareTo(Foo that) { return COMPARATOR.compare(this, that); }
With method references it is more succinct: comparing(Foo::aString) for example.
Using Comparator avoids certain types of bugs, for example when you meant to write
.compare(a.foo, b.foo) but you actually wrote .compare(a.foo, a.foo) or .compare(a.foo, b.bar). ComparisonChain also has a potential performance problem that
Comparator doesn't: it evaluates all the parameters of all the .compare calls,
even when the result of the comparison is already known from previous .compare calls.
That can be expensive.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final ComparisonChain | |
| private static final ComparisonChain | |
| private static final ComparisonChain |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public abstract ComparisonChain | compare(Comparable<?> left, Comparable<?> right)
Compares two comparable objects as specified by |
| public abstract <T extends @Nullable Object> 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. |
| public abstract ComparisonChain | compare(int left, int right)
Compares two |
| public abstract ComparisonChain | compare(long left, long right)
Compares two |
| public abstract ComparisonChain | compare(float left, float right)
Compares two |
| public abstract ComparisonChain | compare(double left, double right)
Compares two |
| public final ComparisonChain | compare(Boolean left, Boolean right)
Deprecated
Use
Discouraged synonym for compareFalseFirst; or, if the parameters passed are being either
negated or reversed, undo the negation or reversal and use compareTrueFirst.
compareFalseFirst.
|
| public abstract ComparisonChain | compareFalseFirst(boolean left, boolean right)
Compares two |
| public abstract ComparisonChain | compareTrueFirst(boolean left, boolean right)
Compares two |
| 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. |
| public static ComparisonChain |
| ACTIVE | back to summary |
|---|---|
| private static final ComparisonChain ACTIVE | |
| GREATER | back to summary |
|---|---|
| private static final ComparisonChain GREATER | |
| LESS | back to summary |
|---|---|
| private static final ComparisonChain LESS | |
| ComparisonChain | back to summary |
|---|---|
| private ComparisonChain() | |
| compare | back to summary |
|---|---|
| public abstract ComparisonChain compare(Comparable<?> left, Comparable<?> right) Compares two comparable objects as specified by This method is declared to accept any 2
| |
| compare | back to summary |
|---|---|
| public abstract <T extends @Nullable Object> 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. | |
| compare | back to summary |
|---|---|
| public abstract ComparisonChain compare(int left, int right) Compares two | |
| compare | back to summary |
|---|---|
| public abstract ComparisonChain compare(long left, long right) Compares two | |
| compare | back to summary |
|---|---|
| public abstract ComparisonChain compare(float left, float right) Compares two | |
| compare | back to summary |
|---|---|
| public abstract ComparisonChain compare(double left, double right) Compares two | |
| compare | back to summary |
|---|---|
| public final ComparisonChain compare(Boolean left, Boolean right)
Deprecated Use Discouraged synonym for
| |
| compareFalseFirst | back to summary |
|---|---|
| public abstract ComparisonChain compareFalseFirst(boolean left, boolean right) Compares two Java 8+ users: you can get the equivalent from
Comparator.comparing(Foo::isBar,
| |
| compareTrueFirst | back to summary |
|---|---|
| public abstract ComparisonChain compareTrueFirst(boolean left, boolean right) Compares two Java 8+ users: you can get the equivalent from
Comparator.comparing(Foo::isBar,
| |
| result | back to summary |
|---|---|
| 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. | |
| start | back to summary |
|---|---|
| public static ComparisonChain start() Begins a new chained comparison statement. See example in the class documentation. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public ComparisonChain | compare(Comparable<?> left, Comparable<?> right)
Implements abstract com. Comparable#compareTo, if the
result of this comparison chain has not already been determined.
|
| public <T extends @Nullable Object> ComparisonChain | compare(T left, T right, Comparator<T> comparator)
Implements abstract com. |
| public ComparisonChain | compare(int left, int right)
Implements abstract com. int values as specified by Ints#compare, if the result of
this comparison chain has not already been determined.
|
| public ComparisonChain | compare(long left, long right)
Implements abstract com. long values as specified by Longs#compare, if the result of
this comparison chain has not already been determined.
|
| public ComparisonChain | compare(float left, float right)
Implements abstract com. float values as specified by Float#compare, if the result
of this comparison chain has not already been determined.
|
| public ComparisonChain | compare(double left, double right)
Implements abstract com. double values as specified by Double#compare, if the result
of this comparison chain has not already been determined.
|
| public ComparisonChain | compareFalseFirst(boolean left, boolean right)
Implements abstract com. boolean values, considering false to be less than true,
if the result of this comparison chain has not already been determined.
|
| public ComparisonChain | compareTrueFirst(boolean left, boolean right)
Implements abstract com. boolean values, considering true to be less than false,
if the result of this comparison chain has not already been determined.
|
| public int | result()
Implements abstract com. |
| result | back to summary |
|---|---|
| pack-priv final int result | |
| InactiveComparisonChain | back to summary |
|---|---|
| pack-priv InactiveComparisonChain(int result) | |
| compare | back to summary |
|---|---|
| public ComparisonChain compare(Comparable<?> left, Comparable<?> right) Implements abstract com. Doc from com. Compares two comparable objects as specified by This method is declared to accept any 2
| |
| compare | back to summary |
|---|---|
| public <T extends @Nullable Object> ComparisonChain compare(T left, T right, Comparator<T> comparator) Implements abstract com. Doc from com. Compares two objects using a comparator, if the result of this comparison chain has not already been determined.
| |
| compare | back to summary |
|---|---|
| public ComparisonChain compare(int left, int right) Implements abstract com. Doc from com. Compares two
| |
| compare | back to summary |
|---|---|
| public ComparisonChain compare(long left, long right) Implements abstract com. Doc from com. Compares two
| |
| compare | back to summary |
|---|---|
| public ComparisonChain compare(float left, float right) Implements abstract com. Doc from com. Compares two
| |
| compare | back to summary |
|---|---|
| public ComparisonChain compare(double left, double right) Implements abstract com. Doc from com. Compares two
| |
| compareFalseFirst | back to summary |
|---|---|
| public ComparisonChain compareFalseFirst(boolean left, boolean right) Implements abstract com. Doc from com. Compares two Java 8+ users: you can get the equivalent from
Comparator.comparing(Foo::isBar,
| |
| compareTrueFirst | back to summary |
|---|---|
| public ComparisonChain compareTrueFirst(boolean left, boolean right) Implements abstract com. Doc from com. Compares two Java 8+ users: you can get the equivalent from
Comparator.comparing(Foo::isBar,
| |
| result | back to summary |
|---|---|
| public int result() Implements abstract com. Doc from com. 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.
| |