| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package jaggregate; |
| 7 | |
|
| 8 | |
import java.util.Comparator; |
| 9 | |
import static java.lang.String.*; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
public class Comparables { |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | 1 | protected Comparables() { |
| 24 | 1 | throw new UnsupportedOperationException(); |
| 25 | |
} |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public static <E extends Comparable<? super E>> UnaryPredicate<E> lessThan( |
| 39 | |
E comparand ) { |
| 40 | |
|
| 41 | 11 | return new LessThan<E>().bindSecond( comparand ); |
| 42 | |
} |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static <E extends Comparable<? super E>> UnaryPredicate<E> lessThanOrEqualTo( |
| 56 | |
E comparand ) { |
| 57 | |
|
| 58 | 7 | return new LessThanOrEqualTo<E>().bindSecond( comparand ); |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public static <E extends Comparable<? super E>> UnaryPredicate<E> greaterThan( |
| 73 | |
E comparand ) { |
| 74 | |
|
| 75 | 7 | return new GreaterThan<E>().bindSecond( comparand ); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public static <E extends Comparable<? super E>> |
| 90 | |
UnaryPredicate<E> greaterThanOrEqualTo( E comparand ) { |
| 91 | 7 | return new GreaterThanOrEqualTo<E>().bindSecond( comparand ); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public static <E extends Comparable<? super E>> UnaryPredicate<E> between( E minimum, |
| 108 | |
E maximum ) { |
| 109 | |
|
| 110 | 6 | return new Between<E>( minimum, maximum ); |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public static <E extends Comparable<? super E>> BinaryFunction<E, E, E> max() { |
| 123 | 4 | return new Max<E>(); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public static <E extends Comparable<? super E>> BinaryFunction<E, E, E> min() { |
| 136 | 4 | return new Min<E>(); |
| 137 | |
} |
| 138 | |
|
| 139 | |
private abstract static |
| 140 | |
class NaturalOrderingPredicate<E extends Comparable<? super E>> |
| 141 | |
extends BinaryPredicate<E, E> { |
| 142 | |
|
| 143 | |
protected static final String LESS_THAN_FORMAT = "a value less than <%1$s>"; |
| 144 | |
protected static final String GREATER_THAN_FORMAT = "a value greater than <%1$s>"; |
| 145 | |
protected static final String LESS_THAN_OR_EQUAL_TO_FORMAT = |
| 146 | |
"a value less than or equal to <%1$s>"; |
| 147 | |
protected static final String GREATER_THAN_OR_EQUAL_TO_FORMAT = |
| 148 | |
"a value greater than or equal to <%1$s>"; |
| 149 | |
|
| 150 | |
protected final Comparator<E> comparator; |
| 151 | |
|
| 152 | 32 | protected NaturalOrderingPredicate() { |
| 153 | 32 | comparator = Comparing.<E>byNaturalOrdering(); |
| 154 | 32 | } |
| 155 | |
} |
| 156 | |
|
| 157 | 40 | private static class LessThan<E extends Comparable<? super E>> |
| 158 | |
extends NaturalOrderingPredicate<E> { |
| 159 | |
|
| 160 | |
public boolean matches( E first, E second ) { |
| 161 | 18 | return comparator.compare( first, second ) < 0; |
| 162 | |
} |
| 163 | |
|
| 164 | |
@Override |
| 165 | |
protected String getSecondBoundDescription() { |
| 166 | 1 | return LESS_THAN_FORMAT; |
| 167 | |
} |
| 168 | |
} |
| 169 | |
|
| 170 | 20 | private static class LessThanOrEqualTo<E extends Comparable<? super E>> |
| 171 | |
extends NaturalOrderingPredicate<E> { |
| 172 | |
|
| 173 | |
public boolean matches( E first, E second ) { |
| 174 | 6 | return comparator.compare( first, second ) <= 0; |
| 175 | |
} |
| 176 | |
|
| 177 | |
@Override |
| 178 | |
protected String getSecondBoundDescription() { |
| 179 | 1 | return LESS_THAN_OR_EQUAL_TO_FORMAT; |
| 180 | |
} |
| 181 | |
} |
| 182 | |
|
| 183 | 20 | private static class GreaterThan<E extends Comparable<? super E>> |
| 184 | |
extends NaturalOrderingPredicate<E> { |
| 185 | |
|
| 186 | |
public boolean matches( E first, E second ) { |
| 187 | 6 | return comparator.compare( first, second ) > 0; |
| 188 | |
} |
| 189 | |
|
| 190 | |
@Override |
| 191 | |
protected String getSecondBoundDescription() { |
| 192 | 1 | return GREATER_THAN_FORMAT; |
| 193 | |
} |
| 194 | |
} |
| 195 | |
|
| 196 | 20 | private static class GreaterThanOrEqualTo<E extends Comparable<? super E>> |
| 197 | |
extends NaturalOrderingPredicate<E> { |
| 198 | |
|
| 199 | |
public boolean matches( E first, E second ) { |
| 200 | 6 | return comparator.compare( first, second ) >= 0; |
| 201 | |
} |
| 202 | |
|
| 203 | |
@Override |
| 204 | |
protected String getSecondBoundDescription() { |
| 205 | 1 | return GREATER_THAN_OR_EQUAL_TO_FORMAT; |
| 206 | |
} |
| 207 | |
} |
| 208 | |
|
| 209 | 8 | private static class Between<E extends Comparable<? super E>> |
| 210 | |
extends UnaryPredicate<E> { |
| 211 | |
|
| 212 | |
private final E first; |
| 213 | |
private final E second; |
| 214 | |
|
| 215 | 6 | Between( E first, E second ) { |
| 216 | 6 | this.first = first; |
| 217 | 6 | this.second = second; |
| 218 | 6 | } |
| 219 | |
|
| 220 | |
public boolean matches( E target ) { |
| 221 | 8 | Comparator<E> comparator = Comparing.<E>byNaturalOrdering(); |
| 222 | 8 | return comparator.compare( first, target ) <= 0 |
| 223 | |
&& comparator.compare( target, second ) <= 0; |
| 224 | |
} |
| 225 | |
|
| 226 | |
@Override |
| 227 | |
public String describe() { |
| 228 | 1 | return format( "a value between <%1$s> and <%2$s>", first, second ); |
| 229 | |
} |
| 230 | |
} |
| 231 | |
|
| 232 | 12 | private static class Max<E extends Comparable<? super E>> |
| 233 | |
extends BinaryFunction<E, E, E> { |
| 234 | |
|
| 235 | 4 | private final Comparator<E> comparator = Comparing.<E>byNaturalOrdering(); |
| 236 | |
|
| 237 | |
public E evaluate( E first, E second ) { |
| 238 | 4 | return comparator.compare( first, second ) >= 0 ? first : second; |
| 239 | |
} |
| 240 | |
} |
| 241 | |
|
| 242 | 12 | private static class Min<E extends Comparable<? super E>> |
| 243 | |
extends BinaryFunction<E, E, E> { |
| 244 | |
|
| 245 | 4 | private final Comparator<E> comparator = Comparing.<E>byNaturalOrdering(); |
| 246 | |
|
| 247 | |
public E evaluate( E first, E second ) { |
| 248 | 4 | return comparator.compare( first, second ) <= 0 ? first : second; |
| 249 | |
} |
| 250 | |
} |
| 251 | |
} |