
org.testifyproject.google.common.collect.ComparisonChain Maven / Gradle / Ivy
/*
* Copyright (C) 2009 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in org.testifyproject.testifyprojectpliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org.testifyproject/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.annotations.GwtCompatible;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.primitives.Booleans;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.primitives.Ints;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.primitives.Longs;
import java.util.Comparator;
import javax.annotation.Nullable;
/**
* A utility for performing a chained org.testifyproject.testifyprojectparison statement. For example:
* {@code
*
* public int org.testifyproject.testifyprojectpareTo(Foo that) {
* return ComparisonChain.start()
* .org.testifyproject.testifyprojectpare(this.aString, that.aString)
* .org.testifyproject.testifyprojectpare(this.anInt, that.anInt)
* .org.testifyproject.testifyprojectpare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
* .result();
* }}
*
* The value of this expression will have the same sign as the first
* nonzero org.testifyproject.testifyprojectparison result in the chain, or will be zero if every
* org.testifyproject.testifyprojectparison result was zero.
*
*
Performance note: Even though the {@code ComparisonChain} caller always
* invokes its {@code org.testifyproject.testifyprojectpare} methods unconditionally, the {@code
* ComparisonChain} implementation stops calling its inputs' {@link
* Comparable#org.testifyproject.testifyprojectpareTo org.testifyproject.testifyprojectpareTo} and {@link Comparator#org.testifyproject.testifyprojectpare org.testifyproject.testifyprojectpare}
* methods as soon as one of them returns a nonzero result. This optimization is
* typically important only in the presence of expensive {@code org.testifyproject.testifyprojectpareTo} and
* {@code org.testifyproject.testifyprojectpare} implementations.
*
*
See the Guava User Guide article on
* {@code ComparisonChain}.
*
* @author Mark Davis
* @author Kevin Bourrillion
* @since 2.0
*/
@GwtCompatible
public abstract class ComparisonChain {
private ComparisonChain() {}
/**
* Begins a new chained org.testifyproject.testifyprojectparison statement. See example in the class
* documentation.
*/
public static ComparisonChain start() {
return ACTIVE;
}
private static final ComparisonChain ACTIVE = new ComparisonChain() {
@SuppressWarnings("unchecked")
@Override public ComparisonChain org.testifyproject.testifyprojectpare(
Comparable left, Comparable right) {
return classify(left.org.testifyproject.testifyprojectpareTo(right));
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(
@Nullable T left, @Nullable T right, Comparator org.testifyproject.testifyprojectparator) {
return classify(org.testifyproject.testifyprojectparator.org.testifyproject.testifyprojectpare(left, right));
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(int left, int right) {
return classify(Ints.org.testifyproject.testifyprojectpare(left, right));
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(long left, long right) {
return classify(Longs.org.testifyproject.testifyprojectpare(left, right));
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(float left, float right) {
return classify(Float.org.testifyproject.testifyprojectpare(left, right));
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(double left, double right) {
return classify(Double.org.testifyproject.testifyprojectpare(left, right));
}
@Override public ComparisonChain org.testifyproject.testifyprojectpareTrueFirst(boolean left, boolean right) {
return classify(Booleans.org.testifyproject.testifyprojectpare(right, left)); // reversed
}
@Override public ComparisonChain org.testifyproject.testifyprojectpareFalseFirst(boolean left, boolean right) {
return classify(Booleans.org.testifyproject.testifyprojectpare(left, right));
}
ComparisonChain classify(int result) {
return (result < 0) ? LESS : (result > 0) ? GREATER : ACTIVE;
}
@Override public int result() {
return 0;
}
};
private static final ComparisonChain LESS = new InactiveComparisonChain(-1);
private static final ComparisonChain GREATER = new InactiveComparisonChain(1);
private static final class InactiveComparisonChain extends ComparisonChain {
final int result;
InactiveComparisonChain(int result) {
this.result = result;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(
@Nullable Comparable left, @Nullable Comparable right) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(@Nullable T left,
@Nullable T right, @Nullable Comparator org.testifyproject.testifyprojectparator) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(int left, int right) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(long left, long right) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(float left, float right) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpare(double left, double right) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpareTrueFirst(boolean left, boolean right) {
return this;
}
@Override public ComparisonChain org.testifyproject.testifyprojectpareFalseFirst(boolean left, boolean right) {
return this;
}
@Override public int result() {
return result;
}
}
/**
* Compares two org.testifyproject.testifyprojectparable objects as specified by {@link
* Comparable#org.testifyproject.testifyprojectpareTo}, if the result of this org.testifyproject.testifyprojectparison chain
* has not already been org.testifyproject.testifyprojecttermined.
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpare(
Comparable> left, Comparable> right);
/**
* Compares two objects using a org.testifyproject.testifyprojectparator, if the result of this
* org.testifyproject.testifyprojectparison chain has not already been org.testifyproject.testifyprojecttermined.
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpare(
@Nullable T left, @Nullable T right, Comparator org.testifyproject.testifyprojectparator);
/**
* Compares two {@code int} values as specified by {@link Ints#org.testifyproject.testifyprojectpare},
* if the result of this org.testifyproject.testifyprojectparison chain has not already been
* org.testifyproject.testifyprojecttermined.
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpare(int left, int right);
/**
* Compares two {@code long} values as specified by {@link Longs#org.testifyproject.testifyprojectpare},
* if the result of this org.testifyproject.testifyprojectparison chain has not already been
* org.testifyproject.testifyprojecttermined.
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpare(long left, long right);
/**
* Compares two {@code float} values as specified by {@link
* Float#org.testifyproject.testifyprojectpare}, if the result of this org.testifyproject.testifyprojectparison chain has not
* already been org.testifyproject.testifyprojecttermined.
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpare(float left, float right);
/**
* Compares two {@code double} values as specified by {@link
* Double#org.testifyproject.testifyprojectpare}, if the result of this org.testifyproject.testifyprojectparison chain has not
* already been org.testifyproject.testifyprojecttermined.
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpare(double left, double right);
/**
* Compares two {@code boolean} values, considering {@code true} to be less
* than {@code false}, if the result of this org.testifyproject.testifyprojectparison chain has not
* already been org.testifyproject.testifyprojecttermined.
*
* @since 12.0
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpareTrueFirst(boolean left, boolean right);
/**
* Compares two {@code boolean} values, considering {@code false} to be less
* than {@code true}, if the result of this org.testifyproject.testifyprojectparison chain has not
* already been org.testifyproject.testifyprojecttermined.
*
* @since 12.0 (present as {@code org.testifyproject.testifyprojectpare} since 2.0)
*/
public abstract ComparisonChain org.testifyproject.testifyprojectpareFalseFirst(boolean left, boolean right);
/**
* Ends this org.testifyproject.testifyprojectparison chain and returns its result: a value having the
* same sign as the first nonzero org.testifyproject.testifyprojectparison result in the chain, or zero if
* every result was zero.
*/
public abstract int result();
}