Package com.google.common.collect
Class ComparisonChain
- java.lang.Object
-
- com.google.common.collect.ComparisonChain
-
@GwtCompatible public abstract class ComparisonChain extends Object
A utility for performing a chained comparison statement. 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.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:
ComparisonChaininstances are immutable. For this utility to work correctly, calls must be chained as illustrated above.Performance note: Even though the
ComparisonChaincaller always invokes itscomparemethods unconditionally, theComparisonChainimplementation stops calling its inputs'compareToandcomparemethods as soon as one of them returns a nonzero result. This optimization is typically important only in the presence of expensivecompareToandcompareimplementations.See the Guava User Guide article on
ComparisonChain.- Since:
- 2.0
- Author:
- Mark Davis, Kevin Bourrillion
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract ComparisonChaincompare(double left, double right)Compares twodoublevalues as specified byDouble.compare(double, double), if the result of this comparison chain has not already been determined.abstract ComparisonChaincompare(float left, float right)Compares twofloatvalues as specified byFloat.compare(float, float), if the result of this comparison chain has not already been determined.abstract ComparisonChaincompare(int left, int right)Compares twointvalues as specified byInts.compare(int, int), if the result of this comparison chain has not already been determined.abstract ComparisonChaincompare(long left, long right)Compares twolongvalues as specified byLongs.compare(long, long), if the result of this comparison chain has not already been determined.abstract <T> ComparisonChaincompare(@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.ComparisonChaincompare(Boolean left, Boolean right)Deprecated.UsecompareFalseFirst(boolean, boolean); or, if the parameters passed are being either negated or reversed, undo the negation or reversal and usecompareTrueFirst(boolean, boolean).abstract ComparisonChaincompare(Comparable<?> left, Comparable<?> right)Compares two comparable objects as specified byComparable.compareTo(T), if the result of this comparison chain has not already been determined.abstract ComparisonChaincompareFalseFirst(boolean left, boolean right)Compares twobooleanvalues, consideringfalseto be less thantrue, if the result of this comparison chain has not already been determined.abstract ComparisonChaincompareTrueFirst(boolean left, boolean right)Compares twobooleanvalues, consideringtrueto be less thanfalse, if the result of this comparison chain has not already been determined.abstract intresult()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 ComparisonChainstart()Begins a new chained comparison statement.
-
-
-
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 byComparable.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 twointvalues as specified byInts.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 twolongvalues as specified byLongs.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 twofloatvalues as specified byFloat.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 twodoublevalues as specified byDouble.compare(double, double), if the result of this comparison chain has not already been determined.
-
compare
@Deprecated public final ComparisonChain compare(Boolean left, Boolean right)
Deprecated.UsecompareFalseFirst(boolean, boolean); or, if the parameters passed are being either negated or reversed, undo the negation or reversal and usecompareTrueFirst(boolean, boolean).Discouraged synonym forcompareFalseFirst(boolean, boolean).- Since:
- 19.0
-
compareTrueFirst
public abstract ComparisonChain compareTrueFirst(boolean left, boolean right)
Compares twobooleanvalues, consideringtrueto be less thanfalse, if the result of this comparison chain has not already been determined.- Since:
- 12.0
-
compareFalseFirst
public abstract ComparisonChain compareFalseFirst(boolean left, boolean right)
Compares twobooleanvalues, consideringfalseto be less thantrue, if the result of this comparison chain has not already been determined.- Since:
- 12.0 (present as
comparesince 2.0)
-
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.
-
-