Class 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: 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.

    Since:
    2.0
    Author:
    Mark Davis, Kevin Bourrillion
    • 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.

        This method is declared to accept any 2 Comparable objects, even if they are not mutually comparable. If you pass objects that are not mutually comparable, this method may throw an exception. (The reason for this decision is lost to time, but the reason might be that we wanted to support legacy classes that implement the raw type Comparable (instead of implementing Comparable<Foo>) without producing warnings. If so, we would prefer today to produce warnings in that case, and we may change this method to do so in the future. Support for raw Comparable types in Guava in general is tracked as #989.)

        Throws:
        ClassCastException - if the parameters are not mutually comparable
      • compare

        public abstract <T extends @Nullable ObjectComparisonChain 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.
      • compareTrueFirst

        public 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.
        Since:
        12.0
      • compareFalseFirst

        public 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.
        Since:
        12.0 (present as compare since 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.