github.com/searKing/golang/go@v1.2.74/util/comparable.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package util 6 7 /** 8 * This interface imposes a total ordering on the objects of each class that 9 * implements it. This ordering is referred to as the class's <i>natural 10 * ordering</i>, and the class's {@code compareTo} method is referred to as 11 * its <i>natural comparison method</i>.<p> 12 * 13 * Lists (and arrays) of objects that implement this interface can be sorted 14 * automatically by {@link Collections#sort(List) Collections.sort} (and 15 * {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this 16 * interface can be used as keys in a {@linkplain SortedMap sorted map} or as 17 * elements in a {@linkplain SortedSet sorted set}, without the need to 18 * specify a {@linkplain Comparator comparator}.<p> 19 * 20 * The natural ordering for a class {@code C} is said to be <i>consistent 21 * with equals</i> if and only if {@code e1.compareTo(e2) == 0} has 22 * the same boolean value as {@code e1.equals(e2)} for every 23 * {@code e1} and {@code e2} of class {@code C}. Note that {@code null} 24 * is not an instance of any class, and {@code e.compareTo(null)} should 25 * throw a {@code NullPointerException} even though {@code e.equals(null)} 26 * returns {@code false}.<p> 27 * 28 * It is strongly recommended (though not required) that natural orderings be 29 * consistent with equals. This is so because sorted sets (and sorted maps) 30 * without explicit comparators behave "strangely" when they are used with 31 * elements (or keys) whose natural ordering is inconsistent with equals. In 32 * particular, such a sorted set (or sorted map) violates the general contract 33 * for set (or map), which is defined in terms of the {@code equals} 34 * method.<p> 35 * 36 * For example, if one adds two keys {@code a} and {@code b} such that 37 * {@code (!a.equals(b) && a.compareTo(b) == 0)} to a sorted 38 * set that does not use an explicit comparator, the second {@code add} 39 * operation returns false (and the size of the sorted set does not increase) 40 * because {@code a} and {@code b} are equivalent from the sorted set's 41 * perspective.<p> 42 * 43 * Virtually all Java core classes that implement {@code Comparable} have natural 44 * orderings that are consistent with equals. One exception is 45 * {@code java.math.BigDecimal}, whose natural ordering equates 46 * {@code BigDecimal} objects with equal values and different precisions 47 * (such as 4.0 and 4.00).<p> 48 * 49 * For the mathematically inclined, the <i>relation</i> that defines 50 * the natural ordering on a given class C is:<pre>{@code 51 * {(x, y) such that x.compareTo(y) <= 0}. 52 * }</pre> The <i>quotient</i> for this total order is: <pre>{@code 53 * {(x, y) such that x.compareTo(y) == 0}. 54 * }</pre> 55 * 56 * It follows immediately from the contract for {@code compareTo} that the 57 * quotient is an <i>equivalence relation</i> on {@code C}, and that the 58 * natural ordering is a <i>total order</i> on {@code C}. When we say that a 59 * class's natural ordering is <i>consistent with equals</i>, we mean that the 60 * quotient for the natural ordering is the equivalence relation defined by 61 * the class's {@link Object#equals(Object) equals(Object)} method:<pre> 62 * {(x, y) such that x.equals(y)}. </pre><p> 63 * 64 * This interface is a member of the 65 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> 66 * Java Collections Framework</a>. 67 * 68 * @param <T> the type of objects that this object may be compared to 69 * 70 * @author Josh Bloch 71 * @see java.util.Comparator 72 * @since 1.2 73 */ 74 type Comparable interface { 75 /** 76 * Compares this object with the specified object for order. Returns a 77 * negative integer, zero, or a positive integer as this object is less 78 * than, equal to, or greater than the specified object. 79 * 80 * <p>The implementor must ensure 81 * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} 82 * for all {@code x} and {@code y}. (This 83 * implies that {@code x.compareTo(y)} must throw an exception iff 84 * {@code y.compareTo(x)} throws an exception.) 85 * 86 * <p>The implementor must also ensure that the relation is transitive: 87 * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies 88 * {@code x.compareTo(z) > 0}. 89 * 90 * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0} 91 * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for 92 * all {@code z}. 93 * 94 * <p>It is strongly recommended, but <i>not</i> strictly required that 95 * {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any 96 * class that implements the {@code Comparable} interface and violates 97 * this condition should clearly indicate this fact. The recommended 98 * language is "Note: this class has a natural ordering that is 99 * inconsistent with equals." 100 * 101 * <p>In the foregoing description, the notation 102 * {@code sgn(}<i>expression</i>{@code )} designates the mathematical 103 * <i>signum</i> function, which is defined to return one of {@code -1}, 104 * {@code 0}, or {@code 1} according to whether the value of 105 * <i>expression</i> is negative, zero, or positive, respectively. 106 * 107 * @param o the object to be compared. 108 * @return a negative integer, zero, or a positive integer as this object 109 * is less than, equal to, or greater than the specified object. 110 * 111 * @throws NullPointerException if the specified object is null 112 * @throws ClassCastException if the specified object's type prevents it 113 * from being compared to this object. 114 */ 115 CompareTo(o interface{}) int 116 } 117 118 type ComparableFunc func(o interface{}) int 119 120 func (f ComparableFunc) CompareTo(o interface{}) int { 121 return f(o) 122 }