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 import javax.annotation.Nullable;
     27 
     28 /** An ordering that uses the reverse of a given order. */
     29 @GwtCompatible(serializable = true)
     30 final class ReverseOrdering<T> extends Ordering<T> implements Serializable {
     31   final Ordering<? super T> forwardOrder;
     32 
     33   ReverseOrdering(Ordering<? super T> forwardOrder) {
     34     this.forwardOrder = checkNotNull(forwardOrder);
     35   }
     36 
     37   @Override public int compare(T a, T b) {
     38     return forwardOrder.compare(b, a);
     39   }
     40 
     41   @SuppressWarnings("unchecked") // how to explain?
     42   @Override public <S extends T> Ordering<S> reverse() {
     43     return (Ordering<S>) forwardOrder;
     44   }
     45 
     46   // Override the min/max methods to "hoist" delegation outside loops
     47 
     48   @Override public <E extends T> E min(E a, E b) {
     49     return forwardOrder.max(a, b);
     50   }
     51 
     52   @Override public <E extends T> E min(E a, E b, E c, E... rest) {
     53     return forwardOrder.max(a, b, c, rest);
     54   }
     55 
     56   @Override public <E extends T> E min(Iterator<E> iterator) {
     57     return forwardOrder.max(iterator);
     58   }
     59 
     60   @Override public <E extends T> E min(Iterable<E> iterable) {
     61     return forwardOrder.max(iterable);
     62   }
     63 
     64   @Override public <E extends T> E max(E a, E b) {
     65     return forwardOrder.min(a, b);
     66   }
     67 
     68   @Override public <E extends T> E max(E a, E b, E c, E... rest) {
     69     return forwardOrder.min(a, b, c, rest);
     70   }
     71 
     72   @Override public <E extends T> E max(Iterator<E> iterator) {
     73     return forwardOrder.min(iterator);
     74   }
     75 
     76   @Override public <E extends T> E max(Iterable<E> iterable) {
     77     return forwardOrder.min(iterable);
     78   }
     79 
     80   @Override public int hashCode() {
     81     return -forwardOrder.hashCode();
     82   }
     83 
     84   @Override public boolean equals(@Nullable Object object) {
     85     if (object == this) {
     86       return true;
     87     }
     88     if (object instanceof ReverseOrdering) {
     89       ReverseOrdering<?> that = (ReverseOrdering<?>) object;
     90       return this.forwardOrder.equals(that.forwardOrder);
     91     }
     92     return false;
     93   }
     94 
     95   @Override public String toString() {
     96     return forwardOrder + ".reverse()";
     97   }
     98 
     99   private static final long serialVersionUID = 0;
    100 }
    101