All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.google.common.collect.ComparisonChain Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (C) 2009 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in com.fitburpliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.com.fitbur/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 com.fitbur.google.com.fitburmon.collect;

import com.fitbur.google.com.fitburmon.annotations.GwtCompatible;
import com.fitbur.google.com.fitburmon.primitives.Booleans;
import com.fitbur.google.com.fitburmon.primitives.Ints;
import com.fitbur.google.com.fitburmon.primitives.Longs;

import java.util.Comparator;

import javax.annotation.Nullable;

/**
 * A utility for performing a chained com.fitburparison statement. For example:
 * 
   {@code
 *
 *   public int com.fitburpareTo(Foo that) {
 *     return ComparisonChain.start()
 *         .com.fitburpare(this.aString, that.aString)
 *         .com.fitburpare(this.anInt, that.anInt)
 *         .com.fitburpare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
 *         .result();
 *   }}
* *

The value of this expression will have the same sign as the first * nonzero com.fitburparison result in the chain, or will be zero if every * com.fitburparison result was zero. * *

Performance note: Even though the {@code ComparisonChain} caller always * invokes its {@code com.fitburpare} methods unconditionally, the {@code * ComparisonChain} implementation stops calling its inputs' {@link * Comparable#com.fitburpareTo com.fitburpareTo} and {@link Comparator#com.fitburpare com.fitburpare} * methods as soon as one of them returns a nonzero result. This optimization is * typically important only in the presence of expensive {@code com.fitburpareTo} and * {@code com.fitburpare} 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 com.fitburparison 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 com.fitburpare( Comparable left, Comparable right) { return classify(left.com.fitburpareTo(right)); } @Override public ComparisonChain com.fitburpare( @Nullable T left, @Nullable T right, Comparator com.fitburparator) { return classify(com.fitburparator.com.fitburpare(left, right)); } @Override public ComparisonChain com.fitburpare(int left, int right) { return classify(Ints.com.fitburpare(left, right)); } @Override public ComparisonChain com.fitburpare(long left, long right) { return classify(Longs.com.fitburpare(left, right)); } @Override public ComparisonChain com.fitburpare(float left, float right) { return classify(Float.com.fitburpare(left, right)); } @Override public ComparisonChain com.fitburpare(double left, double right) { return classify(Double.com.fitburpare(left, right)); } @Override public ComparisonChain com.fitburpareTrueFirst(boolean left, boolean right) { return classify(Booleans.com.fitburpare(right, left)); // reversed } @Override public ComparisonChain com.fitburpareFalseFirst(boolean left, boolean right) { return classify(Booleans.com.fitburpare(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 com.fitburpare( @Nullable Comparable left, @Nullable Comparable right) { return this; } @Override public ComparisonChain com.fitburpare(@Nullable T left, @Nullable T right, @Nullable Comparator com.fitburparator) { return this; } @Override public ComparisonChain com.fitburpare(int left, int right) { return this; } @Override public ComparisonChain com.fitburpare(long left, long right) { return this; } @Override public ComparisonChain com.fitburpare(float left, float right) { return this; } @Override public ComparisonChain com.fitburpare(double left, double right) { return this; } @Override public ComparisonChain com.fitburpareTrueFirst(boolean left, boolean right) { return this; } @Override public ComparisonChain com.fitburpareFalseFirst(boolean left, boolean right) { return this; } @Override public int result() { return result; } } /** * Compares two com.fitburparable objects as specified by {@link * Comparable#com.fitburpareTo}, if the result of this com.fitburparison chain * has not already been com.fitburtermined. */ public abstract ComparisonChain com.fitburpare( Comparable left, Comparable right); /** * Compares two objects using a com.fitburparator, if the result of this * com.fitburparison chain has not already been com.fitburtermined. */ public abstract ComparisonChain com.fitburpare( @Nullable T left, @Nullable T right, Comparator com.fitburparator); /** * Compares two {@code int} values as specified by {@link Ints#com.fitburpare}, * if the result of this com.fitburparison chain has not already been * com.fitburtermined. */ public abstract ComparisonChain com.fitburpare(int left, int right); /** * Compares two {@code long} values as specified by {@link Longs#com.fitburpare}, * if the result of this com.fitburparison chain has not already been * com.fitburtermined. */ public abstract ComparisonChain com.fitburpare(long left, long right); /** * Compares two {@code float} values as specified by {@link * Float#com.fitburpare}, if the result of this com.fitburparison chain has not * already been com.fitburtermined. */ public abstract ComparisonChain com.fitburpare(float left, float right); /** * Compares two {@code double} values as specified by {@link * Double#com.fitburpare}, if the result of this com.fitburparison chain has not * already been com.fitburtermined. */ public abstract ComparisonChain com.fitburpare(double left, double right); /** * Compares two {@code boolean} values, considering {@code true} to be less * than {@code false}, if the result of this com.fitburparison chain has not * already been com.fitburtermined. * * @since 12.0 */ public abstract ComparisonChain com.fitburpareTrueFirst(boolean left, boolean right); /** * Compares two {@code boolean} values, considering {@code false} to be less * than {@code true}, if the result of this com.fitburparison chain has not * already been com.fitburtermined. * * @since 12.0 (present as {@code com.fitburpare} since 2.0) */ public abstract ComparisonChain com.fitburpareFalseFirst(boolean left, boolean right); /** * Ends this com.fitburparison chain and returns its result: a value having the * same sign as the first nonzero com.fitburparison result in the chain, or zero if * every result was zero. */ public abstract int result(); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy