ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/CommonOps.java
Revision: 1.8
Committed: Sun Jan 18 20:17:32 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +1 -0 lines
Log Message:
exactly one blank line before and after package statements

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 package extra166y;
8
9 import jsr166y.*;
10 import static extra166y.Ops.*;
11 import java.util.*;
12
13 /**
14 * A collection of static factory methods providing commonly useful
15 * implementations of operations.
16 */
17 public class CommonOps {
18 private CommonOps() {} // disable construction
19
20 /**
21 * Returns a Comparator for Comparable objects.
22 */
23 public static <T extends Comparable<? super T>> Comparator<T>
24 naturalComparator(Class<T> type) {
25 return new Comparator<T>() {
26 public int compare(T a, T b) { return a.compareTo(b); }
27 };
28 }
29
30 /**
31 * Returns a reducer returning the maximum of two Comparable
32 * elements, treating null as less than any non-null element.
33 */
34 public static <T extends Comparable<? super T>> Reducer<T>
35 naturalMaxReducer(Class<T> type) {
36 return new Reducer<T>() {
37 public T op(T a, T b) {
38 return (a != null &&
39 (b == null || a.compareTo(b) >= 0)) ? a : b;
40 }
41 };
42 }
43
44 /**
45 * Returns a reducer returning the minimum of two Comparable
46 * elements, treating null as greater than any non-null element.
47 */
48 public static <T extends Comparable<? super T>> Reducer<T>
49 naturalMinReducer(Class<T> type) {
50 return new Reducer<T>() {
51 public T op(T a, T b) {
52 return (a != null &&
53 (b == null || a.compareTo(b) <= 0)) ? a : b;
54 }
55 };
56 }
57
58 /**
59 * Returns a reducer returning the maximum of two elements, using
60 * the given comparator, and treating null as less than any
61 * non-null element.
62 */
63 public static <T> Reducer<T> maxReducer
64 (final Comparator<? super T> comparator) {
65 return new Reducer<T>() {
66 public T op(T a, T b) {
67 return (a != null &&
68 (b == null || comparator.compare(a, b) >= 0)) ? a : b;
69 }
70 };
71 }
72
73 /**
74 * Returns a reducer returning the minimum of two elements, using
75 * the given comparator, and treating null as greater than any
76 * non-null element.
77 */
78 public static <T> Reducer<T> minReducer
79 (final Comparator<? super T> comparator) {
80 return new Reducer<T>() {
81 public T op(T a, T b) {
82 return (a != null &&
83 (b == null || comparator.compare(a, b) <= 0)) ? a : b;
84 }
85 };
86 }
87
88 /**
89 * Returns a Comparator that casts its arguments as Comparable on
90 * each comparison, throwing ClassCastException on failure.
91 */
92 public static Comparator<Object> castedComparator() {
93 return (Comparator<Object>)(RawComparator.cmp);
94 }
95 static final class RawComparator implements Comparator {
96 static final RawComparator cmp = new RawComparator();
97 public int compare(Object a, Object b) {
98 return ((Comparable)a).compareTo((Comparable)b);
99 }
100 }
101
102 /**
103 * Returns a reducer returning maximum of two values, or
104 * {@code null} if both arguments are null, and that casts
105 * its arguments as Comparable on each comparison, throwing
106 * ClassCastException on failure.
107 */
108 public static Reducer<Object> castedMaxReducer() {
109 return (Reducer<Object>)RawMaxReducer.max;
110 }
111 static final class RawMaxReducer implements Reducer {
112 static final RawMaxReducer max = new RawMaxReducer();
113 public Object op(Object a, Object b) {
114 return (a != null &&
115 (b == null ||
116 ((Comparable)a).compareTo((Comparable)b) >= 0)) ? a : b;
117 }
118 }
119
120 /**
121 * Returns a reducer returning minimum of two values, or
122 * {@code null} if both arguments are null, and that casts
123 * its arguments as Comparable on each comparison, throwing
124 * ClassCastException on failure.
125 */
126 public static Reducer<Object> castedMinReducer() {
127 return (Reducer<Object>)RawMinReducer.min;
128 }
129 static final class RawMinReducer implements Reducer {
130 static final RawMinReducer min = new RawMinReducer();
131 public Object op(Object a, Object b) {
132 return (a != null &&
133 (b == null ||
134 ((Comparable)a).compareTo((Comparable)b) <= 0)) ? a : b;
135 }
136 }
137
138
139 /**
140 * Returns a comparator for doubles relying on natural ordering.
141 */
142 public static DoubleComparator naturalDoubleComparator() {
143 return NaturalDoubleComparator.comparator;
144 }
145 static final class NaturalDoubleComparator
146 implements DoubleComparator {
147 static final NaturalDoubleComparator comparator = new
148 NaturalDoubleComparator();
149 public int compare(double a, double b) {
150 return Double.compare(a, b);
151 }
152 }
153
154 /**
155 * Returns a reducer returning the maximum of two double elements,
156 * using natural comparator.
157 */
158 public static DoubleReducer naturalDoubleMaxReducer() {
159 return NaturalDoubleMaxReducer.max;
160 }
161
162 static final class NaturalDoubleMaxReducer
163 implements DoubleReducer {
164 public static final NaturalDoubleMaxReducer max =
165 new NaturalDoubleMaxReducer();
166 public double op(double a, double b) { return Math.max(a, b); }
167 }
168
169 /**
170 * Returns a reducer returning the minimum of two double elements,
171 * using natural comparator.
172 */
173 public static DoubleReducer naturalDoubleMinReducer() {
174 return NaturalDoubleMinReducer.min;
175 }
176 static final class NaturalDoubleMinReducer
177 implements DoubleReducer {
178 public static final NaturalDoubleMinReducer min =
179 new NaturalDoubleMinReducer();
180 public double op(double a, double b) { return Math.min(a, b); }
181 }
182
183 /**
184 * Returns a reducer returning the maximum of two double elements,
185 * using the given comparator.
186 */
187 public static DoubleReducer doubleMaxReducer
188 (final DoubleComparator comparator) {
189 return new DoubleReducer() {
190 public double op(double a, double b) {
191 return (comparator.compare(a, b) >= 0) ? a : b;
192 }
193 };
194 }
195
196 /**
197 * Returns a reducer returning the minimum of two double elements,
198 * using the given comparator.
199 */
200 public static DoubleReducer doubleMinReducer
201 (final DoubleComparator comparator) {
202 return new DoubleReducer() {
203 public double op(double a, double b) {
204 return (comparator.compare(a, b) <= 0) ? a : b;
205 }
206 };
207 }
208
209 /**
210 * Returns a comparator for longs relying on natural ordering.
211 */
212 public static LongComparator naturalLongComparator() {
213 return NaturalLongComparator.comparator;
214 }
215 static final class NaturalLongComparator
216 implements LongComparator {
217 static final NaturalLongComparator comparator = new
218 NaturalLongComparator();
219 public int compare(long a, long b) {
220 return (a < b) ? -1 : ((a > b) ? 1 : 0);
221 }
222 }
223
224 /**
225 * Returns a reducer returning the maximum of two long elements,
226 * using natural comparator.
227 */
228 public static LongReducer naturalLongMaxReducer() {
229 return NaturalLongMaxReducer.max;
230 }
231
232 static final class NaturalLongMaxReducer
233 implements LongReducer {
234 public static final NaturalLongMaxReducer max =
235 new NaturalLongMaxReducer();
236 public long op(long a, long b) { return (a >= b) ? a : b; }
237 }
238
239 /**
240 * A reducer returning the minimum of two long elements,
241 * using natural comparator.
242 */
243 public static LongReducer naturalLongMinReducer() {
244 return NaturalLongMinReducer.min;
245 }
246 static final class NaturalLongMinReducer
247 implements LongReducer {
248 public static final NaturalLongMinReducer min =
249 new NaturalLongMinReducer();
250 public long op(long a, long b) { return (a <= b) ? a : b; }
251 }
252
253 /**
254 * Returns a reducer returning the maximum of two long elements,
255 * using the given comparator.
256 */
257 public static LongReducer longMaxReducer
258 (final LongComparator comparator) {
259 return new LongReducer() {
260 public long op(long a, long b) {
261 return (comparator.compare(a, b) >= 0) ? a : b;
262 }
263 };
264 }
265
266 /**
267 * Returns a reducer returning the minimum of two long elements,
268 * using the given comparator.
269 */
270 public static LongReducer longMinReducer
271 (final LongComparator comparator) {
272 return new LongReducer() {
273 public long op(long a, long b) {
274 return (comparator.compare(a, b) <= 0) ? a : b;
275 }
276 };
277 }
278
279 /**
280 * Returns a composite mapper that applies a second mapper to the results
281 * of applying the first one.
282 */
283 public static <T,U,V> Op<T,V> compoundOp
284 (final Op<? super T, ? extends U> first,
285 final Op<? super U, ? extends V> second) {
286 return new Op<T,V>() {
287 public final V op(T t) { return second.op(first.op(t)); }
288 };
289 }
290
291 /**
292 * Returns a composite mapper that applies a second mapper to the results
293 * of applying the first one.
294 */
295 public static <T,V> Op<T,V> compoundOp
296 (final ObjectToDouble<? super T> first,
297 final DoubleToObject<? extends V> second) {
298 return new Op<T,V>() {
299 public final V op(T t) { return second.op(first.op(t)); }
300 };
301 }
302
303 /**
304 * Returns a composite mapper that applies a second mapper to the results
305 * of applying the first one.
306 */
307 public static <T,V> Op<T,V> compoundOp
308 (final ObjectToLong<? super T> first,
309 final LongToObject<? extends V> second) {
310 return new Op<T,V>() {
311 public final V op(T t) { return second.op(first.op(t)); }
312 };
313 }
314
315 /**
316 * Returns a composite mapper that applies a second mapper to the results
317 * of applying the first one.
318 */
319 public static <T,V> DoubleToObject<V> compoundOp
320 (final DoubleToObject<? extends T> first,
321 final Op<? super T,? extends V> second) {
322 return new DoubleToObject<V>() {
323 public final V op(double t) { return second.op(first.op(t)); }
324 };
325 }
326
327 /**
328 * Returns a composite mapper that applies a second mapper to the results
329 * of applying the first one.
330 */
331 public static <T,V> LongToObject<V> compoundOp
332 (final LongToObject<? extends T> first,
333 final Op<? super T,? extends V> second) {
334 return new LongToObject<V>() {
335 public final V op(long t) { return second.op(first.op(t)); }
336 };
337 }
338
339 /**
340 * Returns a composite mapper that applies a second mapper to the results
341 * of applying the first one.
342 */
343 public static <T,U> ObjectToDouble<T> compoundOp
344 (final Op<? super T, ? extends U> first,
345 final ObjectToDouble<? super U> second) {
346 return new ObjectToDouble<T>() {
347 public final double op(T t) { return second.op(first.op(t)); }
348 };
349 }
350
351 /**
352 * Returns a composite mapper that applies a second mapper to the results
353 * of applying the first one.
354 */
355 public static <T,U> ObjectToLong<T> compoundOp
356 (final Op<? super T, ? extends U> first,
357 final ObjectToLong<? super U> second) {
358 return new ObjectToLong<T>() {
359 public final long op(T t) { return second.op(first.op(t)); }
360 };
361 }
362
363 /**
364 * Returns a composite mapper that applies a second mapper to the results
365 * of applying the first one.
366 */
367 public static <T> ObjectToDouble<T> compoundOp
368 (final ObjectToDouble<? super T> first,
369 final DoubleOp second) {
370 return new ObjectToDouble<T>() {
371 public final double op(T t) { return second.op(first.op(t)); }
372 };
373 }
374
375 /**
376 * Returns a composite mapper that applies a second mapper to the results
377 * of applying the first one.
378 */
379 public static <T> ObjectToLong<T> compoundOp
380 (final ObjectToDouble<? super T> first,
381 final DoubleToLong second) {
382 return new ObjectToLong<T>() {
383 public final long op(T t) { return second.op(first.op(t)); }
384 };
385 }
386
387 /**
388 * Returns a composite mapper that applies a second mapper to the results
389 * of applying the first one.
390 */
391 public static <T> ObjectToLong<T> compoundOp
392 (final ObjectToLong<? super T> first,
393 final LongOp second) {
394 return new ObjectToLong<T>() {
395 public final long op(T t) { return second.op(first.op(t)); }
396 };
397 }
398
399 /**
400 * Returns a composite mapper that applies a second mapper to the results
401 * of applying the first one.
402 */
403 public static <T> ObjectToDouble<T> compoundOp
404 (final ObjectToLong<? super T> first,
405 final LongToDouble second) {
406 return new ObjectToDouble<T>() {
407 public final double op(T t) { return second.op(first.op(t)); }
408 };
409 }
410
411 /**
412 * Returns a composite mapper that applies a second mapper to the results
413 * of applying the first one.
414 */
415 public static DoubleOp compoundOp
416 (final DoubleOp first,
417 final DoubleOp second) {
418 return new DoubleOp() {
419 public final double op(double t) { return second.op(first.op(t)); }
420 };
421 }
422
423 /**
424 * Returns a composite mapper that applies a second mapper to the results
425 * of applying the first one.
426 */
427 public static DoubleToLong compoundOp
428 (final DoubleOp first,
429 final DoubleToLong second) {
430 return new DoubleToLong() {
431 public final long op(double t) { return second.op(first.op(t)); }
432 };
433 }
434
435 /**
436 * Returns a composite mapper that applies a second mapper to the results
437 * of applying the first one.
438 */
439 public static DoubleToLong compoundOp
440 (final DoubleToLong first,
441 final LongOp second) {
442 return new DoubleToLong() {
443 public final long op(double t) { return second.op(first.op(t)); }
444 };
445 }
446
447 /**
448 * Returns a composite mapper that applies a second mapper to the results
449 * of applying the first one.
450 */
451 public static <T> DoubleToObject<T> compoundOp
452 (final DoubleToLong first,
453 final LongToObject<? extends T> second) {
454 return new DoubleToObject<T>() {
455 public final T op(double t) { return second.op(first.op(t)); }
456 };
457 }
458
459 /**
460 * Returns a composite mapper that applies a second mapper to the results
461 * of applying the first one.
462 */
463 public static <T> LongToObject<T> compoundOp
464 (final LongToDouble first,
465 final DoubleToObject<? extends T> second) {
466 return new LongToObject<T>() {
467 public final T op(long t) { return second.op(first.op(t)); }
468 };
469 }
470
471 /**
472 * Returns a composite mapper that applies a second mapper to the results
473 * of applying the first one.
474 */
475 public static LongToDouble compoundOp
476 (final LongOp first,
477 final LongToDouble second) {
478 return new LongToDouble() {
479 public final double op(long t) { return second.op(first.op(t)); }
480 };
481 }
482
483 /**
484 * Returns a composite mapper that applies a second mapper to the results
485 * of applying the first one.
486 */
487 public static LongToDouble compoundOp
488 (final LongToDouble first,
489 final DoubleOp second) {
490 return new LongToDouble() {
491 public final double op(long t) { return second.op(first.op(t)); }
492 };
493 }
494
495 /**
496 * Returns a composite mapper that applies a second mapper to the results
497 * of applying the first one.
498 */
499 public static <T> DoubleToObject<T> compoundOp
500 (final DoubleOp first,
501 final DoubleToObject<? extends T> second) {
502 return new DoubleToObject<T>() {
503 public final T op(double t) { return second.op(first.op(t)); }
504 };
505 }
506
507 /**
508 * Returns a composite mapper that applies a second mapper to the results
509 * of applying the first one.
510 */
511 public static <T> LongToObject<T> compoundOp
512 (final LongOp first,
513 final LongToObject<? extends T> second) {
514 return new LongToObject<T>() {
515 public final T op(long t) { return second.op(first.op(t)); }
516 };
517 }
518
519 /**
520 * Returns a composite mapper that applies a second mapper to the results
521 * of applying the first one.
522 */
523 public static <T> DoubleOp compoundOp
524 (final DoubleToObject<? extends T> first,
525 final ObjectToDouble<? super T> second) {
526 return new DoubleOp() {
527 public final double op(double t) { return second.op(first.op(t)); }
528 };
529 }
530
531 /**
532 * Returns a composite mapper that applies a second mapper to the results
533 * of applying the first one.
534 */
535 public static <T> LongToDouble compoundOp
536 (final LongToObject<? extends T> first,
537 final ObjectToDouble<? super T> second) {
538 return new LongToDouble() {
539 public final double op(long t) { return second.op(first.op(t)); }
540 };
541 }
542
543 /**
544 * Returns a composite mapper that applies a second mapper to the results
545 * of applying the first one.
546 */
547 public static <T> DoubleToLong compoundOp
548 (final DoubleToObject<? extends T> first,
549 final ObjectToLong<? super T> second) {
550 return new DoubleToLong() {
551 public final long op(double t) { return second.op(first.op(t)); }
552 };
553 }
554
555 /**
556 * Returns a composite mapper that applies a second mapper to the results
557 * of applying the first one.
558 */
559 public static <T> LongOp compoundOp
560 (final LongToObject<? extends T> first,
561 final ObjectToLong<? super T> second) {
562 return new LongOp() {
563 public final long op(long t) { return second.op(first.op(t)); }
564 };
565 }
566
567 /**
568 * Returns a composite mapper that applies a second mapper to the results
569 * of applying the first one.
570 */
571 public static LongOp compoundOp
572 (final LongOp first,
573 final LongOp second) {
574 return new LongOp() {
575 public final long op(long t) { return second.op(first.op(t)); }
576 };
577 }
578
579 /**
580 * Returns a composite mapper that applies a second mapper to the results
581 * of applying the first one.
582 */
583 public static DoubleOp compoundOp
584 (final DoubleToLong first,
585 final LongToDouble second) {
586 return new DoubleOp() {
587 public final double op(double t) { return second.op(first.op(t)); }
588 };
589 }
590
591 /**
592 * Returns a composite mapper that applies a second mapper to the results
593 * of applying the first one.
594 */
595 public static LongOp compoundOp
596 (final LongToDouble first,
597 final DoubleToLong second) {
598 return new LongOp() {
599 public final long op(long t) { return second.op(first.op(t)); }
600 };
601 }
602
603 /**
604 * Returns a predicate evaluating to the negation of its contained predicate.
605 */
606 public static <T> Predicate<T> notPredicate
607 (final Predicate<T> pred) {
608 return new Predicate<T>() {
609 public final boolean op(T x) { return !pred.op(x); }
610 };
611 }
612
613 /**
614 * Returns a predicate evaluating to the negation of its contained predicate.
615 */
616 public static DoublePredicate notPredicate
617 (final DoublePredicate pred) {
618 return new DoublePredicate() {
619 public final boolean op(double x) { return !pred.op(x); }
620 };
621 }
622
623 /**
624 * Returns a predicate evaluating to the negation of its contained predicate.
625 */
626 public static LongPredicate notPredicate
627 (final LongPredicate pred) {
628 return new LongPredicate() {
629 public final boolean op(long x) { return !pred.op(x); }
630 };
631 }
632
633 /**
634 * Returns a predicate evaluating to the conjunction of its contained predicates.
635 */
636 public static <S, T extends S> Predicate<T> andPredicate
637 (final Predicate<S> first,
638 final Predicate<? super T> second) {
639 return new Predicate<T>() {
640 public final boolean op(T x) {
641 return first.op(x) && second.op(x);
642 }
643 };
644 }
645
646 /**
647 * Returns a predicate evaluating to the disjunction of its contained predicates.
648 */
649 public static <S, T extends S> Predicate<T> orPredicate
650 (final Predicate<S> first,
651 final Predicate<? super T> second) {
652 return new Predicate<T>() {
653 public final boolean op(T x) {
654 return first.op(x) || second.op(x);
655 }
656 };
657 }
658
659 /**
660 * Returns a predicate evaluating to the conjunction of its contained predicates.
661 */
662 public static DoublePredicate andPredicate
663 (final DoublePredicate first,
664 final DoublePredicate second) {
665 return new DoublePredicate() {
666 public final boolean op(double x) {
667 return first.op(x) && second.op(x);
668 }
669 };
670 }
671
672 /**
673 * Returns a predicate evaluating to the disjunction of its contained predicates.
674 */
675 public static DoublePredicate orPredicate
676 (final DoublePredicate first,
677 final DoublePredicate second) {
678 return new DoublePredicate() {
679 public final boolean op(double x) {
680 return first.op(x) || second.op(x);
681 }
682 };
683 }
684
685
686 /**
687 * Returns a predicate evaluating to the conjunction of its contained predicates.
688 */
689 public static LongPredicate andPredicate
690 (final LongPredicate first,
691 final LongPredicate second) {
692 return new LongPredicate() {
693 public final boolean op(long x) {
694 return first.op(x) && second.op(x);
695 }
696 };
697 }
698
699 /**
700 * Returns a predicate evaluating to the disjunction of its contained predicates.
701 */
702 public static LongPredicate orPredicate
703 (final LongPredicate first,
704 final LongPredicate second) {
705 return new LongPredicate() {
706 public final boolean op(long x) {
707 return first.op(x) || second.op(x);
708 }
709 };
710 }
711
712 /**
713 * Returns a predicate evaluating to true if its argument is non-null.
714 */
715 public static Predicate<Object> isNonNullPredicate() {
716 return IsNonNullPredicate.predicate;
717 }
718 static final class IsNonNullPredicate implements Predicate<Object> {
719 static final IsNonNullPredicate predicate =
720 new IsNonNullPredicate();
721 public final boolean op(Object x) {
722 return x != null;
723 }
724 }
725
726 /**
727 * Returns a predicate evaluating to true if its argument is null.
728 */
729 public static Predicate<Object> isNullPredicate() {
730 return IsNullPredicate.predicate;
731 }
732 static final class IsNullPredicate implements Predicate<Object> {
733 static final IsNullPredicate predicate =
734 new IsNullPredicate();
735 public final boolean op(Object x) {
736 return x != null;
737 }
738 }
739
740 /**
741 * Returns a predicate evaluating to true if its argument is an instance
742 * of (see {@link Class#isInstance} the given type (class).
743 */
744 public static Predicate<Object> instanceofPredicate(final Class type) {
745 return new Predicate<Object>() {
746 public final boolean op(Object x) {
747 return type.isInstance(x);
748 }
749 };
750 }
751
752 /**
753 * Returns a predicate evaluating to true if its argument is assignable
754 * from (see {@link Class#isAssignableFrom} the given type (class).
755 */
756 public static Predicate<Object> isAssignablePredicate(final Class type) {
757 return new Predicate<Object>() {
758 public final boolean op(Object x) {
759 return type.isAssignableFrom(x.getClass());
760 }
761 };
762 }
763
764 /**
765 * Returns a reducer that adds two double elements.
766 */
767 public static DoubleReducer doubleAdder() { return DoubleAdder.adder; }
768 static final class DoubleAdder implements DoubleReducer {
769 static final DoubleAdder adder = new DoubleAdder();
770 public double op(double a, double b) { return a + b; }
771 }
772
773 /**
774 * Returns a reducer that adds two long elements.
775 */
776 public static LongReducer longAdder() { return LongAdder.adder; }
777 static final class LongAdder implements LongReducer {
778 static final LongAdder adder = new LongAdder();
779 public long op(long a, long b) { return a + b; }
780 }
781
782 /**
783 * Returns a reducer that adds two int elements.
784 */
785 public static IntReducer intAdder() { return IntAdder.adder; }
786 static final class IntAdder implements IntReducer {
787 static final IntAdder adder = new IntAdder();
788 public int op(int a, int b) { return a + b; }
789 }
790
791 /**
792 * Returns a generator producing uniform random values between
793 * zero and one, with the same properties as {@link
794 * java.util.Random#nextDouble}.
795 */
796 public static DoubleGenerator doubleRandom() {
797 return DoubleRandomGenerator.generator;
798 }
799 static final class DoubleRandomGenerator implements DoubleGenerator {
800 static final DoubleRandomGenerator generator =
801 new DoubleRandomGenerator();
802 public double op() {
803 return ThreadLocalRandom.current().nextDouble();
804 }
805 }
806
807 /**
808 * Returns a generator producing uniform random values between
809 * zero and the given bound, with the same properties as {@link
810 * java.util.Random#nextDouble}.
811 * @param bound the upper bound (exclusive) of opd values
812 */
813 public static DoubleGenerator doubleRandom(double bound) {
814 return new DoubleBoundedRandomGenerator(bound);
815 }
816 static final class DoubleBoundedRandomGenerator implements DoubleGenerator {
817 final double bound;
818 DoubleBoundedRandomGenerator(double bound) { this.bound = bound; }
819 public double op() {
820 return ThreadLocalRandom.current().nextDouble() * bound;
821 }
822 }
823
824 /**
825 * Returns a generator producing uniform random values between the
826 * given least value (inclusive) and bound (exclusive).
827 * @param least the least value returned
828 * @param bound the upper bound (exclusive) of opd values
829 */
830 public static DoubleGenerator doubleRandom(double least, double bound) {
831 return new DoubleIntervalRandomGenerator(least, bound);
832 }
833 static final class DoubleIntervalRandomGenerator implements DoubleGenerator {
834 final double least;
835 final double range;
836 DoubleIntervalRandomGenerator(double least, double bound) {
837 this.least = least; this.range = bound - least;
838 }
839 public double op() {
840 return ThreadLocalRandom.current().nextDouble() * range + least;
841 }
842 }
843
844 /**
845 * Returns a generator producing uniform random values with the
846 * same properties as {@link java.util.Random#nextLong}.
847 */
848 public static LongGenerator longRandom() {
849 return LongRandomGenerator.generator;
850 }
851 static final class LongRandomGenerator implements LongGenerator {
852 static final LongRandomGenerator generator =
853 new LongRandomGenerator();
854 public long op() {
855 return ThreadLocalRandom.current().nextLong();
856 }
857 }
858
859 /**
860 * Returns a generator producing uniform random values with the
861 * same properties as {@link java.util.Random#nextInt(int)}.
862 * @param bound the upper bound (exclusive) of opd values
863 */
864 public static LongGenerator longRandom(long bound) {
865 if (bound <= 0)
866 throw new IllegalArgumentException();
867 return new LongBoundedRandomGenerator(bound);
868 }
869 static final class LongBoundedRandomGenerator implements LongGenerator {
870 final long bound;
871 LongBoundedRandomGenerator(long bound) { this.bound = bound; }
872 public long op() {
873 return ThreadLocalRandom.current().nextLong(bound);
874 }
875 }
876
877 /**
878 * Returns a generator producing uniform random values between the
879 * given least value (inclusive) and bound (exclusive).
880 * @param least the least value returned
881 * @param bound the upper bound (exclusive) of opd values
882 */
883 public static LongGenerator longRandom(long least, long bound) {
884 if (least >= bound)
885 throw new IllegalArgumentException();
886 return new LongIntervalRandomGenerator(least, bound);
887 }
888 static final class LongIntervalRandomGenerator implements LongGenerator {
889 final long least;
890 final long range;
891 LongIntervalRandomGenerator(long least, long bound) {
892 this.least = least; this.range = bound - least;
893 }
894 public long op() {
895 return ThreadLocalRandom.current().nextLong(range) + least;
896 }
897 }
898
899 /**
900 * Returns a generator producing uniform random values with the
901 * same properties as {@link java.util.Random#nextInt}.
902 */
903 public static IntGenerator intRandom() {
904 return IntRandomGenerator.generator;
905 }
906 static final class IntRandomGenerator implements IntGenerator {
907 static final IntRandomGenerator generator =
908 new IntRandomGenerator();
909 public int op() {
910 return ThreadLocalRandom.current().nextInt();
911 }
912 }
913
914 /**
915 * Returns a generator producing uniform random values with the
916 * same properties as {@link java.util.Random#nextInt(int)}.
917 * @param bound the upper bound (exclusive) of opd values
918 */
919 public static IntGenerator intRandom(int bound) {
920 if (bound <= 0)
921 throw new IllegalArgumentException();
922 return new IntBoundedRandomGenerator(bound);
923 }
924 static final class IntBoundedRandomGenerator implements IntGenerator {
925 final int bound;
926 IntBoundedRandomGenerator(int bound) { this.bound = bound; }
927 public int op() {
928 return ThreadLocalRandom.current().nextInt(bound);
929 }
930 }
931
932 /**
933 * Returns a generator producing uniform random values between the
934 * given least value (inclusive) and bound (exclusive).
935 * @param least the least value returned
936 * @param bound the upper bound (exclusive) of opd values
937 */
938 public static IntGenerator intRandom(int least, int bound) {
939 if (least >= bound)
940 throw new IllegalArgumentException();
941 return new IntIntervalRandomGenerator(least, bound);
942 }
943 static final class IntIntervalRandomGenerator implements IntGenerator {
944 final int least;
945 final int range;
946 IntIntervalRandomGenerator(int least, int bound) {
947 this.least = least; this.range = bound - least;
948 }
949 public int op() {
950 return ThreadLocalRandom.current().nextInt(range) + least;
951 }
952 }
953
954 /**
955 * Returns a predicate evaluating to true if the
956 * first argument {@code equals} the second.
957 */
958 public static BinaryPredicate<Object, Object> equalityPredicate() {
959 return EqualityPredicate.predicate;
960 }
961 static final class EqualityPredicate implements BinaryPredicate<Object, Object> {
962 static final EqualityPredicate predicate =
963 new EqualityPredicate();
964 public final boolean op(Object x, Object y) {
965 return x.equals(y);
966 }
967 }
968
969 /**
970 * Returns a predicate evaluating to true if the
971 * first argument {@code ==} the second.
972 */
973 public static BinaryPredicate<Object, Object> identityPredicate() {
974 return IdentityPredicate.predicate;
975 }
976 static final class IdentityPredicate implements BinaryPredicate<Object, Object> {
977 static final IdentityPredicate predicate =
978 new IdentityPredicate();
979 public final boolean op(Object x, Object y) {
980 return x == y;
981 }
982 }
983
984 /**
985 * Returns a predicate evaluating to true if the
986 * first argument {@code ==} the second.
987 */
988 public static BinaryIntPredicate intEqualityPredicate() {
989 return IntEqualityPredicate.predicate;
990 }
991 static final class IntEqualityPredicate implements BinaryIntPredicate {
992 static final IntEqualityPredicate predicate =
993 new IntEqualityPredicate();
994 public final boolean op(int x, int y) {
995 return x == y;
996 }
997 }
998
999 /**
1000 * Returns a predicate evaluating to true if the
1001 * first argument {@code ==} the second.
1002 */
1003 public static BinaryLongPredicate longEqualityPredicate() {
1004 return LongEqualityPredicate.predicate;
1005 }
1006 static final class LongEqualityPredicate implements BinaryLongPredicate {
1007 static final LongEqualityPredicate predicate =
1008 new LongEqualityPredicate();
1009 public final boolean op(long x, long y) {
1010 return x == y;
1011 }
1012 }
1013
1014 /**
1015 * Returns a predicate evaluating to true if the
1016 * first argument {@code ==} the second.
1017 */
1018 public static BinaryDoublePredicate doubleEqualityPredicate() {
1019 return DoubleEqualityPredicate.predicate;
1020 }
1021 static final class DoubleEqualityPredicate implements BinaryDoublePredicate {
1022 static final DoubleEqualityPredicate predicate =
1023 new DoubleEqualityPredicate();
1024 public final boolean op(double x, double y) {
1025 return x == y;
1026 }
1027 }
1028
1029
1030 /**
1031 * Returns a predicate evaluating to true if the
1032 * first argument {@code !equals} the second.
1033 */
1034 public static BinaryPredicate<Object, Object> inequalityPredicate() {
1035 return InequalityPredicate.predicate;
1036 }
1037 static final class InequalityPredicate implements BinaryPredicate<Object, Object> {
1038 static final InequalityPredicate predicate =
1039 new InequalityPredicate();
1040 public final boolean op(Object x, Object y) {
1041 return !x.equals(y);
1042 }
1043 }
1044
1045 /**
1046 * Returns a predicate evaluating to true if the
1047 * first argument {@code !=} the second.
1048 */
1049 public static BinaryPredicate<Object, Object> nonidentityPredicate() {
1050 return NonidentityPredicate.predicate;
1051 }
1052 static final class NonidentityPredicate implements BinaryPredicate<Object, Object> {
1053 static final NonidentityPredicate predicate =
1054 new NonidentityPredicate();
1055 public final boolean op(Object x, Object y) {
1056 return x != y;
1057 }
1058 }
1059
1060 /**
1061 * Returns a predicate evaluating to true if the
1062 * first argument {@code !=} the second.
1063 */
1064 public static BinaryIntPredicate intInequalityPredicate() {
1065 return IntInequalityPredicate.predicate;
1066 }
1067 static final class IntInequalityPredicate implements BinaryIntPredicate {
1068 static final IntInequalityPredicate predicate =
1069 new IntInequalityPredicate();
1070 public final boolean op(int x, int y) {
1071 return x != y;
1072 }
1073 }
1074
1075 /**
1076 * Returns a predicate evaluating to true if the
1077 * first argument {@code ==} the second.
1078 */
1079 public static BinaryLongPredicate longInequalityPredicate() {
1080 return LongInequalityPredicate.predicate;
1081 }
1082 static final class LongInequalityPredicate implements BinaryLongPredicate {
1083 static final LongInequalityPredicate predicate =
1084 new LongInequalityPredicate();
1085 public final boolean op(long x, long y) {
1086 return x != y;
1087 }
1088 }
1089
1090 /**
1091 * Returns a predicate evaluating to true if the
1092 * first argument {@code !=} the second.
1093 */
1094 public static BinaryDoublePredicate doubleInequalityPredicate() {
1095 return DoubleInequalityPredicate.predicate;
1096 }
1097 static final class DoubleInequalityPredicate implements BinaryDoublePredicate {
1098 static final DoubleInequalityPredicate predicate =
1099 new DoubleInequalityPredicate();
1100 public final boolean op(double x, double y) {
1101 return x != y;
1102 }
1103 }
1104
1105
1106 }