• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 java.util.Arrays;
22 import org.checkerframework.checker.nullness.qual.Nullable;
23 
24 /**
25  * A class that implements {@code Comparable} without generics, such as those found in libraries
26  * that support Java 1.4 and before. Our library needs to do the bare minimum to accommodate such
27  * types, though their use may still require an explicit type parameter and/or warning suppression.
28  *
29  * @author Kevin Bourrillion
30  */
31 @SuppressWarnings({"ComparableType", "rawtypes"}) // https://github.com/google/guava/issues/989
32 @GwtCompatible
33 @ElementTypesAreNonnullByDefault
34 class LegacyComparable implements Comparable, Serializable {
35   static final LegacyComparable X = new LegacyComparable("x");
36   static final LegacyComparable Y = new LegacyComparable("y");
37   static final LegacyComparable Z = new LegacyComparable("z");
38 
39   static final Iterable<LegacyComparable> VALUES_FORWARD = Arrays.asList(X, Y, Z);
40   static final Iterable<LegacyComparable> VALUES_BACKWARD = Arrays.asList(Z, Y, X);
41 
42   private final String value;
43 
LegacyComparable(String value)44   LegacyComparable(String value) {
45     this.value = value;
46   }
47 
48   @Override
compareTo(Object object)49   public int compareTo(Object object) {
50     // This method is spec'd to throw CCE if object is of the wrong type
51     LegacyComparable that = (LegacyComparable) object;
52     return this.value.compareTo(that.value);
53   }
54 
55   @Override
equals(@ullable Object object)56   public boolean equals(@Nullable Object object) {
57     if (object instanceof LegacyComparable) {
58       LegacyComparable that = (LegacyComparable) object;
59       return this.value.equals(that.value);
60     }
61     return false;
62   }
63 
64   @Override
hashCode()65   public int hashCode() {
66     return value.hashCode();
67   }
68 
69   private static final long serialVersionUID = 0;
70 }
71