Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/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.google.common.collect;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
/**
* A comparator, with additional methods to support common operations. This is an "enriched" version
* of {@code Comparator} for pre-Java-8 users, in the same sense that {@link FluentIterable} is an
* enriched {@link Iterable} for pre-Java-8 users.
*
*
Three types of methods
*
* Like other fluent types, there are three types of methods present: methods for acquiring,
* chaining, and using.
*
*
Acquiring
*
*
The common ways to get an instance of {@code Ordering} are:
*
*
*
Subclass it and implement {@link #compare} instead of implementing {@link Comparator}
* directly
*
Pass a pre-existing {@link Comparator} instance to {@link #from(Comparator)}
*
Use the natural ordering, {@link Ordering#natural}
*
*
*
Chaining
*
*
Then you can use the chaining methods to get an altered version of that {@code
* Ordering}, including:
*
*
*
{@link #reverse}
*
{@link #compound(Comparator)}
*
{@link #onResultOf(Function)}
*
{@link #nullsFirst} / {@link #nullsLast}
*
*
*
Using
*
*
Finally, use the resulting {@code Ordering} anywhere a {@link Comparator} is required, or use
* any of its special operations, such as:
*
*
*
{@link #immutableSortedCopy}
*
{@link #isOrdered} / {@link #isStrictlyOrdered}
*
{@link #min} / {@link #max}
*
*
*
Understanding complex orderings
*
*
Complex chained orderings like the following example can be challenging to understand.
*
*
*
* Note that each chaining method returns a new ordering instance which is backed by the previous
* instance, but has the chance to act on values before handing off to that backing instance.
* As a result, it usually helps to read chained ordering expressions backwards. For example,
* when {@code compare} is called on the above ordering:
*
*
*
First, if only one {@code Foo} is null, that null value is treated as greater
*
Next, non-null {@code Foo} values are passed to {@code getBarFunction} (we will be
* comparing {@code Bar} values from now on)
*
Next, if only one {@code Bar} is null, that null value is treated as lesser
*
Finally, natural ordering is used (i.e. the result of {@code Bar.compareTo(Bar)} is
* returned)
*
*
*
Alas, {@link #reverse} is a little different. As you read backwards through a chain and
* encounter a call to {@code reverse}, continue working backwards until a result is determined, and
* then reverse that result.
*
*
Additional notes
*
*
Except as noted, the orderings returned by the factory methods of this class are serializable
* if and only if the provided instances that back them are. For example, if {@code ordering} and
* {@code function} can themselves be serialized, then {@code ordering.onResultOf(function)} can as
* well.
*
*
For Java 8 users
*
*
If you are using Java 8, this class is now obsolete. Most of its functionality is now provided
* by {@link java.util.stream.Stream Stream} and by {@link Comparator} itself, and the rest can now
* be found as static methods in our new {@link Comparators} class. See each method below for
* further instructions. Whenever possible, you should change any references of type {@code
* Ordering} to be of type {@code Comparator} instead. However, at this time we have no plan to
* deprecate this class.
*
*
Many replacements involve adopting {@code Stream}, and these changes can sometimes make your
* code verbose. Whenever following this advice, you should check whether {@code Stream} could be
* adopted more comprehensively in your code; the end result may be quite a bit simpler.
*
*
See also
*
*
See the Guava User Guide article on {@code Ordering}.
*
* @author Jesse Wilson
* @author Kevin Bourrillion
* @since 2.0
*/
@GwtCompatible
public abstract class Ordering implements Comparator {
// Natural order
/**
* Returns a serializable ordering that uses the natural order of the values. The ordering throws
* a {@link NullPointerException} when passed a null parameter.
*
*
The type specification is {@code }, instead of the technically correct
* {@code >}, to support legacy types from before Java 5.
*
*
Java 8 users: use {@link Comparator#naturalOrder} instead.
*/
@GwtCompatible(serializable = true)
@SuppressWarnings("unchecked") // TODO(kevinb): right way to explain this??
public static Ordering natural() {
return (Ordering) NaturalOrdering.INSTANCE;
}
// Static factories
/**
* Returns an ordering based on an existing comparator instance. Note that it is
* unnecessary to create a new anonymous inner class implementing {@code Comparator} just
* to pass it in here. Instead, simply subclass {@code Ordering} and implement its {@code compare}
* method directly.
*
*
Java 8 users: this class is now obsolete as explained in the class documentation, so
* there is no need to use this method.
*
* @param comparator the comparator that defines the order
* @return comparator itself if it is already an {@code Ordering}; otherwise an ordering that
* wraps that comparator
*/
@GwtCompatible(serializable = true)
public static Ordering from(Comparator comparator) {
return (comparator instanceof Ordering)
? (Ordering) comparator
: new ComparatorOrdering(comparator);
}
/**
* Simply returns its argument.
*
* @deprecated no need to use this
*/
@GwtCompatible(serializable = true)
@Deprecated
public static Ordering from(Ordering ordering) {
return checkNotNull(ordering);
}
/**
* Returns an ordering that compares objects according to the order in which they appear in the
* given list. Only objects present in the list (according to {@link Object#equals}) may be
* compared. This comparator imposes a "partial ordering" over the type {@code T}. Subsequent
* changes to the {@code valuesInOrder} list will have no effect on the returned comparator. Null
* values in the list are not supported.
*
*
The returned comparator throws a {@link ClassCastException} when it receives an input
* parameter that isn't among the provided values.
*
*
The generated comparator is serializable if all the provided values are serializable.
*
* @param valuesInOrder the values that the returned comparator will be able to compare, in the
* order the comparator should induce
* @return the comparator described above
* @throws NullPointerException if any of the provided values is null
* @throws IllegalArgumentException if {@code valuesInOrder} contains any duplicate values
* (according to {@link Object#equals})
*/
// TODO(kevinb): provide replacement
@GwtCompatible(serializable = true)
public static Ordering explicit(List valuesInOrder) {
return new ExplicitOrdering(valuesInOrder);
}
/**
* Returns an ordering that compares objects according to the order in which they are given to
* this method. Only objects present in the argument list (according to {@link Object#equals}) may
* be compared. This comparator imposes a "partial ordering" over the type {@code T}. Null values
* in the argument list are not supported.
*
*
The returned comparator throws a {@link ClassCastException} when it receives an input
* parameter that isn't among the provided values.
*
*
The generated comparator is serializable if all the provided values are serializable.
*
* @param leastValue the value which the returned comparator should consider the "least" of all
* values
* @param remainingValuesInOrder the rest of the values that the returned comparator will be able
* to compare, in the order the comparator should follow
* @return the comparator described above
* @throws NullPointerException if any of the provided values is null
* @throws IllegalArgumentException if any duplicate values (according to {@link
* Object#equals(Object)}) are present among the method arguments
*/
// TODO(kevinb): provide replacement
@GwtCompatible(serializable = true)
public static Ordering explicit(T leastValue, T... remainingValuesInOrder) {
return explicit(Lists.asList(leastValue, remainingValuesInOrder));
}
// Ordering