1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 2090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copyright (C) 2007 Google Inc. 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License. 6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at 7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and 14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible; 20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkNotNull; 21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable; 23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collections; 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** An ordering that uses the natural order of the values. */ 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible(serializable = true) 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@SuppressWarnings("unchecked") // TODO: the right way to explain this?? 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonfinal class NaturalOrdering 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson extends Ordering<Comparable> implements Serializable { 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static final NaturalOrdering INSTANCE = new NaturalOrdering(); 32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int compare(Comparable left, Comparable right) { 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(right); // left null is caught later 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (left == right) { 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return 0; 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // we're permitted to throw CCE 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int result = left.compareTo(right); 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return result; 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // TODO: the right way to explain this?? 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public <S extends Comparable> Ordering<S> reverse() { 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Ordering) ReverseNaturalOrdering.INSTANCE; 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Override to remove a level of indirection from inner loop 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // TODO: the right way to explain this?? 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int binarySearch( 52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<? extends Comparable> sortedList, Comparable key) { 53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Collections.binarySearch((List) sortedList, key); 54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Override to remove a level of indirection from inner loop 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public <E extends Comparable> List<E> sortedCopy( 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<E> iterable) { 59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<E> list = Lists.newArrayList(iterable); 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collections.sort(list); 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return list; 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // preserving singleton-ness gives equals()/hashCode() for free 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Object readResolve() { 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return INSTANCE; 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "Ordering.natural()"; 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private NaturalOrdering() {} 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 77