0
|
1 package fj;
|
|
2
|
|
3 import fj.data.Array;
|
|
4 import fj.data.Either;
|
|
5 import fj.data.List;
|
|
6 import fj.data.Natural;
|
|
7 import fj.data.NonEmptyList;
|
|
8 import fj.data.Option;
|
|
9 import fj.data.Set;
|
|
10 import fj.data.Stream;
|
|
11 import fj.data.Validation;
|
|
12
|
|
13 import java.math.BigDecimal;
|
|
14 import java.math.BigInteger;
|
|
15
|
|
16 import static fj.Function.curry;
|
|
17
|
|
18 /**
|
|
19 * Tests for ordering between two objects.
|
|
20 *
|
|
21 * @version %build.number%
|
|
22 */
|
|
23 public final class Ord<A> {
|
1
|
24 private final F<A, F<A, Ordering>> f;
|
0
|
25
|
1
|
26 private Ord(final F<A, F<A, Ordering>> f) {
|
|
27 this.f = f;
|
|
28 }
|
0
|
29
|
1
|
30 /**
|
|
31 * First-class ordering.
|
|
32 *
|
|
33 * @return A function that returns an ordering for its arguments.
|
|
34 */
|
|
35 public F<A, F<A, Ordering>> compare() {
|
|
36 return f;
|
|
37 }
|
0
|
38
|
1
|
39 /**
|
|
40 * Returns an ordering for the given arguments.
|
|
41 *
|
|
42 * @param a1 An instance to compare for ordering to another.
|
|
43 * @param a2 An instance to compare for ordering to another.
|
|
44 * @return An ordering for the given arguments.
|
|
45 */
|
|
46 public Ordering compare(final A a1, final A a2) {
|
|
47 F<A, Ordering> f1 = f.f(a1);
|
|
48 return f1.f(a2);
|
|
49 }
|
|
50
|
0
|
51
|
1
|
52 /**
|
|
53 * Returns <code>true</code> if the given arguments are equal, <code>false</code> otherwise.
|
|
54 *
|
|
55 * @param a1 An instance to compare for equality to another.
|
|
56 * @param a2 An instance to compare for equality to another.
|
|
57 * @return <code>true</code> if the given arguments are equal, <code>false</code> otherwise.
|
|
58 */
|
|
59 public boolean eq(final A a1, final A a2) {
|
|
60 return compare(a1, a2) == Ordering.EQ;
|
|
61 }
|
0
|
62
|
1
|
63 /**
|
|
64 * Returns an <code>Equal</code> for this order.
|
|
65 *
|
|
66 * @return An <code>Equal</code> for this order.
|
|
67 */
|
|
68 public Equal<A> equal() {
|
|
69 return Equal.equal(curry(new F2<A, A, Boolean>() {
|
|
70 public Boolean f(final A a1, final A a2) {
|
|
71 return eq(a1, a2);
|
|
72 }
|
|
73 }));
|
|
74 }
|
0
|
75
|
1
|
76 /**
|
|
77 * Maps the given function across this ord as a contra-variant functor.
|
|
78 *
|
|
79 * @param f The function to map.
|
|
80 * @return A new ord.
|
|
81 */
|
|
82 public <B> Ord<B> comap(final F<B, A> f) {
|
|
83 return ord(F1Functions.o(F1Functions.o(F1Functions.<B, A, Ordering>andThen(f), this.f), f));
|
|
84 }
|
0
|
85
|
1
|
86 /**
|
|
87 * Returns <code>true</code> if the first given argument is less than the second given argument,
|
|
88 * <code>false</code> otherwise.
|
|
89 *
|
|
90 * @param a1 An instance to compare for ordering to another.
|
|
91 * @param a2 An instance to compare for ordering to another.
|
|
92 * @return <code>true</code> if the first given argument is less than the second given argument,
|
|
93 * <code>false</code> otherwise.
|
|
94 */
|
|
95 public boolean isLessThan(final A a1, final A a2) {
|
|
96 return compare(a1, a2) == Ordering.LT;
|
|
97 }
|
0
|
98
|
1
|
99 /**
|
|
100 * Returns <code>true</code> if the first given argument is greater than the second given
|
|
101 * argument, <code>false</code> otherwise.
|
|
102 *
|
|
103 * @param a1 An instance to compare for ordering to another.
|
|
104 * @param a2 An instance to compare for ordering to another.
|
|
105 * @return <code>true</code> if the first given argument is greater than the second given
|
|
106 * argument, <code>false</code> otherwise.
|
|
107 */
|
|
108 public boolean isGreaterThan(final A a1, final A a2) {
|
|
109 return compare(a1, a2) == Ordering.GT;
|
|
110 }
|
0
|
111
|
1
|
112 /**
|
|
113 * Returns a function that returns true if its argument is less than the argument to this method.
|
|
114 *
|
|
115 * @param a A value to compare against.
|
|
116 * @return A function that returns true if its argument is less than the argument to this method.
|
|
117 */
|
|
118 public F<A, Boolean> isLessThan(final A a) {
|
|
119 return new F<A, Boolean>() {
|
|
120 public Boolean f(final A a2) {
|
|
121 return compare(a2, a) == Ordering.LT;
|
|
122 }
|
|
123 };
|
|
124 }
|
0
|
125
|
1
|
126 /**
|
|
127 * Returns a function that returns true if its argument is greater than than the argument to this method.
|
|
128 *
|
|
129 * @param a A value to compare against.
|
|
130 * @return A function that returns true if its argument is greater than the argument to this method.
|
|
131 */
|
|
132 public F<A, Boolean> isGreaterThan(final A a) {
|
|
133 return new F<A, Boolean>() {
|
|
134 public Boolean f(final A a2) {
|
|
135 return compare(a2, a) == Ordering.GT;
|
|
136 }
|
|
137 };
|
|
138 }
|
0
|
139
|
1
|
140 /**
|
|
141 * Returns the greater of its two arguments.
|
|
142 *
|
|
143 * @param a1 A value to compare with another.
|
|
144 * @param a2 A value to compare with another.
|
|
145 * @return The greater of the two values.
|
|
146 */
|
|
147 public A max(final A a1, final A a2) {
|
|
148 return isGreaterThan(a1, a2) ? a1 : a2;
|
|
149 }
|
0
|
150
|
|
151
|
1
|
152 /**
|
|
153 * Returns the lesser of its two arguments.
|
|
154 *
|
|
155 * @param a1 A value to compare with another.
|
|
156 * @param a2 A value to compare with another.
|
|
157 * @return The lesser of the two values.
|
|
158 */
|
|
159 public A min(final A a1, final A a2) {
|
|
160 return isLessThan(a1, a2) ? a1 : a2;
|
0
|
161 }
|
1
|
162
|
|
163 /**
|
|
164 * A function that returns the greater of its two arguments.
|
|
165 */
|
|
166 public final F<A, F<A, A>> max = curry(new F2<A, A, A>() {
|
|
167 public A f(final A a, final A a1) {
|
|
168 return max(a, a1);
|
|
169 }
|
|
170 });
|
0
|
171
|
1
|
172 /**
|
|
173 * A function that returns the lesser of its two arguments.
|
|
174 */
|
|
175 public final F<A, F<A, A>> min = curry(new F2<A, A, A>() {
|
|
176 public A f(final A a, final A a1) {
|
|
177 return min(a, a1);
|
|
178 }
|
|
179 });
|
|
180
|
|
181 /**
|
|
182 * Returns an order instance that uses the given equality test and ordering function.
|
|
183 *
|
|
184 * @param f The order function.
|
|
185 * @return An order instance.
|
|
186 */
|
|
187 public static <A> Ord<A> ord(final F<A, F<A, Ordering>> f) {
|
|
188 return new Ord<A>(f);
|
0
|
189 }
|
|
190
|
1
|
191 /**
|
|
192 * An order instance for the <code>boolean</code> type.
|
|
193 */
|
|
194 public static final Ord<Boolean> booleanOrd = new Ord<Boolean>(
|
|
195 new F<Boolean, F<Boolean, Ordering>>() {
|
|
196 public F<Boolean, Ordering> f(final Boolean a1) {
|
|
197 return new F<Boolean, Ordering>() {
|
|
198 public Ordering f(final Boolean a2) {
|
|
199 final int x = a1.compareTo(a2);
|
|
200 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
201 }
|
|
202 };
|
|
203 }
|
|
204 });
|
0
|
205
|
1
|
206 /**
|
|
207 * An order instance for the <code>byte</code> type.
|
|
208 */
|
|
209 public static final Ord<Byte> byteOrd = new Ord<Byte>(
|
|
210 new F<Byte, F<Byte, Ordering>>() {
|
|
211 public F<Byte, Ordering> f(final Byte a1) {
|
|
212 return new F<Byte, Ordering>() {
|
|
213 public Ordering f(final Byte a2) {
|
|
214 final int x = a1.compareTo(a2);
|
|
215 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
216 }
|
|
217 };
|
|
218 }
|
|
219 });
|
0
|
220
|
1
|
221 /**
|
|
222 * An order instance for the <code>char</code> type.
|
|
223 */
|
|
224 public static final Ord<Character> charOrd = new Ord<Character>(
|
|
225 new F<Character, F<Character, Ordering>>() {
|
|
226 public F<Character, Ordering> f(final Character a1) {
|
|
227 return new F<Character, Ordering>() {
|
|
228 public Ordering f(final Character a2) {
|
|
229 final int x = a1.compareTo(a2);
|
|
230 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
231 }
|
|
232 };
|
|
233 }
|
|
234 });
|
0
|
235
|
1
|
236 /**
|
|
237 * An order instance for the <code>double</code> type.
|
|
238 */
|
|
239 public static final Ord<Double> doubleOrd = new Ord<Double>(
|
|
240 new F<Double, F<Double, Ordering>>() {
|
|
241 public F<Double, Ordering> f(final Double a1) {
|
|
242 return new F<Double, Ordering>() {
|
|
243 public Ordering f(final Double a2) {
|
|
244 final int x = a1.compareTo(a2);
|
|
245 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
246 }
|
|
247 };
|
|
248 }
|
|
249 });
|
0
|
250
|
1
|
251 /**
|
|
252 * An order instance for the <code>float</code> type.
|
|
253 */
|
|
254 public static final Ord<Float> floatOrd = new Ord<Float>(
|
|
255 new F<Float, F<Float, Ordering>>() {
|
|
256 public F<Float, Ordering> f(final Float a1) {
|
|
257 return new F<Float, Ordering>() {
|
|
258 public Ordering f(final Float a2) {
|
|
259 final int x = a1.compareTo(a2);
|
|
260 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
261 }
|
|
262 };
|
|
263 }
|
|
264 });
|
0
|
265
|
1
|
266 /**
|
|
267 * An order instance for the <code>int</code> type.
|
|
268 */
|
|
269 public static final Ord<Integer> intOrd = new Ord<Integer>(
|
|
270 new F<Integer, F<Integer, Ordering>>() {
|
|
271 public F<Integer, Ordering> f(final Integer a1) {
|
|
272 return new F<Integer, Ordering>() {
|
|
273 public Ordering f(final Integer a2) {
|
|
274 final int x = a1.compareTo(a2);
|
|
275 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
276 }
|
|
277 };
|
|
278 }
|
|
279 });
|
0
|
280
|
1
|
281 /**
|
|
282 * An order instance for the <code>BigInteger</code> type.
|
|
283 */
|
|
284 public static final Ord<BigInteger> bigintOrd = new Ord<BigInteger>(
|
|
285 new F<BigInteger, F<BigInteger, Ordering>>() {
|
|
286 public F<BigInteger, Ordering> f(final BigInteger a1) {
|
|
287 return new F<BigInteger, Ordering>() {
|
|
288 public Ordering f(final BigInteger a2) {
|
|
289 final int x = a1.compareTo(a2);
|
|
290 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
291 }
|
|
292 };
|
|
293 }
|
|
294 });
|
0
|
295
|
1
|
296 /**
|
|
297 * An order instance for the <code>BigDecimal</code> type.
|
|
298 */
|
|
299 public static final Ord<BigDecimal> bigdecimalOrd = new Ord<BigDecimal>(
|
|
300 new F<BigDecimal, F<BigDecimal, Ordering>>() {
|
|
301 public F<BigDecimal, Ordering> f(final BigDecimal a1) {
|
|
302 return new F<BigDecimal, Ordering>() {
|
|
303 public Ordering f(final BigDecimal a2) {
|
|
304 final int x = a1.compareTo(a2);
|
|
305 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
306 }
|
|
307 };
|
|
308 }
|
|
309 });
|
0
|
310
|
1
|
311 /**
|
|
312 * An order instance for the <code>long</code> type.
|
|
313 */
|
|
314 public static final Ord<Long> longOrd = new Ord<Long>(
|
|
315 new F<Long, F<Long, Ordering>>() {
|
|
316 public F<Long, Ordering> f(final Long a1) {
|
|
317 return new F<Long, Ordering>() {
|
|
318 public Ordering f(final Long a2) {
|
|
319 final int x = a1.compareTo(a2);
|
|
320 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
321 }
|
|
322 };
|
|
323 }
|
|
324 });
|
0
|
325
|
1
|
326 /**
|
|
327 * An order instance for the <code>short</code> type.
|
|
328 */
|
|
329 public static final Ord<Short> shortOrd = new Ord<Short>(
|
|
330 new F<Short, F<Short, Ordering>>() {
|
|
331 public F<Short, Ordering> f(final Short a1) {
|
|
332 return new F<Short, Ordering>() {
|
|
333 public Ordering f(final Short a2) {
|
|
334 final int x = a1.compareTo(a2);
|
|
335 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
336 }
|
|
337 };
|
|
338 }
|
|
339 });
|
0
|
340
|
1
|
341 /**
|
|
342 * An order instance for the {@link Ordering} type.
|
|
343 */
|
|
344 public static final Ord<Ordering> orderingOrd = new Ord<Ordering>(curry(new F2<Ordering, Ordering, Ordering>() {
|
|
345 public Ordering f(final Ordering o1, final Ordering o2) {
|
|
346 return o1 == o2 ?
|
|
347 Ordering.EQ :
|
|
348 o1 == Ordering.LT ?
|
|
349 Ordering.LT :
|
|
350 o2 == Ordering.LT ?
|
|
351 Ordering.GT :
|
|
352 o1 == Ordering.EQ ?
|
|
353 Ordering.LT :
|
|
354 Ordering.GT;
|
|
355 }
|
|
356 }));
|
0
|
357
|
1
|
358 /**
|
|
359 * An order instance for the {@link String} type.
|
|
360 */
|
|
361 public static final Ord<String> stringOrd = new Ord<String>(
|
|
362 new F<String, F<String, Ordering>>() {
|
|
363 public F<String, Ordering> f(final String a1) {
|
|
364 return new F<String, Ordering>() {
|
|
365 public Ordering f(final String a2) {
|
2
|
366 //final int x = a1.compareTo(a2);
|
|
367 final int x = a1.hashCode() - a2.hashCode();
|
1
|
368 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
369 }
|
|
370 };
|
|
371 }
|
|
372 });
|
0
|
373
|
1
|
374 /**
|
|
375 * An order instance for the {@link StringBuffer} type.
|
|
376 */
|
|
377 public static final Ord<StringBuffer> stringBufferOrd =
|
|
378 new Ord<StringBuffer>(new F<StringBuffer, F<StringBuffer, Ordering>>() {
|
|
379 public F<StringBuffer, Ordering> f(final StringBuffer a1) {
|
|
380 return new F<StringBuffer, Ordering>() {
|
|
381 public Ordering f(final StringBuffer a2) {
|
|
382 return stringOrd.compare(a1.toString(), a2.toString());
|
|
383 }
|
|
384 };
|
|
385 }
|
|
386 });
|
0
|
387
|
1
|
388 /**
|
|
389 * An order instance for the {@link StringBuffer} type.
|
|
390 */
|
|
391 public static final Ord<StringBuilder> stringBuilderOrd =
|
|
392 new Ord<StringBuilder>(new F<StringBuilder, F<StringBuilder, Ordering>>() {
|
|
393 public F<StringBuilder, Ordering> f(final StringBuilder a1) {
|
|
394 return new F<StringBuilder, Ordering>() {
|
|
395 public Ordering f(final StringBuilder a2) {
|
|
396 return stringOrd.compare(a1.toString(), a2.toString());
|
|
397 }
|
|
398 };
|
|
399 }
|
|
400 });
|
0
|
401
|
1
|
402 /**
|
|
403 * An order instance for the {@link Option} type.
|
|
404 *
|
|
405 * @param oa Order across the element of the option.
|
|
406 * @return An order instance for the {@link Option} type.
|
|
407 */
|
|
408 public static <A> Ord<Option<A>> optionOrd(final Ord<A> oa) {
|
|
409 return new Ord<Option<A>>(new F<Option<A>, F<Option<A>, Ordering>>() {
|
|
410 public F<Option<A>, Ordering> f(final Option<A> o1) {
|
|
411 return new F<Option<A>, Ordering>() {
|
|
412 public Ordering f(final Option<A> o2) {
|
|
413 return o1.isNone() ?
|
|
414 o2.isNone() ?
|
|
415 Ordering.EQ :
|
|
416 Ordering.LT :
|
|
417 o2.isNone() ?
|
|
418 Ordering.GT :
|
|
419 oa.f.f(o1.some()).f(o2.some());
|
|
420 }
|
|
421 };
|
|
422 }
|
|
423 });
|
|
424 }
|
0
|
425
|
1
|
426 /**
|
|
427 * An order instance for the {@link Either} type.
|
|
428 *
|
|
429 * @param oa Order across the left side of {@link Either}.
|
|
430 * @param ob Order across the right side of {@link Either}.
|
|
431 * @return An order instance for the {@link Either} type.
|
|
432 */
|
|
433 public static <A, B> Ord<Either<A, B>> eitherOrd(final Ord<A> oa, final Ord<B> ob) {
|
|
434 return new Ord<Either<A, B>>(new F<Either<A, B>, F<Either<A, B>, Ordering>>() {
|
|
435 public F<Either<A, B>, Ordering> f(final Either<A, B> e1) {
|
|
436 return new F<Either<A, B>, Ordering>() {
|
|
437 public Ordering f(final Either<A, B> e2) {
|
|
438 return e1.isLeft() ?
|
|
439 e2.isLeft() ?
|
|
440 oa.f.f(e1.left().value()).f(e2.left().value()) :
|
|
441 Ordering.LT :
|
|
442 e2.isLeft() ?
|
|
443 Ordering.GT :
|
|
444 ob.f.f(e1.right().value()).f(e2.right().value());
|
|
445 }
|
|
446 };
|
|
447 }
|
|
448 });
|
|
449 }
|
0
|
450
|
1
|
451 /**
|
|
452 * An order instance for the {@link Validation} type.
|
|
453 *
|
|
454 * @param oa Order across the failing side of {@link Validation}.
|
|
455 * @param ob Order across the succeeding side of {@link Validation}.
|
|
456 * @return An order instance for the {@link Validation} type.
|
|
457 */
|
|
458 public static <A, B> Ord<Validation<A, B>> validationOrd(final Ord<A> oa, final Ord<B> ob) {
|
|
459 return eitherOrd(oa, ob).comap(Validation.<A, B>either());
|
|
460 }
|
0
|
461
|
1
|
462 /**
|
|
463 * An order instance for the {@link List} type.
|
|
464 *
|
|
465 * @param oa Order across the elements of the list.
|
|
466 * @return An order instance for the {@link List} type.
|
|
467 */
|
|
468 public static <A> Ord<List<A>> listOrd(final Ord<A> oa) {
|
|
469 return new Ord<List<A>>(new F<List<A>, F<List<A>, Ordering>>() {
|
|
470 public F<List<A>, Ordering> f(final List<A> l1) {
|
|
471 return new F<List<A>, Ordering>() {
|
|
472 public Ordering f(final List<A> l2) {
|
|
473 if (l1.isEmpty())
|
|
474 return l2.isEmpty() ? Ordering.EQ : Ordering.LT;
|
|
475 else if (l2.isEmpty())
|
|
476 return l1.isEmpty() ? Ordering.EQ : Ordering.GT;
|
|
477 else {
|
|
478 final Ordering c = oa.compare(l1.head(), l2.head());
|
|
479 return c == Ordering.EQ ? listOrd(oa).f.f(l1.tail()).f(l2.tail()) : c;
|
|
480 }
|
|
481 }
|
|
482 };
|
0
|
483 }
|
1
|
484 });
|
|
485 }
|
0
|
486
|
1
|
487 /**
|
|
488 * An order instance for the {@link NonEmptyList} type.
|
|
489 *
|
|
490 * @param oa Order across the elements of the non-empty list.
|
|
491 * @return An order instance for the {@link NonEmptyList} type.
|
|
492 */
|
|
493 public static <A> Ord<NonEmptyList<A>> nonEmptyListOrd(final Ord<A> oa) {
|
|
494 return listOrd(oa).comap(NonEmptyList.<A>toList_());
|
|
495 }
|
0
|
496
|
1
|
497 /**
|
|
498 * An order instance for the {@link Stream} type.
|
|
499 *
|
|
500 * @param oa Order across the elements of the stream.
|
|
501 * @return An order instance for the {@link Stream} type.
|
|
502 */
|
|
503 public static <A> Ord<Stream<A>> streamOrd(final Ord<A> oa) {
|
|
504 return new Ord<Stream<A>>(new F<Stream<A>, F<Stream<A>, Ordering>>() {
|
|
505 public F<Stream<A>, Ordering> f(final Stream<A> s1) {
|
|
506 return new F<Stream<A>, Ordering>() {
|
|
507 public Ordering f(final Stream<A> s2) {
|
|
508 if (s1.isEmpty())
|
|
509 return s2.isEmpty() ? Ordering.EQ : Ordering.LT;
|
|
510 else if (s2.isEmpty())
|
|
511 return s1.isEmpty() ? Ordering.EQ : Ordering.GT;
|
|
512 else {
|
|
513 final Ordering c = oa.compare(s1.head(), s2.head());
|
|
514 return c == Ordering.EQ ? streamOrd(oa).f.f(s1.tail()._1()).f(s2.tail()._1()) : c;
|
|
515 }
|
|
516 }
|
|
517 };
|
0
|
518 }
|
1
|
519 });
|
|
520 }
|
0
|
521
|
1
|
522 /**
|
|
523 * An order instance for the {@link Array} type.
|
|
524 *
|
|
525 * @param oa Order across the elements of the array.
|
|
526 * @return An order instance for the {@link Array} type.
|
|
527 */
|
|
528 public static <A> Ord<Array<A>> arrayOrd(final Ord<A> oa) {
|
|
529 return new Ord<Array<A>>(new F<Array<A>, F<Array<A>, Ordering>>() {
|
|
530 public F<Array<A>, Ordering> f(final Array<A> a1) {
|
|
531 return new F<Array<A>, Ordering>() {
|
|
532 public Ordering f(final Array<A> a2) {
|
|
533 int i = 0;
|
|
534 //noinspection ForLoopWithMissingComponent
|
|
535 for (; i < a1.length() && i < a2.length(); i++) {
|
|
536 final Ordering c = oa.compare(a1.get(i), a2.get(i));
|
|
537 if (c == Ordering.GT || c == Ordering.LT)
|
|
538 return c;
|
|
539 }
|
|
540 return i == a1.length() ?
|
|
541 i == a2.length() ?
|
|
542 Ordering.EQ :
|
|
543 Ordering.LT :
|
|
544 i == a1.length() ?
|
|
545 Ordering.EQ :
|
|
546 Ordering.GT;
|
|
547 }
|
|
548 };
|
0
|
549 }
|
1
|
550 });
|
|
551 }
|
0
|
552
|
1
|
553 /**
|
|
554 * An order instance for the {@link Set} type.
|
|
555 *
|
|
556 * @param oa Order across the elements of the set.
|
|
557 * @return An order instance for the {@link Set} type.
|
|
558 */
|
|
559 public static <A> Ord<Set<A>> setOrd(final Ord<A> oa) {
|
|
560 return streamOrd(oa).comap(new F<Set<A>, Stream<A>>() {
|
|
561 public Stream<A> f(final Set<A> as) {
|
|
562 return as.toStream();
|
|
563 }
|
|
564 });
|
|
565 }
|
0
|
566
|
1
|
567 /**
|
|
568 * An order instance for the {@link Unit} type.
|
|
569 */
|
|
570 public static final Ord<Unit> unitOrd = ord(curry(new F2<Unit, Unit, Ordering>() {
|
|
571 public Ordering f(final Unit u1, final Unit u2) {
|
|
572 return Ordering.EQ;
|
|
573 }
|
|
574 }));
|
0
|
575
|
1
|
576 /**
|
|
577 * An order instance for a product-1.
|
|
578 *
|
|
579 * @param oa Order across the produced type.
|
|
580 * @return An order instance for a product-1.
|
|
581 */
|
|
582 public static <A> Ord<P1<A>> p1Ord(final Ord<A> oa) {
|
|
583 return oa.comap(P1.<A>__1());
|
|
584 }
|
0
|
585
|
|
586
|
1
|
587 /**
|
|
588 * An order instance for a product-2, with the first factor considered most significant.
|
|
589 *
|
|
590 * @param oa An order instance for the first factor.
|
|
591 * @param ob An order instance for the second factor.
|
|
592 * @return An order instance for a product-2, with the first factor considered most significant.
|
|
593 */
|
|
594 public static <A, B> Ord<P2<A, B>> p2Ord(final Ord<A> oa, final Ord<B> ob) {
|
|
595 return ord(curry(new F2<P2<A, B>, P2<A, B>, Ordering>() {
|
|
596 public Ordering f(final P2<A, B> a, final P2<A, B> b) {
|
|
597 return oa.eq(a._1(), b._1()) ? ob.compare(a._2(), b._2()) : oa.compare(a._1(), b._1());
|
|
598 }
|
|
599 }));
|
|
600 }
|
0
|
601
|
1
|
602 /**
|
|
603 * An order instance for a product-3, with the first factor considered most significant.
|
|
604 *
|
|
605 * @param oa An order instance for the first factor.
|
|
606 * @param ob An order instance for the second factor.
|
|
607 * @param oc An order instance for the third factor.
|
|
608 * @return An order instance for a product-3, with the first factor considered most significant.
|
|
609 */
|
|
610 public static <A, B, C> Ord<P3<A, B, C>> p3Ord(final Ord<A> oa, final Ord<B> ob, final Ord<C> oc) {
|
|
611 return ord(curry(new F2<P3<A, B, C>, P3<A, B, C>, Ordering>() {
|
|
612 public Ordering f(final P3<A, B, C> a, final P3<A, B, C> b) {
|
|
613 return oa.eq(a._1(), b._1()) ?
|
|
614 p2Ord(ob, oc).compare(P.p(a._2(), a._3()), P.p(b._2(), b._3()))
|
|
615 : oa.compare(a._1(), b._1());
|
|
616 }
|
|
617 }));
|
|
618 }
|
0
|
619
|
1
|
620 /**
|
|
621 * An order instance for the <code>Natural</code> type.
|
|
622 */
|
|
623 public static final Ord<Natural> naturalOrd = bigintOrd.comap(Natural.bigIntegerValue);
|
0
|
624
|
|
625
|
1
|
626 /**
|
|
627 * An order instance for the <code>Comparable</code> interface.
|
|
628 *
|
|
629 * @return An order instance for the <code>Comparable</code> interface.
|
|
630 */
|
|
631 public static <A extends Comparable<A>> Ord<A> comparableOrd() {
|
|
632 return ord(new F<A, F<A, Ordering>>() {
|
|
633 public F<A, Ordering> f(final A a1) {
|
|
634 return new F<A, Ordering>() {
|
|
635 public Ordering f(final A a2) {
|
|
636 return Ordering.fromInt(a1.compareTo(a2));
|
|
637 }
|
|
638 };
|
|
639 }
|
|
640 });
|
|
641 }
|
0
|
642
|
1
|
643 /**
|
|
644 * An order instance that uses {@link Object#hashCode()} for computing the order and equality,
|
|
645 * thus objects returning the same hashCode are considered to be equals (check {@link #hashEqualsOrd()}
|
|
646 * for an additional check on {@link Object#equals(Object)}).
|
|
647 *
|
|
648 * @return An order instance that is based on {@link Object#hashCode()}.
|
|
649 * @see #hashEqualsOrd()
|
|
650 */
|
|
651 public static <A> Ord<A> hashOrd() {
|
|
652 return Ord.<A>ord(new F<A, F<A, Ordering>>() {
|
|
653 @Override
|
|
654 public F<A, Ordering> f(final A a) {
|
|
655 return new F<A, Ordering>() {
|
|
656 @Override
|
|
657 public Ordering f(final A a2) {
|
|
658 final int x = a.hashCode() - a2.hashCode();
|
|
659 return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
|
|
660 }
|
|
661 };
|
|
662 }
|
|
663 });
|
|
664 }
|
0
|
665
|
1
|
666 /**
|
|
667 * An order instance that uses {@link Object#hashCode()} and {@link Object#equals} for computing
|
|
668 * the order and equality. First the hashCode is compared, if this is equal, objects are compared
|
|
669 * using {@link Object#equals}.
|
|
670 *
|
|
671 * @return An order instance that is based on {@link Object#hashCode()} and {@link Object#equals}.
|
|
672 */
|
|
673 public static <A> Ord<A> hashEqualsOrd() {
|
|
674 return Ord.<A>ord(new F<A, F<A, Ordering>>() {
|
|
675 @Override
|
|
676 public F<A, Ordering> f(final A a) {
|
|
677 return new F<A, Ordering>() {
|
|
678 @Override
|
|
679 public Ordering f(final A a2) {
|
|
680 final int x = a.hashCode() - a2.hashCode();
|
|
681 return x < 0 ? Ordering.LT : x == 0 && a.equals(a2) ? Ordering.EQ : Ordering.GT;
|
|
682 }
|
|
683 };
|
|
684 }
|
|
685 });
|
|
686 }
|
0
|
687
|
|
688 }
|