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