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. * Reverses the order of another comparator by reversing the arguments
  21. * to its {@link #compare(Object, Object) compare} method.
  22. *
  23. * @since Commons Collections 2.0
  24. * @version $Revision: 1.20 $ $Date: 2004/05/16 11:49:24 $
  25. *
  26. * @author Henri Yandell
  27. * @author Michael A. Smith
  28. *
  29. * @see java.util.Collections#reverseOrder()
  30. */
  31. public class ReverseComparator implements Comparator, Serializable {
  32. /** Serialization version from Collections 2.0. */
  33. private static final long serialVersionUID = 2858887242028539265L;
  34. /** The comparator being decorated. */
  35. private Comparator comparator;
  36. //-----------------------------------------------------------------------
  37. /**
  38. * Creates a comparator that compares objects based on the inverse of their
  39. * natural ordering. Using this Constructor will create a ReverseComparator
  40. * that is functionally identical to the Comparator returned by
  41. * java.util.Collections.<b>reverseOrder()</b>.
  42. *
  43. * @see java.util.Collections#reverseOrder()
  44. */
  45. public ReverseComparator() {
  46. this(null);
  47. }
  48. /**
  49. * Creates a comparator that inverts the comparison
  50. * of the given comparator. If you pass in <code>null</code>,
  51. * the ReverseComparator defaults to reversing the
  52. * natural order, as per
  53. * {@link java.util.Collections#reverseOrder()}</b>.
  54. *
  55. * @param comparator Comparator to reverse
  56. */
  57. public ReverseComparator(Comparator comparator) {
  58. if(comparator != null) {
  59. this.comparator = comparator;
  60. } else {
  61. this.comparator = ComparableComparator.getInstance();
  62. }
  63. }
  64. //-----------------------------------------------------------------------
  65. /**
  66. * Compares two objects in reverse order.
  67. *
  68. * @param obj1 the first object to compare
  69. * @param obj2 the second object to compare
  70. * @return negative if obj1 is less, positive if greater, zero if equal
  71. */
  72. public int compare(Object obj1, Object obj2) {
  73. return comparator.compare(obj2, obj1);
  74. }
  75. //-----------------------------------------------------------------------
  76. /**
  77. * Implement a hash code for this comparator that is consistent with
  78. * {@link #equals(Object) equals}.
  79. *
  80. * @return a suitable hash code
  81. * @since Commons Collections 3.0
  82. */
  83. public int hashCode() {
  84. return "ReverseComparator".hashCode() ^ comparator.hashCode();
  85. }
  86. /**
  87. * Returns <code>true</code> iff <i>that</i> Object is
  88. * is a {@link Comparator} whose ordering is known to be
  89. * equivalent to mine.
  90. * <p>
  91. * This implementation returns <code>true</code>
  92. * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
  93. * equals <code>this.getClass()</code>, and the underlying
  94. * comparators are equal.
  95. * Subclasses may want to override this behavior to remain consistent
  96. * with the {@link Comparator#equals(Object) equals} contract.
  97. *
  98. * @param object the object to compare to
  99. * @return true if equal
  100. * @since Commons Collections 3.0
  101. */
  102. public boolean equals(Object object) {
  103. if(this == object) {
  104. return true;
  105. } else if(null == object) {
  106. return false;
  107. } else if(object.getClass().equals(this.getClass())) {
  108. ReverseComparator thatrc = (ReverseComparator)object;
  109. return comparator.equals(thatrc.comparator);
  110. } else {
  111. return false;
  112. }
  113. }
  114. }