001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.common.primitives.Booleans;
021import com.google.errorprone.annotations.InlineMe;
022import java.util.Comparator;
023import org.jspecify.annotations.Nullable;
024
025/**
026 * A utility for performing a chained comparison statement. <b>Note:</b> Java 8+ users should
027 * generally prefer the methods in {@link Comparator}; see <a href="#java8">below</a>.
028 *
029 * <p>Example usage of {@code ComparisonChain}:
030 *
031 * <pre>{@code
032 * public int compareTo(Foo that) {
033 *   return ComparisonChain.start()
034 *       .compare(this.aString, that.aString)
035 *       .compare(this.anInt, that.anInt)
036 *       .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
037 *       .result();
038 * }
039 * }</pre>
040 *
041 * <p>The value of this expression will have the same sign as the <i>first nonzero</i> comparison
042 * result in the chain, or will be zero if every comparison result was zero.
043 *
044 * <p><b>Note:</b> {@code ComparisonChain} instances are <b>immutable</b>. For this utility to work
045 * correctly, calls must be chained as illustrated above.
046 *
047 * <p>Performance note: Even though the {@code ComparisonChain} caller always invokes its {@code
048 * compare} methods unconditionally, the {@code ComparisonChain} implementation stops calling its
049 * inputs' {@link Comparable#compareTo compareTo} and {@link Comparator#compare compare} methods as
050 * soon as one of them returns a nonzero result. This optimization is typically important only in
051 * the presence of expensive {@code compareTo} and {@code compare} implementations.
052 *
053 * <p>See the Guava User Guide article on <a href=
054 * "https://github.com/google/guava/wiki/CommonObjectUtilitiesExplained#comparecompareto">{@code
055 * ComparisonChain}</a>.
056 *
057 * <h4 id="java8">Java 8+ equivalents</h4>
058 *
059 * If you are using Java version 8 or greater, you should generally use the static methods in {@link
060 * Comparator} instead of {@code ComparisonChain}. The example above can be implemented like this:
061 *
062 * <pre>{@code
063 * import static java.util.Comparator.comparing;
064 * import static java.util.Comparator.nullsLast;
065 * import static java.util.Comparator.naturalOrder;
066 *
067 * ...
068 *   private static final Comparator<Foo> COMPARATOR =
069 *       comparing((Foo foo) -> foo.aString)
070 *           .thenComparing(foo -> foo.anInt)
071 *           .thenComparing(foo -> foo.anEnum, nullsLast(naturalOrder()));}
072 *
073 *   {@code @Override}{@code
074 *   public int compareTo(Foo that) {
075 *     return COMPARATOR.compare(this, that);
076 *   }
077 * }</pre>
078 *
079 * <p>With method references it is more succinct: {@code comparing(Foo::aString)} for example.
080 *
081 * <p>Using {@link Comparator} avoids certain types of bugs, for example when you meant to write
082 * {@code .compare(a.foo, b.foo)} but you actually wrote {@code .compare(a.foo, a.foo)} or {@code
083 * .compare(a.foo, b.bar)}. {@code ComparisonChain} also has a potential performance problem that
084 * {@code Comparator} doesn't: it evaluates all the parameters of all the {@code .compare} calls,
085 * even when the result of the comparison is already known from previous {@code .compare} calls.
086 * That can be expensive.
087 *
088 * @author Mark Davis
089 * @author Kevin Bourrillion
090 * @since 2.0
091 */
092@GwtCompatible
093public abstract class ComparisonChain {
094  private ComparisonChain() {}
095
096  /** Begins a new chained comparison statement. See example in the class documentation. */
097  public static ComparisonChain start() {
098    return ACTIVE;
099  }
100
101  private static final ComparisonChain ACTIVE =
102      new ComparisonChain() {
103        @SuppressWarnings("unchecked") // unsafe; see discussion on supertype
104        @Override
105        public ComparisonChain compare(Comparable<?> left, Comparable<?> right) {
106          return classify(((Comparable<Object>) left).compareTo(right));
107        }
108
109        @Override
110        public <T extends @Nullable Object> ComparisonChain compare(
111            @ParametricNullness T left, @ParametricNullness T right, Comparator<T> comparator) {
112          return classify(comparator.compare(left, right));
113        }
114
115        @Override
116        public ComparisonChain compare(int left, int right) {
117          return classify(Integer.compare(left, right));
118        }
119
120        @Override
121        public ComparisonChain compare(long left, long right) {
122          return classify(Long.compare(left, right));
123        }
124
125        @Override
126        public ComparisonChain compare(float left, float right) {
127          return classify(Float.compare(left, right));
128        }
129
130        @Override
131        public ComparisonChain compare(double left, double right) {
132          return classify(Double.compare(left, right));
133        }
134
135        @Override
136        public ComparisonChain compareTrueFirst(boolean left, boolean right) {
137          return classify(Boolean.compare(right, left)); // reversed
138        }
139
140        @Override
141        public ComparisonChain compareFalseFirst(boolean left, boolean right) {
142          return classify(Boolean.compare(left, right));
143        }
144
145        ComparisonChain classify(int result) {
146          return (result < 0) ? LESS : (result > 0) ? GREATER : ACTIVE;
147        }
148
149        @Override
150        public int result() {
151          return 0;
152        }
153      };
154
155  private static final ComparisonChain LESS = new InactiveComparisonChain(-1);
156
157  private static final ComparisonChain GREATER = new InactiveComparisonChain(1);
158
159  private static final class InactiveComparisonChain extends ComparisonChain {
160    final int result;
161
162    InactiveComparisonChain(int result) {
163      this.result = result;
164    }
165
166    @Override
167    public ComparisonChain compare(Comparable<?> left, Comparable<?> right) {
168      return this;
169    }
170
171    @Override
172    public <T extends @Nullable Object> ComparisonChain compare(
173        @ParametricNullness T left, @ParametricNullness T right, Comparator<T> comparator) {
174      return this;
175    }
176
177    @Override
178    public ComparisonChain compare(int left, int right) {
179      return this;
180    }
181
182    @Override
183    public ComparisonChain compare(long left, long right) {
184      return this;
185    }
186
187    @Override
188    public ComparisonChain compare(float left, float right) {
189      return this;
190    }
191
192    @Override
193    public ComparisonChain compare(double left, double right) {
194      return this;
195    }
196
197    @Override
198    public ComparisonChain compareTrueFirst(boolean left, boolean right) {
199      return this;
200    }
201
202    @Override
203    public ComparisonChain compareFalseFirst(boolean left, boolean right) {
204      return this;
205    }
206
207    @Override
208    public int result() {
209      return result;
210    }
211  }
212
213  /**
214   * Compares two comparable objects as specified by {@link Comparable#compareTo}, <i>if</i> the
215   * result of this comparison chain has not already been determined.
216   *
217   * <p>This method is declared to accept any 2 {@code Comparable} objects, even if they are not <a
218   * href="https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html">mutually
219   * comparable</a>. If you pass objects that are not mutually comparable, this method may throw an
220   * exception. (The reason for this decision is lost to time, but the reason <i>might</i> be that
221   * we wanted to support legacy classes that implement the raw type {@code Comparable} (instead of
222   * implementing {@code Comparable<Foo>}) without producing warnings. If so, we would prefer today
223   * to produce warnings in that case, and we may change this method to do so in the future. Support
224   * for raw {@code Comparable} types in Guava in general is tracked as <a
225   * href="https://github.com/google/guava/issues/989">#989</a>.)
226   *
227   * @throws ClassCastException if the parameters are not mutually comparable
228   */
229  public abstract ComparisonChain compare(Comparable<?> left, Comparable<?> right);
230
231  /**
232   * Compares two objects using a comparator, <i>if</i> the result of this comparison chain has not
233   * already been determined.
234   */
235  public abstract <T extends @Nullable Object> ComparisonChain compare(
236      @ParametricNullness T left, @ParametricNullness T right, Comparator<T> comparator);
237
238  /**
239   * Compares two {@code int} values as specified by {@link Integer#compare}, <i>if</i> the result
240   * of this comparison chain has not already been determined.
241   */
242  public abstract ComparisonChain compare(int left, int right);
243
244  /**
245   * Compares two {@code long} values as specified by {@link Long#compare}, <i>if</i> the result of
246   * this comparison chain has not already been determined.
247   */
248  public abstract ComparisonChain compare(long left, long right);
249
250  /**
251   * Compares two {@code float} values as specified by {@link Float#compare}, <i>if</i> the result
252   * of this comparison chain has not already been determined.
253   */
254  public abstract ComparisonChain compare(float left, float right);
255
256  /**
257   * Compares two {@code double} values as specified by {@link Double#compare}, <i>if</i> the result
258   * of this comparison chain has not already been determined.
259   */
260  public abstract ComparisonChain compare(double left, double right);
261
262  /**
263   * Discouraged synonym for {@link #compareFalseFirst}.
264   *
265   * @deprecated Use {@link #compareFalseFirst}; or, if the parameters passed are being either
266   *     negated or reversed, undo the negation or reversal and use {@link #compareTrueFirst}.
267   * @since 19.0
268   */
269  @InlineMe(replacement = "this.compareFalseFirst(left, right)")
270  @Deprecated
271  public final ComparisonChain compare(Boolean left, Boolean right) {
272    return compareFalseFirst(left, right);
273  }
274
275  /**
276   * Compares two {@code boolean} values, considering {@code true} to be less than {@code false},
277   * <i>if</i> the result of this comparison chain has not already been determined.
278   *
279   * <p>Java 8+ users: you can get the equivalent from {@link Booleans#trueFirst()}. For example:
280   *
281   * <pre>
282   * Comparator.comparing(Foo::isBar, {@link Booleans#trueFirst()})
283   * </pre>
284   *
285   * @since 12.0
286   */
287  public abstract ComparisonChain compareTrueFirst(boolean left, boolean right);
288
289  /**
290   * Compares two {@code boolean} values, considering {@code false} to be less than {@code true},
291   * <i>if</i> the result of this comparison chain has not already been determined.
292   *
293   * <p>Java 8+ users: you can get the equivalent from {@link Booleans#falseFirst()}. For example:
294   *
295   * <pre>
296   * Comparator.comparing(Foo::isBar, {@link Booleans#falseFirst()})
297   * </pre>
298   *
299   * @since 12.0 (present as {@code compare} since 2.0)
300   */
301  public abstract ComparisonChain compareFalseFirst(boolean left, boolean right);
302
303  /**
304   * Ends this comparison chain and returns its result: a value having the same sign as the first
305   * nonzero comparison result in the chain, or zero if every result was zero.
306   */
307  public abstract int result();
308}