• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.annotations.GwtCompatible;
20 import java.io.Serializable;
21 import javax.annotation.CheckForNull;
22 import org.checkerframework.checker.nullness.qual.Nullable;
23 
24 /** An ordering that treats {@code null} as greater than all other values. */
25 @GwtCompatible(serializable = true)
26 @ElementTypesAreNonnullByDefault
27 final class NullsLastOrdering<T extends @Nullable Object> extends Ordering<@Nullable T>
28     implements Serializable {
29   final Ordering<? super T> ordering;
30 
NullsLastOrdering(Ordering<? super T> ordering)31   NullsLastOrdering(Ordering<? super T> ordering) {
32     this.ordering = ordering;
33   }
34 
35   @Override
compare(@heckForNull T left, @CheckForNull T right)36   public int compare(@CheckForNull T left, @CheckForNull T right) {
37     if (left == right) {
38       return 0;
39     }
40     if (left == null) {
41       return LEFT_IS_GREATER;
42     }
43     if (right == null) {
44       return RIGHT_IS_GREATER;
45     }
46     return ordering.compare(left, right);
47   }
48 
49   @Override
50   @SuppressWarnings("nullness") // should be safe, but not sure if we can avoid the warning
reverse()51   public <S extends @Nullable T> Ordering<S> reverse() {
52     // ordering.reverse() might be optimized, so let it do its thing
53     return ordering.reverse().nullsFirst();
54   }
55 
56   @Override
nullsFirst()57   public <S extends T> Ordering<@Nullable S> nullsFirst() {
58     return ordering.<S>nullsFirst();
59   }
60 
61   @SuppressWarnings("unchecked") // still need the right way to explain this
62   @Override
nullsLast()63   public <S extends T> Ordering<@Nullable S> nullsLast() {
64     return (Ordering<@Nullable S>) this;
65   }
66 
67   @Override
equals(@heckForNull Object object)68   public boolean equals(@CheckForNull Object object) {
69     if (object == this) {
70       return true;
71     }
72     if (object instanceof NullsLastOrdering) {
73       NullsLastOrdering<?> that = (NullsLastOrdering<?>) object;
74       return this.ordering.equals(that.ordering);
75     }
76     return false;
77   }
78 
79   @Override
hashCode()80   public int hashCode() {
81     return ordering.hashCode() ^ -921210296; // meaningless
82   }
83 
84   @Override
toString()85   public String toString() {
86     return ordering + ".nullsLast()";
87   }
88 
89   private static final long serialVersionUID = 0;
90 }
91