1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  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. package org.apache.commons.collections.comparators;
  17. import java.io.Serializable;
  18. import java.util.Comparator;
  19. /**
  20. * A {@link Comparator Comparator} that compares
  21. * {@link Comparable Comparable} objects.
  22. * <p />
  23. * This Comparator is useful, for example,
  24. * for enforcing the natural order in custom implementations
  25. * of SortedSet and SortedMap.
  26. * <p />
  27. * Note: In the 2.0 and 2.1 releases of Commons Collections,
  28. * this class would throw a {@link ClassCastException} if
  29. * either of the arguments to {@link #compare(Object, Object) compare}
  30. * were <code>null</code>, not {@link Comparable Comparable},
  31. * or for which {@link Comparable#compareTo(Object) compareTo} gave
  32. * inconsistent results. This is no longer the case. See
  33. * {@link #compare(Object, Object) compare} for details.
  34. *
  35. * @since Commons Collections 2.0
  36. * @version $Revision: 1.15 $ $Date: 2004/05/15 13:24:11 $
  37. *
  38. * @author Henri Yandell
  39. *
  40. * @see java.util.Collections#reverseOrder()
  41. */
  42. public class ComparableComparator implements Comparator, Serializable {
  43. /** Serialization version. */
  44. private static final long serialVersionUID=-291439688585137865L;
  45. /** The singleton instance. */
  46. private static final ComparableComparator instance = new ComparableComparator();
  47. //-----------------------------------------------------------------------
  48. /**
  49. * Gets the singleton instance of a ComparableComparator.
  50. * <p>
  51. * Developers are encouraged to use the comparator returned from this method
  52. * instead of constructing a new instance to reduce allocation and GC overhead
  53. * when multiple comparable comparators may be used in the same VM.
  54. *
  55. * @return the singleton ComparableComparator
  56. */
  57. public static ComparableComparator getInstance() {
  58. return instance;
  59. }
  60. //-----------------------------------------------------------------------
  61. /**
  62. * Constructor whose use should be avoided.
  63. * <p>
  64. * Please use the {@link #getInstance()} method whenever possible.
  65. */
  66. public ComparableComparator() {
  67. super();
  68. }
  69. //-----------------------------------------------------------------------
  70. /**
  71. * Compare the two {@link Comparable Comparable} arguments.
  72. * This method is equivalent to:
  73. * <pre>((Comparable)obj1).compareTo(obj2)</pre>
  74. *
  75. * @param obj1 the first object to compare
  76. * @param obj2 the second object to compare
  77. * @return negative if obj1 is less, positive if greater, zero if equal
  78. * @throws NullPointerException when <i>obj1</i> is <code>null</code>,
  79. * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
  80. * @throws ClassCastException when <i>obj1</i> is not a <code>Comparable</code>,
  81. * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
  82. */
  83. public int compare(Object obj1, Object obj2) {
  84. return ((Comparable)obj1).compareTo(obj2);
  85. }
  86. //-----------------------------------------------------------------------
  87. /**
  88. * Implement a hash code for this comparator that is consistent with
  89. * {@link #equals(Object) equals}.
  90. *
  91. * @return a hash code for this comparator.
  92. * @since Commons Collections 3.0
  93. */
  94. public int hashCode() {
  95. return "ComparableComparator".hashCode();
  96. }
  97. /**
  98. * Returns <code>true</code> iff <i>that</i> Object is
  99. * is a {@link Comparator Comparator} whose ordering is
  100. * known to be equivalent to mine.
  101. * <p>
  102. * This implementation returns <code>true</code>
  103. * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
  104. * equals <code>this.getClass()</code>.
  105. * Subclasses may want to override this behavior to remain consistent
  106. * with the {@link Comparator#equals(Object)} contract.
  107. *
  108. * @param object the object to compare with
  109. * @return true if equal
  110. * @since Commons Collections 3.0
  111. */
  112. public boolean equals(Object object) {
  113. return (this == object) ||
  114. ((null != object) && (object.getClass().equals(this.getClass())));
  115. }
  116. }