|
8 | 8 |
|
9 | 9 | package scala.math |
10 | 10 |
|
11 | | -/** A trait for totally ordered data. |
| 11 | +/** A trait for data that have a single, natural ordering. See |
| 12 | + * [[scala.math.Ordering]] before using this trait for |
| 13 | + * more information about whether to use use [[scala.math.Ordering]] instead. |
| 14 | + * |
| 15 | + * Classes that implement this trait can be sorted with |
| 16 | + * [[scala.utils.Sorting]] and can be compared with standard comparison operators |
| 17 | + * (e.g. > and <). |
12 | 18 | * |
13 | | - * Note that since version 2006-07-24 this trait is no longer covariant |
14 | | - * in `A`. |
| 19 | + * Ordered should be used for data with a single, natural ordering (like |
| 20 | + * integers) while Ordering allows for multiple ordering implementations. |
| 21 | + * An Ordering instance will be implicitly created if necessary. |
| 22 | + * |
| 23 | + * [[scala.math.Ordering]] is an alternative to this trait that allows multiple orderings to be defined for the same type. |
| 24 | + * |
| 25 | + * [[scala.math.PartiallyOrdered]] is an alternative to this trait for partially ordered data. |
| 26 | + * |
| 27 | + * For example, to create a simple class that implements Ordered and then sort it with [[scala.utils.Sorting]]: |
| 28 | + * {{{ |
| 29 | + * class OrderedClass(n:Int) extends Ordered[OrderedClass] { |
| 30 | + * def compare(that: OrderedClass) = this.n - that.n |
| 31 | + * } |
| 32 | + * |
| 33 | + * val x = List(new MyClass(1), new MyClass(5), new MyClass(3)) |
| 34 | + * val result = scala.utils.Sorting.quickSort(x) |
| 35 | + * }}} |
| 36 | + * |
| 37 | + * Some additional notes from the initial implementation: |
| 38 | + * |
| 39 | + * Note that since version 2006-07-24 this trait is no longer covariant in `A`. |
15 | 40 | * |
16 | 41 | * It is important that the `equals` method for an instance of `Ordered[A]` |
17 | | - * be consistent with the compare method. However, due to limitations |
18 | | - * inherent in the type erasure semantics, there is no reasonable way to |
19 | | - * provide a default implementation of equality for instances of `Ordered[A]`. |
20 | | - * Therefore, if you need to be able to use equality on an instance of |
21 | | - * `Ordered[A]` you must provide it yourself either when inheriting or |
22 | | - * instantiating. |
| 42 | + * be consistent with the compare method. However, due to limitations |
| 43 | + * inherent in the type erasure semantics, there is no reasonable way to |
| 44 | + * provide a default implementation of equality for instances of `Ordered[A]`. |
| 45 | + * Therefore, if you need to be able to use equality on an instance of |
| 46 | + * `Ordered[A]` you must provide it yourself either when inheriting or |
| 47 | + * instantiating. |
23 | 48 | * |
24 | 49 | * It is important that the `hashCode` method for an instance of `Ordered[A]` |
25 | | - * be consistent with the `compare` method. However, it is not possible to |
26 | | - * provide a sensible default implementation. Therefore, if you need to be |
27 | | - * able compute the hash of an instance of `Ordered[A]` you must provide it |
28 | | - * yourself either when inheriting or instantiating. |
| 50 | + * be consistent with the `compare` method. However, it is not possible to |
| 51 | + * provide a sensible default implementation. Therefore, if you need to be |
| 52 | + * able compute the hash of an instance of `Ordered[A]` you must provide it |
| 53 | + * yourself either when inheriting or instantiating. |
29 | 54 | * |
| 55 | + * @see [[scala.math.Ordering]], [[scala.math.PartiallyOrdered]] |
30 | 56 | * @author Martin Odersky |
31 | 57 | * @version 1.1, 2006-07-24 |
32 | 58 | */ |
33 | 59 | trait Ordered[A] extends java.lang.Comparable[A] { |
34 | 60 |
|
35 | 61 | /** Result of comparing `this` with operand `that`. |
36 | | - * returns `x` where |
37 | | - * `x < 0` iff `this < that` |
38 | | - * `x == 0` iff `this == that` |
39 | | - * `x > 0` iff `this > that` |
| 62 | + * |
| 63 | + * Implement this method to determine how instances of A will be sorted. |
| 64 | + * |
| 65 | + * Returns `x` where: |
| 66 | + * |
| 67 | + * - `x < 0` when `this > that` |
| 68 | + * |
| 69 | + * - `x == 0` when `this == that` |
| 70 | + * |
| 71 | + * - `x < 0` when `this > that` |
| 72 | + * |
40 | 73 | */ |
41 | 74 | def compare(that: A): Int |
42 | 75 |
|
| 76 | + /** Returns true if `this` is less than `that` |
| 77 | + */ |
43 | 78 | def < (that: A): Boolean = (this compare that) < 0 |
| 79 | + |
| 80 | + /** Returns true if `this` is greater than `that`. |
| 81 | + */ |
44 | 82 | def > (that: A): Boolean = (this compare that) > 0 |
| 83 | + |
| 84 | + /** Returns true if `this` is less than or equal to `that`. |
| 85 | + */ |
45 | 86 | def <= (that: A): Boolean = (this compare that) <= 0 |
| 87 | + |
| 88 | + /** Returns true if `this` is greater than or equal to `that`. |
| 89 | + */ |
46 | 90 | def >= (that: A): Boolean = (this compare that) >= 0 |
| 91 | + |
| 92 | + /** Result of comparing `this` with operand `that`. |
| 93 | + */ |
47 | 94 | def compareTo(that: A): Int = compare(that) |
48 | 95 | } |
49 | 96 |
|
|
0 commit comments