Home | History | Annotate | Download | only in collect
      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 import java.util.Iterator;
     25 
     26 /** An ordering that uses the reverse of the natural order of the values. */
     27 @GwtCompatible(serializable = true)
     28 @SuppressWarnings("unchecked") // TODO(kevinb): the right way to explain this??
     29 final class ReverseNaturalOrdering
     30     extends Ordering<Comparable> implements Serializable {
     31   static final ReverseNaturalOrdering INSTANCE = new ReverseNaturalOrdering();
     32 
     33   @Override public int compare(Comparable left, Comparable right) {
     34     checkNotNull(left); // right null is caught later
     35     if (left == right) {
     36       return 0;
     37     }
     38 
     39     return right.compareTo(left);
     40   }
     41 
     42   @Override public <S extends Comparable> Ordering<S> reverse() {
     43     return Ordering.natural();
     44   }
     45 
     46   // Override the min/max methods to "hoist" delegation outside loops
     47 
     48   @Override public <E extends Comparable> E min(E a, E b) {
     49     return NaturalOrdering.INSTANCE.max(a, b);
     50   }
     51 
     52   @Override public <E extends Comparable> E min(E a, E b, E c, E... rest) {
     53     return NaturalOrdering.INSTANCE.max(a, b, c, rest);
     54   }
     55 
     56   @Override public <E extends Comparable> E min(Iterator<E> iterator) {
     57     return NaturalOrdering.INSTANCE.max(iterator);
     58   }
     59 
     60   @Override public <E extends Comparable> E min(Iterable<E> iterable) {
     61     return NaturalOrdering.INSTANCE.max(iterable);
     62   }
     63 
     64   @Override public <E extends Comparable> E max(E a, E b) {
     65     return NaturalOrdering.INSTANCE.min(a, b);
     66   }
     67 
     68   @Override public <E extends Comparable> E max(E a, E b, E c, E... rest) {
     69     return NaturalOrdering.INSTANCE.min(a, b, c, rest);
     70   }
     71 
     72   @Override public <E extends Comparable> E max(Iterator<E> iterator) {
     73     return NaturalOrdering.INSTANCE.min(iterator);
     74   }
     75 
     76   @Override public <E extends Comparable> E max(Iterable<E> iterable) {
     77     return NaturalOrdering.INSTANCE.min(iterable);
     78   }
     79 
     80   // preserving singleton-ness gives equals()/hashCode() for free
     81   private Object readResolve() {
     82     return INSTANCE;
     83   }
     84 
     85   @Override public String toString() {
     86     return "Ordering.natural().reverse()";
     87   }
     88 
     89   private ReverseNaturalOrdering() {}
     90 
     91   private static final long serialVersionUID = 0;
     92 }
     93