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

# 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 jsr166 1.4 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     package extra166y;
8 jsr166 1.8
9 dl 1.1 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 jsr166 1.7 * Returns a Comparator for Comparable objects.
22 dl 1.1 */
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 jsr166 1.3 (b == null || a.compareTo(b) >= 0)) ? a : b;
40 dl 1.1 }
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 jsr166 1.3 (b == null || a.compareTo(b) <= 0)) ? a : b;
54 dl 1.1 }
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 jsr166 1.3 (b == null || comparator.compare(a, b) >= 0)) ? a : b;
69 dl 1.1 }
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 jsr166 1.3 (b == null || comparator.compare(a, b) <= 0)) ? a : b;
84 dl 1.1 }
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 jsr166 1.6 * {@code null} if both arguments are null, and that casts
105 dl 1.1 * 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 jsr166 1.3 ((Comparable)a).compareTo((Comparable)b) >= 0)) ? a : b;
117 dl 1.1 }
118     }
119    
120     /**
121     * Returns a reducer returning minimum of two values, or
122 jsr166 1.6 * {@code null} if both arguments are null, and that casts
123 dl 1.1 * 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 jsr166 1.3 ((Comparable)a).compareTo((Comparable)b) <= 0)) ? a : b;
135 dl 1.1 }
136     }
137    
138    
139     /**
140 jsr166 1.7 * Returns a comparator for doubles relying on natural ordering.
141 dl 1.1 */
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 jsr166 1.7 * using natural comparator.
157 dl 1.1 */
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 jsr166 1.7 * using natural comparator.
172 dl 1.1 */
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 jsr166 1.7 * using the given comparator.
186 dl 1.1 */
187     public static DoubleReducer doubleMaxReducer
188     (final DoubleComparator comparator) {
189     return new DoubleReducer() {
190     public double op(double a, double b) {
191 jsr166 1.3 return (comparator.compare(a, b) >= 0) ? a : b;
192 dl 1.1 }
193     };
194     }
195    
196     /**
197     * Returns a reducer returning the minimum of two double elements,
198 jsr166 1.7 * using the given comparator.
199 dl 1.1 */
200     public static DoubleReducer doubleMinReducer
201     (final DoubleComparator comparator) {
202     return new DoubleReducer() {
203     public double op(double a, double b) {
204 jsr166 1.3 return (comparator.compare(a, b) <= 0) ? a : b;
205 dl 1.1 }
206     };
207     }
208    
209     /**
210 jsr166 1.7 * Returns a comparator for longs relying on natural ordering.
211 dl 1.1 */
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 jsr166 1.3 return (a < b) ? -1 : ((a > b) ? 1 : 0);
221 dl 1.1 }
222     }
223    
224     /**
225     * Returns a reducer returning the maximum of two long elements,
226 jsr166 1.7 * using natural comparator.
227 dl 1.1 */
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 jsr166 1.3 public long op(long a, long b) { return (a >= b) ? a : b; }
237 dl 1.1 }
238    
239     /**
240     * A reducer returning the minimum of two long elements,
241 jsr166 1.7 * using natural comparator.
242 dl 1.1 */
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 jsr166 1.3 public long op(long a, long b) { return (a <= b) ? a : b; }
251 dl 1.1 }
252    
253     /**
254     * Returns a reducer returning the maximum of two long elements,
255 jsr166 1.7 * using the given comparator.
256 dl 1.1 */
257     public static LongReducer longMaxReducer
258     (final LongComparator comparator) {
259     return new LongReducer() {
260     public long op(long a, long b) {
261 jsr166 1.3 return (comparator.compare(a, b) >= 0) ? a : b;
262 dl 1.1 }
263     };
264     }
265    
266     /**
267     * Returns a reducer returning the minimum of two long elements,
268 jsr166 1.7 * using the given comparator.
269 dl 1.1 */
270     public static LongReducer longMinReducer
271     (final LongComparator comparator) {
272     return new LongReducer() {
273     public long op(long a, long b) {
274 jsr166 1.3 return (comparator.compare(a, b) <= 0) ? a : b;
275 dl 1.1 }
276     };
277     }
278    
279     /**
280     * Returns a composite mapper that applies a second mapper to the results
281 jsr166 1.7 * of applying the first one.
282 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
294 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
306 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
318 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
330 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
342 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
354 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
366 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
378 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
390 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
402 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
414 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
426 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
438 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
450 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
462 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
474 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
486 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
498 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
510 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
522 dl 1.1 */
523     public static <T> DoubleOp compoundOp
524     (final DoubleToObject<? extends T> first,
525 jsr166 1.5 final ObjectToDouble<? super T> second) {
526 dl 1.1 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 jsr166 1.7 * of applying the first one.
534 dl 1.1 */
535     public static <T> LongToDouble compoundOp
536     (final LongToObject<? extends T> first,
537 jsr166 1.5 final ObjectToDouble<? super T> second) {
538 dl 1.1 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 jsr166 1.7 * of applying the first one.
546 dl 1.1 */
547     public static <T> DoubleToLong compoundOp
548     (final DoubleToObject<? extends T> first,
549 jsr166 1.5 final ObjectToLong<? super T> second) {
550 dl 1.1 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 jsr166 1.7 * of applying the first one.
558 dl 1.1 */
559     public static <T> LongOp compoundOp
560     (final LongToObject<? extends T> first,
561 jsr166 1.5 final ObjectToLong<? super T> second) {
562 dl 1.1 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 jsr166 1.7 * of applying the first one.
570 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
582 dl 1.1 */
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 jsr166 1.7 * of applying the first one.
594 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the negation of its contained predicate.
605 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the negation of its contained predicate.
615 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the negation of its contained predicate.
625 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the conjunction of its contained predicates.
635 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the disjunction of its contained predicates.
648 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the conjunction of its contained predicates.
661 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the disjunction of its contained predicates.
674 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the conjunction of its contained predicates.
688 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to the disjunction of its contained predicates.
701 dl 1.1 */
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 jsr166 1.7 * Returns a predicate evaluating to true if its argument is non-null.
714 dl 1.1 */
715 jsr166 1.5 public static Predicate<Object> isNonNullPredicate() {
716 dl 1.1 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 jsr166 1.7 * Returns a predicate evaluating to true if its argument is null.
728 dl 1.1 */
729 jsr166 1.5 public static Predicate<Object> isNullPredicate() {
730 dl 1.1 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 jsr166 1.7 * Returns a reducer that adds two double elements.
766 dl 1.1 */
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 jsr166 1.7 * Returns a reducer that adds two long elements.
775 dl 1.1 */
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 jsr166 1.7 * Returns a reducer that adds two int elements.
784 dl 1.1 */
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 jsr166 1.7 * java.util.Random#nextDouble}.
795 dl 1.1 */
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 dl 1.2 return ThreadLocalRandom.current().nextDouble();
804 dl 1.1 }
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 dl 1.2 * java.util.Random#nextDouble}.
811 dl 1.1 * @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 dl 1.2 return ThreadLocalRandom.current().nextDouble() * bound;
821 dl 1.1 }
822     }
823    
824     /**
825     * Returns a generator producing uniform random values between the
826 jsr166 1.7 * given least value (inclusive) and bound (exclusive).
827 dl 1.1 * @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 dl 1.2 return ThreadLocalRandom.current().nextDouble() * range + least;
841 dl 1.1 }
842     }
843    
844     /**
845     * Returns a generator producing uniform random values with the
846 jsr166 1.7 * same properties as {@link java.util.Random#nextLong}.
847 dl 1.1 */
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 dl 1.2 return ThreadLocalRandom.current().nextLong();
856 dl 1.1 }
857     }
858    
859     /**
860     * Returns a generator producing uniform random values with the
861 jsr166 1.7 * same properties as {@link java.util.Random#nextInt(int)}.
862 dl 1.1 * @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 dl 1.2 return ThreadLocalRandom.current().nextLong(bound);
874 dl 1.1 }
875     }
876    
877     /**
878     * Returns a generator producing uniform random values between the
879 dl 1.2 * given least value (inclusive) and bound (exclusive).
880 dl 1.1 * @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 dl 1.2 return ThreadLocalRandom.current().nextLong(range) + least;
896 dl 1.1 }
897     }
898    
899     /**
900     * Returns a generator producing uniform random values with the
901 jsr166 1.7 * same properties as {@link java.util.Random#nextInt}.
902 dl 1.1 */
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 dl 1.2 return ThreadLocalRandom.current().nextInt();
911 dl 1.1 }
912     }
913    
914     /**
915     * Returns a generator producing uniform random values with the
916 jsr166 1.7 * same properties as {@link java.util.Random#nextInt(int)}.
917 dl 1.1 * @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 dl 1.2 return ThreadLocalRandom.current().nextInt(bound);
929 dl 1.1 }
930     }
931    
932     /**
933     * Returns a generator producing uniform random values between the
934 jsr166 1.7 * given least value (inclusive) and bound (exclusive).
935 dl 1.1 * @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 dl 1.2 return ThreadLocalRandom.current().nextInt(range) + least;
951 dl 1.1 }
952     }
953    
954     /**
955     * Returns a predicate evaluating to true if the
956 jsr166 1.7 * first argument {@code equals} the second.
957 dl 1.1 */
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 jsr166 1.7 * first argument {@code ==} the second.
972 dl 1.1 */
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 jsr166 1.7 * first argument {@code ==} the second.
987 dl 1.1 */
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 jsr166 1.7 * first argument {@code ==} the second.
1002 dl 1.1 */
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 jsr166 1.7 * first argument {@code ==} the second.
1017 dl 1.1 */
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 jsr166 1.7 * first argument {@code !equals} the second.
1033 dl 1.1 */
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 jsr166 1.7 * first argument {@code !=} the second.
1048 dl 1.1 */
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 jsr166 1.7 * first argument {@code !=} the second.
1063 dl 1.1 */
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 jsr166 1.7 * first argument {@code ==} the second.
1078 dl 1.1 */
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 jsr166 1.7 * first argument {@code !=} the second.
1093 dl 1.1 */
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     }