ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/CommonOps.java
Revision: 1.3
Committed: Fri Oct 22 05:18:30 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +13 -13 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.3 (b == null || a.compareTo(b) >= 0)) ? a : b;
39 dl 1.1 }
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 jsr166 1.3 (b == null || a.compareTo(b) <= 0)) ? a : b;
53 dl 1.1 }
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 jsr166 1.3 (b == null || comparator.compare(a, b) >= 0)) ? a : b;
68 dl 1.1 }
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 jsr166 1.3 (b == null || comparator.compare(a, b) <= 0)) ? a : b;
83 dl 1.1 }
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 jsr166 1.3 ((Comparable)a).compareTo((Comparable)b) >= 0)) ? a : b;
116 dl 1.1 }
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 jsr166 1.3 ((Comparable)a).compareTo((Comparable)b) <= 0)) ? a : b;
134 dl 1.1 }
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 jsr166 1.3 return (comparator.compare(a, b) >= 0) ? a : b;
191 dl 1.1 }
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 jsr166 1.3 return (comparator.compare(a, b) <= 0) ? a : b;
204 dl 1.1 }
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 jsr166 1.3 return (a < b) ? -1 : ((a > b) ? 1 : 0);
220 dl 1.1 }
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 jsr166 1.3 public long op(long a, long b) { return (a >= b) ? a : b; }
236 dl 1.1 }
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 jsr166 1.3 public long op(long a, long b) { return (a <= b) ? a : b; }
250 dl 1.1 }
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 jsr166 1.3 return (comparator.compare(a, b) >= 0) ? a : b;
261 dl 1.1 }
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 jsr166 1.3 return (comparator.compare(a, b) <= 0) ? a : b;
274 dl 1.1 }
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 dl 1.2 * java.util.Random#nextDouble}
794 dl 1.1 */
795     public static DoubleGenerator doubleRandom() {
796     return DoubleRandomGenerator.generator;
797     }
798     static final class DoubleRandomGenerator implements DoubleGenerator {
799     static final DoubleRandomGenerator generator =
800     new DoubleRandomGenerator();
801     public double op() {
802 dl 1.2 return ThreadLocalRandom.current().nextDouble();
803 dl 1.1 }
804     }
805    
806     /**
807     * Returns a generator producing uniform random values between
808     * zero and the given bound, with the same properties as {@link
809 dl 1.2 * java.util.Random#nextDouble}.
810 dl 1.1 * @param bound the upper bound (exclusive) of opd values
811     */
812     public static DoubleGenerator doubleRandom(double bound) {
813     return new DoubleBoundedRandomGenerator(bound);
814     }
815     static final class DoubleBoundedRandomGenerator implements DoubleGenerator {
816     final double bound;
817     DoubleBoundedRandomGenerator(double bound) { this.bound = bound; }
818     public double op() {
819 dl 1.2 return ThreadLocalRandom.current().nextDouble() * bound;
820 dl 1.1 }
821     }
822    
823     /**
824     * Returns a generator producing uniform random values between the
825 dl 1.2 * given least value (inclusive) and bound (exclusive)
826 dl 1.1 * @param least the least value returned
827     * @param bound the upper bound (exclusive) of opd values
828     */
829     public static DoubleGenerator doubleRandom(double least, double bound) {
830     return new DoubleIntervalRandomGenerator(least, bound);
831     }
832     static final class DoubleIntervalRandomGenerator implements DoubleGenerator {
833     final double least;
834     final double range;
835     DoubleIntervalRandomGenerator(double least, double bound) {
836     this.least = least; this.range = bound - least;
837     }
838     public double op() {
839 dl 1.2 return ThreadLocalRandom.current().nextDouble() * range + least;
840 dl 1.1 }
841     }
842    
843     /**
844     * Returns a generator producing uniform random values with the
845 dl 1.2 * same properties as {@link java.util.Random#nextLong}
846 dl 1.1 */
847     public static LongGenerator longRandom() {
848     return LongRandomGenerator.generator;
849     }
850     static final class LongRandomGenerator implements LongGenerator {
851     static final LongRandomGenerator generator =
852     new LongRandomGenerator();
853     public long op() {
854 dl 1.2 return ThreadLocalRandom.current().nextLong();
855 dl 1.1 }
856     }
857    
858     /**
859     * Returns a generator producing uniform random values with the
860 dl 1.2 * same properties as {@link java.util.Random#nextInt(int)}
861 dl 1.1 * @param bound the upper bound (exclusive) of opd values
862     */
863     public static LongGenerator longRandom(long bound) {
864     if (bound <= 0)
865     throw new IllegalArgumentException();
866     return new LongBoundedRandomGenerator(bound);
867     }
868     static final class LongBoundedRandomGenerator implements LongGenerator {
869     final long bound;
870     LongBoundedRandomGenerator(long bound) { this.bound = bound; }
871     public long op() {
872 dl 1.2 return ThreadLocalRandom.current().nextLong(bound);
873 dl 1.1 }
874     }
875    
876     /**
877     * Returns a generator producing uniform random values between the
878 dl 1.2 * given least value (inclusive) and bound (exclusive).
879 dl 1.1 * @param least the least value returned
880     * @param bound the upper bound (exclusive) of opd values
881     */
882     public static LongGenerator longRandom(long least, long bound) {
883     if (least >= bound)
884     throw new IllegalArgumentException();
885     return new LongIntervalRandomGenerator(least, bound);
886     }
887     static final class LongIntervalRandomGenerator implements LongGenerator {
888     final long least;
889     final long range;
890     LongIntervalRandomGenerator(long least, long bound) {
891     this.least = least; this.range = bound - least;
892     }
893     public long op() {
894 dl 1.2 return ThreadLocalRandom.current().nextLong(range) + least;
895 dl 1.1 }
896     }
897    
898     /**
899     * Returns a generator producing uniform random values with the
900 dl 1.2 * same properties as {@link java.util.Random#nextInt}
901 dl 1.1 */
902     public static IntGenerator intRandom() {
903     return IntRandomGenerator.generator;
904     }
905     static final class IntRandomGenerator implements IntGenerator {
906     static final IntRandomGenerator generator =
907     new IntRandomGenerator();
908     public int op() {
909 dl 1.2 return ThreadLocalRandom.current().nextInt();
910 dl 1.1 }
911     }
912    
913     /**
914     * Returns a generator producing uniform random values with the
915 dl 1.2 * same properties as {@link java.util.Random#nextInt(int)}
916 dl 1.1 * @param bound the upper bound (exclusive) of opd values
917     */
918     public static IntGenerator intRandom(int bound) {
919     if (bound <= 0)
920     throw new IllegalArgumentException();
921     return new IntBoundedRandomGenerator(bound);
922     }
923     static final class IntBoundedRandomGenerator implements IntGenerator {
924     final int bound;
925     IntBoundedRandomGenerator(int bound) { this.bound = bound; }
926     public int op() {
927 dl 1.2 return ThreadLocalRandom.current().nextInt(bound);
928 dl 1.1 }
929     }
930    
931     /**
932     * Returns a generator producing uniform random values between the
933 dl 1.2 * given least value (inclusive) and bound (exclusive)
934 dl 1.1 * @param least the least value returned
935     * @param bound the upper bound (exclusive) of opd values
936     */
937     public static IntGenerator intRandom(int least, int bound) {
938     if (least >= bound)
939     throw new IllegalArgumentException();
940     return new IntIntervalRandomGenerator(least, bound);
941     }
942     static final class IntIntervalRandomGenerator implements IntGenerator {
943     final int least;
944     final int range;
945     IntIntervalRandomGenerator(int least, int bound) {
946     this.least = least; this.range = bound - least;
947     }
948     public int op() {
949 dl 1.2 return ThreadLocalRandom.current().nextInt(range) + least;
950 dl 1.1 }
951     }
952    
953     /**
954     * Returns a predicate evaluating to true if the
955     * first argument <tt>equals</tt> the second
956     */
957     public static BinaryPredicate<Object, Object> equalityPredicate() {
958     return EqualityPredicate.predicate;
959     }
960     static final class EqualityPredicate implements BinaryPredicate<Object, Object> {
961     static final EqualityPredicate predicate =
962     new EqualityPredicate();
963     public final boolean op(Object x, Object y) {
964     return x.equals(y);
965     }
966     }
967    
968     /**
969     * Returns a predicate evaluating to true if the
970     * first argument <tt>==</tt> the second
971     */
972     public static BinaryPredicate<Object, Object> identityPredicate() {
973     return IdentityPredicate.predicate;
974     }
975     static final class IdentityPredicate implements BinaryPredicate<Object, Object> {
976     static final IdentityPredicate predicate =
977     new IdentityPredicate();
978     public final boolean op(Object x, Object y) {
979     return x == y;
980     }
981     }
982    
983     /**
984     * Returns a predicate evaluating to true if the
985     * first argument <tt>==</tt> the second
986     */
987     public static BinaryIntPredicate intEqualityPredicate() {
988     return IntEqualityPredicate.predicate;
989     }
990     static final class IntEqualityPredicate implements BinaryIntPredicate {
991     static final IntEqualityPredicate predicate =
992     new IntEqualityPredicate();
993     public final boolean op(int x, int y) {
994     return x == y;
995     }
996     }
997    
998     /**
999     * Returns a predicate evaluating to true if the
1000     * first argument <tt>==</tt> the second
1001     */
1002     public static BinaryLongPredicate longEqualityPredicate() {
1003     return LongEqualityPredicate.predicate;
1004     }
1005     static final class LongEqualityPredicate implements BinaryLongPredicate {
1006     static final LongEqualityPredicate predicate =
1007     new LongEqualityPredicate();
1008     public final boolean op(long x, long y) {
1009     return x == y;
1010     }
1011     }
1012    
1013     /**
1014     * Returns a predicate evaluating to true if the
1015     * first argument <tt>==</tt> the second
1016     */
1017     public static BinaryDoublePredicate doubleEqualityPredicate() {
1018     return DoubleEqualityPredicate.predicate;
1019     }
1020     static final class DoubleEqualityPredicate implements BinaryDoublePredicate {
1021     static final DoubleEqualityPredicate predicate =
1022     new DoubleEqualityPredicate();
1023     public final boolean op(double x, double y) {
1024     return x == y;
1025     }
1026     }
1027    
1028    
1029     /**
1030     * Returns a predicate evaluating to true if the
1031     * first argument <tt>!equals</tt> the second
1032     */
1033     public static BinaryPredicate<Object, Object> inequalityPredicate() {
1034     return InequalityPredicate.predicate;
1035     }
1036     static final class InequalityPredicate implements BinaryPredicate<Object, Object> {
1037     static final InequalityPredicate predicate =
1038     new InequalityPredicate();
1039     public final boolean op(Object x, Object y) {
1040     return !x.equals(y);
1041     }
1042     }
1043    
1044     /**
1045     * Returns a predicate evaluating to true if the
1046     * first argument <tt>!=</tt> the second
1047     */
1048     public static BinaryPredicate<Object, Object> nonidentityPredicate() {
1049     return NonidentityPredicate.predicate;
1050     }
1051     static final class NonidentityPredicate implements BinaryPredicate<Object, Object> {
1052     static final NonidentityPredicate predicate =
1053     new NonidentityPredicate();
1054     public final boolean op(Object x, Object y) {
1055     return x != y;
1056     }
1057     }
1058    
1059     /**
1060     * Returns a predicate evaluating to true if the
1061     * first argument <tt>!=</tt> the second
1062     */
1063     public static BinaryIntPredicate intInequalityPredicate() {
1064     return IntInequalityPredicate.predicate;
1065     }
1066     static final class IntInequalityPredicate implements BinaryIntPredicate {
1067     static final IntInequalityPredicate predicate =
1068     new IntInequalityPredicate();
1069     public final boolean op(int x, int y) {
1070     return x != y;
1071     }
1072     }
1073    
1074     /**
1075     * Returns a predicate evaluating to true if the
1076     * first argument <tt>==</tt> the second
1077     */
1078     public static BinaryLongPredicate longInequalityPredicate() {
1079     return LongInequalityPredicate.predicate;
1080     }
1081     static final class LongInequalityPredicate implements BinaryLongPredicate {
1082     static final LongInequalityPredicate predicate =
1083     new LongInequalityPredicate();
1084     public final boolean op(long x, long y) {
1085     return x != y;
1086     }
1087     }
1088    
1089     /**
1090     * Returns a predicate evaluating to true if the
1091     * first argument <tt>!=</tt> the second
1092     */
1093     public static BinaryDoublePredicate doubleInequalityPredicate() {
1094     return DoubleInequalityPredicate.predicate;
1095     }
1096     static final class DoubleInequalityPredicate implements BinaryDoublePredicate {
1097     static final DoubleInequalityPredicate predicate =
1098     new DoubleInequalityPredicate();
1099     public final boolean op(double x, double y) {
1100     return x != y;
1101     }
1102     }
1103    
1104    
1105     }