Coverage Summary for Class: NaturalOrdering (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| NaturalOrdering | 100% (1/1) | 75% (6/8) | 68.8% (11/16) |
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 import java.io.Serializable; 23 import org.checkerframework.checker.nullness.qual.Nullable; 24 25 /** An ordering that uses the natural order of the values. */ 26 @GwtCompatible(serializable = true) 27 @SuppressWarnings({"unchecked", "rawtypes"}) // TODO(kevinb): the right way to explain this?? 28 final class NaturalOrdering extends Ordering<Comparable> implements Serializable { 29 static final NaturalOrdering INSTANCE = new NaturalOrdering(); 30 31 private transient @Nullable Ordering<Comparable> nullsFirst; 32 private transient @Nullable Ordering<Comparable> nullsLast; 33 34 @Override 35 public int compare(Comparable left, Comparable right) { 36 checkNotNull(left); // for GWT 37 checkNotNull(right); 38 return left.compareTo(right); 39 } 40 41 @Override 42 public <S extends Comparable> Ordering<S> nullsFirst() { 43 Ordering<Comparable> result = nullsFirst; 44 if (result == null) { 45 result = nullsFirst = super.nullsFirst(); 46 } 47 return (Ordering<S>) result; 48 } 49 50 @Override 51 public <S extends Comparable> Ordering<S> nullsLast() { 52 Ordering<Comparable> result = nullsLast; 53 if (result == null) { 54 result = nullsLast = super.nullsLast(); 55 } 56 return (Ordering<S>) result; 57 } 58 59 @Override 60 public <S extends Comparable> Ordering<S> reverse() { 61 return (Ordering<S>) ReverseNaturalOrdering.INSTANCE; 62 } 63 64 // preserving singleton-ness gives equals()/hashCode() for free 65 private Object readResolve() { 66 return INSTANCE; 67 } 68 69 @Override 70 public String toString() { 71 return "Ordering.natural()"; 72 } 73 74 private NaturalOrdering() {} 75 76 private static final long serialVersionUID = 0; 77 }