ReverseNaturalOrdering.java revision 7dd252788645e940eada959bdde927426e2531c9
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
17package com.google.common.collect;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.common.annotations.GwtCompatible;
22
23import java.io.Serializable;
24import 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")
29// TODO(kevinb): the right way to explain this??
30final class ReverseNaturalOrdering extends Ordering<Comparable> implements Serializable {
31  static final ReverseNaturalOrdering INSTANCE = new ReverseNaturalOrdering();
32
33  @Override
34  public int compare(Comparable left, Comparable right) {
35    checkNotNull(left); // right null is caught later
36    if (left == right) {
37      return 0;
38    }
39
40    return right.compareTo(left);
41  }
42
43  @Override
44  public <S extends Comparable> Ordering<S> reverse() {
45    return Ordering.natural();
46  }
47
48  // Override the min/max methods to "hoist" delegation outside loops
49
50  @Override
51  public <E extends Comparable> E min(E a, E b) {
52    return NaturalOrdering.INSTANCE.max(a, b);
53  }
54
55  @Override
56  public <E extends Comparable> E min(E a, E b, E c, E... rest) {
57    return NaturalOrdering.INSTANCE.max(a, b, c, rest);
58  }
59
60  @Override
61  public <E extends Comparable> E min(Iterator<E> iterator) {
62    return NaturalOrdering.INSTANCE.max(iterator);
63  }
64
65  @Override
66  public <E extends Comparable> E min(Iterable<E> iterable) {
67    return NaturalOrdering.INSTANCE.max(iterable);
68  }
69
70  @Override
71  public <E extends Comparable> E max(E a, E b) {
72    return NaturalOrdering.INSTANCE.min(a, b);
73  }
74
75  @Override
76  public <E extends Comparable> E max(E a, E b, E c, E... rest) {
77    return NaturalOrdering.INSTANCE.min(a, b, c, rest);
78  }
79
80  @Override
81  public <E extends Comparable> E max(Iterator<E> iterator) {
82    return NaturalOrdering.INSTANCE.min(iterator);
83  }
84
85  @Override
86  public <E extends Comparable> E max(Iterable<E> iterable) {
87    return NaturalOrdering.INSTANCE.min(iterable);
88  }
89
90  // preserving singleton-ness gives equals()/hashCode() for free
91  private Object readResolve() {
92    return INSTANCE;
93  }
94
95  @Override
96  public String toString() {
97    return "Ordering.natural().reverse()";
98  }
99
100  private ReverseNaturalOrdering() {}
101
102  private static final long serialVersionUID = 0;
103}
104