1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.common.annotations.GwtCompatible; 22 23 import java.io.Serializable; 24 25 /** An ordering that uses the natural order of the values. */ 26 @GwtCompatible(serializable = true) 27 @SuppressWarnings("unchecked") // TODO(kevinb): the right way to explain this?? 28 final class NaturalOrdering 29 extends Ordering<Comparable> implements Serializable { 30 static final NaturalOrdering INSTANCE = new NaturalOrdering(); 31 compare(Comparable left, Comparable right)32 @Override public int compare(Comparable left, Comparable right) { 33 checkNotNull(left); // for GWT 34 checkNotNull(right); 35 return left.compareTo(right); 36 } 37 reverse()38 @Override public <S extends Comparable> Ordering<S> reverse() { 39 return (Ordering<S>) ReverseNaturalOrdering.INSTANCE; 40 } 41 42 // preserving singleton-ness gives equals()/hashCode() for free readResolve()43 private Object readResolve() { 44 return INSTANCE; 45 } 46 toString()47 @Override public String toString() { 48 return "Ordering.natural()"; 49 } 50 NaturalOrdering()51 private NaturalOrdering() {} 52 53 private static final long serialVersionUID = 0; 54 } 55