1 16 package org.apache.commons.collections; 17 18 import java.util.Collection ; 19 import java.util.Comparator ; 20 21 import org.apache.commons.collections.comparators.BooleanComparator; 22 import org.apache.commons.collections.comparators.ComparableComparator; 23 import org.apache.commons.collections.comparators.ComparatorChain; 24 import org.apache.commons.collections.comparators.NullComparator; 25 import org.apache.commons.collections.comparators.ReverseComparator; 26 import org.apache.commons.collections.comparators.TransformingComparator; 27 28 43 public class ComparatorUtils { 44 45 48 public ComparatorUtils() { 49 } 50 51 56 public static final Comparator NATURAL_COMPARATOR = ComparableComparator.getInstance(); 57 58 63 public static Comparator naturalComparator() { 64 return NATURAL_COMPARATOR; 65 } 66 67 78 public static Comparator chainedComparator(Comparator comparator1, Comparator comparator2) { 79 return chainedComparator(new Comparator [] {comparator1, comparator2}); 80 } 81 82 91 public static Comparator chainedComparator(Comparator [] comparators) { 92 ComparatorChain chain = new ComparatorChain(); 93 for (int i = 0; i < comparators.length; i++) { 94 if (comparators[i] == null) { 95 throw new NullPointerException ("Comparator cannot be null"); 96 } 97 chain.addComparator(comparators[i]); 98 } 99 return chain; 100 } 101 102 113 public static Comparator chainedComparator(Collection comparators) { 114 return chainedComparator( 115 (Comparator []) comparators.toArray(new Comparator [comparators.size()]) 116 ); 117 } 118 119 126 public static Comparator reversedComparator(Comparator comparator) { 127 if (comparator == null) { 128 comparator = NATURAL_COMPARATOR; 129 } 130 return new ReverseComparator(comparator); 131 } 132 133 145 public static Comparator booleanComparator(boolean trueFirst) { 146 return BooleanComparator.getBooleanComparator(trueFirst); 147 } 148 149 160 public static Comparator nullLowComparator(Comparator comparator) { 161 if (comparator == null) { 162 comparator = NATURAL_COMPARATOR; 163 } 164 return new NullComparator(comparator, false); 165 } 166 167 178 public static Comparator nullHighComparator(Comparator comparator) { 179 if (comparator == null) { 180 comparator = NATURAL_COMPARATOR; 181 } 182 return new NullComparator(comparator, true); 183 } 184 185 197 public static Comparator transformedComparator(Comparator comparator, Transformer transformer) { 198 if (comparator == null) { 199 comparator = NATURAL_COMPARATOR; 200 } 201 return new TransformingComparator(transformer, comparator); 202 } 203 204 214 public static Object min(Object o1, Object o2, Comparator comparator) { 215 if (comparator == null) { 216 comparator = NATURAL_COMPARATOR; 217 } 218 int c = comparator.compare(o1, o2); 219 return (c < 0) ? o1 : o2; 220 } 221 222 232 public static Object max(Object o1, Object o2, Comparator comparator) { 233 if (comparator == null) { 234 comparator = NATURAL_COMPARATOR; 235 } 236 int c = comparator.compare(o1, o2); 237 return (c > 0) ? o1 : o2; 238 } 239 240 } 241 | Popular Tags |