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 Google Inc.
*
* 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 com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.concurrent.atomic.AtomicInteger;
/**
* A comparator with added methods to support common functions. For example:
*
{@code
*
* if (Ordering.from(comparator).reverse().isOrdered(list)) { ... }}
*
*
The {@link #from(Comparator)} method returns the equivalent {@code
* Ordering} instance for a pre-existing comparator. You can also skip the
* comparator step and extend {@code Ordering} directly:
{@code
*
* Ordering byLengthOrdering = new Ordering() {
* public int compare(String left, String right) {
* return Ints.compare(left.length(), right.length());
* }
* };}
*
* 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.
*
* @author Jesse Wilson
* @author Kevin Bourrillion
* @since 2 (imported from Google Collections Library)
*/
@GwtCompatible
public abstract class Ordering implements Comparator {
// Static factories
/**
* 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.
*/
@GwtCompatible(serializable = true)
@SuppressWarnings("unchecked") // TODO: the right way to explain this??
public static Ordering natural() {
return (Ordering) NaturalOrdering.INSTANCE;
}
/**
* Returns an ordering for a pre-existing {@code comparator}. Note
* that if the comparator is not pre-existing, and you don't require
* serialization, you can subclass {@code Ordering} and implement its
* {@link #compare(Object, Object) compare} method instead.
*
* @param comparator the comparator that defines the order
*/
@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 an {@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})
*/
@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
*/
@GwtCompatible(serializable = true)
public static Ordering explicit(
T leastValue, T... remainingValuesInOrder) {
return explicit(Lists.asList(leastValue, remainingValuesInOrder));
}
/**
* Exception thrown by a {@link Ordering#explicit(List)} or {@link
* Ordering#explicit(Object, Object[])} comparator when comparing a value
* outside the set of values it can compare. Extending {@link
* ClassCastException} may seem odd, but it is required.
*/
// TODO: consider making this exception type public. or consider getting rid
// of it.
@VisibleForTesting
static class IncomparableValueException extends ClassCastException {
final Object value;
IncomparableValueException(Object value) {
super("Cannot compare value: " + value);
this.value = value;
}
private static final long serialVersionUID = 0;
}
/**
* Returns an arbitrary ordering over all objects, for which {@code compare(a,
* b) == 0} implies {@code a == b} (identity equality). There is no meaning
* whatsoever to the order imposed, but it is constant for the life of the VM.
*
*
Because the ordering is identity-based, it is not "consistent with
* {@link Object#equals(Object)}" as defined by {@link Comparator}. Use
* caution when building a {@link SortedSet} or {@link SortedMap} from it, as
* the resulting collection will not behave exactly according to spec.
*
*
This ordering is not serializable, as its implementation relies on
* {@link System#identityHashCode(Object)}, so its behavior cannot be
* preserved across serialization.
*
* @since 2
*/
public static Ordering