1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.lang; 19 20 /** 21 * This interface should be implemented by all classes that wish to define a 22 * <em>natural order</em> of their instances. 23 * {@link java.util.Collections#sort} and {@code java.util.Arrays#sort} can then 24 * be used to automatically sort lists of classes that implement this interface. 25 * <p> 26 * The order rule must be both transitive (if {@code x.compareTo(y) < 0} and 27 * {@code y.compareTo(z) < 0}, then {@code x.compareTo(z) < 0} must hold) and 28 * invertible (the sign of the result of x.compareTo(y) must be equal to the 29 * negation of the sign of the result of y.compareTo(x) for all combinations of 30 * x and y). 31 * <p> 32 * In addition, it is recommended (but not required) that if and only if the 33 * result of x.compareTo(y) is zero, then the result of x.equals(y) should be 34 * {@code true}. 35 */ 36 public interface Comparable<T> { 37 38 /** 39 * Compares this object to the specified object to determine their relative 40 * order. 41 * 42 * @param another 43 * the object to compare to this instance. 44 * @return a negative integer if this instance is less than {@code another}; 45 * a positive integer if this instance is greater than 46 * {@code another}; 0 if this instance has the same order as 47 * {@code another}. 48 * @throws ClassCastException 49 * if {@code another} cannot be converted into something 50 * comparable to {@code this} instance. 51 */ compareTo(T another)52 int compareTo(T another); 53 } 54