1. /*
  2. * Copyright 2002-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;
  17. import java.util.Collection;
  18. import java.util.Comparator;
  19. import org.apache.commons.collections.comparators.BooleanComparator;
  20. import org.apache.commons.collections.comparators.ComparableComparator;
  21. import org.apache.commons.collections.comparators.ComparatorChain;
  22. import org.apache.commons.collections.comparators.NullComparator;
  23. import org.apache.commons.collections.comparators.ReverseComparator;
  24. import org.apache.commons.collections.comparators.TransformingComparator;
  25. /**
  26. * Provides convenient static utility methods for <Code>Comparator</Code>
  27. * objects.
  28. * <p>
  29. * Most of the functionality in this class can also be found in the
  30. * <code>comparators</code> package. This class merely provides a
  31. * convenient central place if you have use for more than one class
  32. * in the <code>comparators</code> subpackage.
  33. *
  34. * @since Commons Collections 2.1
  35. * @version $Revision: $ $Date: $
  36. *
  37. * @author Paul Jack
  38. * @author Stephen Colebourne
  39. */
  40. public class ComparatorUtils {
  41. /**
  42. * ComparatorUtils should not normally be instantiated.
  43. */
  44. public ComparatorUtils() {
  45. }
  46. /**
  47. * Comparator for natural sort order.
  48. *
  49. * @see ComparableComparator#getInstance
  50. */
  51. public static final Comparator NATURAL_COMPARATOR = ComparableComparator.getInstance();
  52. /**
  53. * Gets a comparator that uses the natural order of the objects.
  54. *
  55. * @return a comparator which uses natural order
  56. */
  57. public static Comparator naturalComparator() {
  58. return NATURAL_COMPARATOR;
  59. }
  60. /**
  61. * Gets a comparator that compares using two {@link Comparator}s.
  62. * <p>
  63. * The second comparator is used if the first comparator returns equal.
  64. *
  65. * @param comparator1 the first comparator to use, not null
  66. * @param comparator2 the first comparator to use, not null
  67. * @return a {@link ComparatorChain} formed from the two comparators
  68. * @throws NullPointerException if either comparator is null
  69. * @see ComparatorChain
  70. */
  71. public static Comparator chainedComparator(Comparator comparator1, Comparator comparator2) {
  72. return chainedComparator(new Comparator[] {comparator1, comparator2});
  73. }
  74. /**
  75. * Gets a comparator that compares using an array of {@link Comparator}s, applied
  76. * in sequence until one returns not equal or the array is exhausted.
  77. *
  78. * @param comparators the comparators to use, not null or empty or containing nulls
  79. * @return a {@link ComparatorChain} formed from the input comparators
  80. * @throws NullPointerException if comparators array is null or contains a null
  81. * @see ComparatorChain
  82. */
  83. public static Comparator chainedComparator(Comparator[] comparators) {
  84. ComparatorChain chain = new ComparatorChain();
  85. for (int i = 0; i < comparators.length; i++) {
  86. if (comparators[i] == null) {
  87. throw new NullPointerException("Comparator cannot be null");
  88. }
  89. chain.addComparator(comparators[i]);
  90. }
  91. return chain;
  92. }
  93. /**
  94. * Gets a comparator that compares using a collection of {@link Comparator}s,
  95. * applied in (default iterator) sequence until one returns not equal or the
  96. * collection is exhausted.
  97. *
  98. * @param comparators the comparators to use, not null or empty or containing nulls
  99. * @return a {@link ComparatorChain} formed from the input comparators
  100. * @throws NullPointerException if comparators collection is null or contains a null
  101. * @throws ClassCastException if the comparators collection contains the wrong object type
  102. * @see ComparatorChain
  103. */
  104. public static Comparator chainedComparator(Collection comparators) {
  105. return chainedComparator(
  106. (Comparator[]) comparators.toArray(new Comparator[comparators.size()])
  107. );
  108. }
  109. /**
  110. * Gets a comparator that reverses the order of the given comparator.
  111. *
  112. * @param comparator the comparator to reverse
  113. * @return a comparator that reverses the order of the input comparator
  114. * @see ReverseComparator
  115. */
  116. public static Comparator reversedComparator(Comparator comparator) {
  117. if (comparator == null) {
  118. comparator = NATURAL_COMPARATOR;
  119. }
  120. return new ReverseComparator(comparator);
  121. }
  122. /**
  123. * Gets a Comparator that can sort Boolean objects.
  124. * <p>
  125. * The parameter specifies whether true or false is sorted first.
  126. * <p>
  127. * The comparator throws NullPointerException if a null value is compared.
  128. *
  129. * @param trueFirst when <code>true</code>, sort
  130. * <code>true</code> {@link Boolean}s before
  131. * <code>false</code> {@link Boolean}s.
  132. * @return a comparator that sorts booleans
  133. */
  134. public static Comparator booleanComparator(boolean trueFirst) {
  135. return BooleanComparator.getBooleanComparator(trueFirst);
  136. }
  137. /**
  138. * Gets a Comparator that controls the comparison of <code>null</code> values.
  139. * <p>
  140. * The returned comparator will consider a null value to be less than
  141. * any nonnull value, and equal to any other null value. Two nonnull
  142. * values will be evaluated with the given comparator.
  143. *
  144. * @param comparator the comparator that wants to allow nulls
  145. * @return a version of that comparator that allows nulls
  146. * @see NullComparator
  147. */
  148. public static Comparator nullLowComparator(Comparator comparator) {
  149. if (comparator == null) {
  150. comparator = NATURAL_COMPARATOR;
  151. }
  152. return new NullComparator(comparator, false);
  153. }
  154. /**
  155. * Gets a Comparator that controls the comparison of <code>null</code> values.
  156. * <p>
  157. * The returned comparator will consider a null value to be greater than
  158. * any nonnull value, and equal to any other null value. Two nonnull
  159. * values will be evaluated with the given comparator.
  160. *
  161. * @param comparator the comparator that wants to allow nulls
  162. * @return a version of that comparator that allows nulls
  163. * @see NullComparator
  164. */
  165. public static Comparator nullHighComparator(Comparator comparator) {
  166. if (comparator == null) {
  167. comparator = NATURAL_COMPARATOR;
  168. }
  169. return new NullComparator(comparator, true);
  170. }
  171. /**
  172. * Gets a Comparator that passes transformed objects to the given comparator.
  173. * <p>
  174. * Objects passed to the returned comparator will first be transformed
  175. * by the given transformer before they are compared by the given
  176. * comparator.
  177. *
  178. * @param comparator the sort order to use
  179. * @param transformer the transformer to use
  180. * @return a comparator that transforms its input objects before comparing them
  181. * @see TransformingComparator
  182. */
  183. public static Comparator transformedComparator(Comparator comparator, Transformer transformer) {
  184. if (comparator == null) {
  185. comparator = NATURAL_COMPARATOR;
  186. }
  187. return new TransformingComparator(transformer, comparator);
  188. }
  189. /**
  190. * Returns the smaller of the given objects according to the given
  191. * comparator, returning the second object if the comparator
  192. * returns equal.
  193. *
  194. * @param o1 the first object to compare
  195. * @param o2 the second object to compare
  196. * @param comparator the sort order to use
  197. * @return the smaller of the two objects
  198. */
  199. public static Object min(Object o1, Object o2, Comparator comparator) {
  200. if (comparator == null) {
  201. comparator = NATURAL_COMPARATOR;
  202. }
  203. int c = comparator.compare(o1, o2);
  204. return (c < 0) ? o1 : o2;
  205. }
  206. /**
  207. * Returns the larger of the given objects according to the given
  208. * comparator, returning the second object if the comparator
  209. * returns equal.
  210. *
  211. * @param o1 the first object to compare
  212. * @param o2 the second object to compare
  213. * @param comparator the sort order to use
  214. * @return the larger of the two objects
  215. */
  216. public static Object max(Object o1, Object o2, Comparator comparator) {
  217. if (comparator == null) {
  218. comparator = NATURAL_COMPARATOR;
  219. }
  220. int c = comparator.compare(o1, o2);
  221. return (c > 0) ? o1 : o2;
  222. }
  223. }