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 com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
/**
* 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
*/
@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) NATURAL_ORDER;
}
private static final NaturalOrdering NATURAL_ORDER = new NaturalOrdering();
// Package-private for GWT serialization.
@GwtCompatible(serializable = true)
@SuppressWarnings("unchecked") // TODO: the right way to explain this??
static class NaturalOrdering extends Ordering
implements Serializable {
public int compare(Comparable left, Comparable right) {
checkNotNull(right); // left null is caught later
if (left == right) {
return 0;
}
@SuppressWarnings("unchecked") // we're permitted to throw CCE
int result = left.compareTo(right);
return result;
}
// preserving singleton-ness gives equals()/hashCode() for free
private Object readResolve() {
return NATURAL_ORDER;
}
@Override public String toString() {
return "Ordering.natural()";
}
private static final long serialVersionUID = 0;
}
/**
* 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
*/
public static Ordering from(final Comparator comparator) {
return (comparator instanceof Ordering)
? (Ordering) comparator
: new ComparatorOrdering(comparator);
}
/**
* Simply returns its argument.
*
* @deprecated no need to use this
*/
@Deprecated public static Ordering from(Ordering ordering) {
return checkNotNull(ordering);
}
private static final class ComparatorOrdering extends Ordering
implements Serializable {
final Comparator comparator;
ComparatorOrdering(Comparator comparator) {
this.comparator = checkNotNull(comparator);
}
public int compare(T a, T b) {
return comparator.compare(a, b);
}
@Override public boolean equals(@Nullable Object object) {
if (object == this) {
return true;
}
if (object instanceof ComparatorOrdering) {
ComparatorOrdering> that = (ComparatorOrdering>) object;
return this.comparator.equals(that.comparator);
}
return false;
}
@Override public int hashCode() {
return comparator.hashCode();
}
@Override public String toString() {
return comparator.toString();
}
private static final long serialVersionUID = 0;
}
/**
* 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})
*/
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
*/
public static Ordering explicit(
T leastValue, T... remainingValuesInOrder) {
return explicit(Lists.asList(leastValue, remainingValuesInOrder));
}
/**
* This method has been renamed to {@link Ordering#explicit(List)}. It
* will not be present in the final release of 1.0.
*
* @deprecated use {@link Ordering#explicit(List)}
*/
@Deprecated public static Ordering givenOrder(List valuesInOrder) {
return explicit(valuesInOrder);
}
/**
* This method has been renamed to {@link Ordering#explicit(Object,Object[])}.
* It will not be present in the final release of 1.0.
*
* @deprecated use {@link Ordering#explicit(Object,Object[])}
*/
@Deprecated public static Ordering givenOrder(
T leastValue, T... remainingValuesInOrder) {
return explicit(leastValue, remainingValuesInOrder);
}
private static class ExplicitOrdering extends Ordering
implements Serializable {
final ImmutableMap rankMap;
ExplicitOrdering(List valuesInOrder) {
rankMap = buildRankMap(valuesInOrder);
}
public int compare(T left, T right) {
return rank(left) - rank(right); // safe because both are nonnegative
}
int rank(T value) {
Integer rank = rankMap.get(value);
if (rank == null) {
throw new IncomparableValueException(value);
}
return rank;
}
static ImmutableMap buildRankMap(List valuesInOrder) {
ImmutableMap.Builder builder = ImmutableMap.builder();
int rank = 0;
for (T value : valuesInOrder) {
builder.put(value, rank++);
}
return builder.build();
}
@Override public boolean equals(@Nullable Object object) {
if (object instanceof ExplicitOrdering) {
ExplicitOrdering> that = (ExplicitOrdering>) object;
return this.rankMap.equals(that.rankMap);
}
return false;
}
@Override public int hashCode() {
return rankMap.hashCode();
}
@Override public String toString() {
return "Ordering.explicit(" + rankMap.keySet() + ")";
}
private static final long serialVersionUID = 0;
}
/**
* 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 ordering that compares objects by the natural ordering of their
* string representations as returned by {@code toString()}. It does not
* support null values.
*
*
The comparator is serializable.
*/
public static Ordering