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