ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/CommonOps.java
Revision: 1.1
Committed: Tue Jan 6 14:30:57 2009 UTC (15 years, 3 months ago) by dl
Branch: MAIN
Log Message:
Repackaged parallel collection classes

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