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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package extra166y; |
35 |
|
return new Reducer<T>() { |
36 |
|
public T op(T a, T b) { |
37 |
|
return (a != null && |
38 |
< |
(b == null || a.compareTo(b) >= 0))? a : b; |
38 |
> |
(b == null || a.compareTo(b) >= 0)) ? a : b; |
39 |
|
} |
40 |
|
}; |
41 |
|
} |
49 |
|
return new Reducer<T>() { |
50 |
|
public T op(T a, T b) { |
51 |
|
return (a != null && |
52 |
< |
(b == null || a.compareTo(b) <= 0))? a : b; |
52 |
> |
(b == null || a.compareTo(b) <= 0)) ? a : b; |
53 |
|
} |
54 |
|
}; |
55 |
|
} |
64 |
|
return new Reducer<T>() { |
65 |
|
public T op(T a, T b) { |
66 |
|
return (a != null && |
67 |
< |
(b == null || comparator.compare(a, b) >= 0))? a : b; |
67 |
> |
(b == null || comparator.compare(a, b) >= 0)) ? a : b; |
68 |
|
} |
69 |
|
}; |
70 |
|
} |
79 |
|
return new Reducer<T>() { |
80 |
|
public T op(T a, T b) { |
81 |
|
return (a != null && |
82 |
< |
(b == null || comparator.compare(a, b) <= 0))? a : b; |
82 |
> |
(b == null || comparator.compare(a, b) <= 0)) ? a : b; |
83 |
|
} |
84 |
|
}; |
85 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
|
* Returns a reducer returning maximum of two values, or |
103 |
< |
* <tt>null</tt> if both arguments are null, and that casts |
103 |
> |
* {@code null} if both arguments are null, and that casts |
104 |
|
* its arguments as Comparable on each comparison, throwing |
105 |
|
* ClassCastException on failure. |
106 |
|
*/ |
112 |
|
public Object op(Object a, Object b) { |
113 |
|
return (a != null && |
114 |
|
(b == null || |
115 |
< |
((Comparable)a).compareTo((Comparable)b) >= 0))? a : b; |
115 |
> |
((Comparable)a).compareTo((Comparable)b) >= 0)) ? a : b; |
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* Returns a reducer returning minimum of two values, or |
121 |
< |
* <tt>null</tt> if both arguments are null, and that casts |
121 |
> |
* {@code null} if both arguments are null, and that casts |
122 |
|
* its arguments as Comparable on each comparison, throwing |
123 |
|
* ClassCastException on failure. |
124 |
|
*/ |
130 |
|
public Object op(Object a, Object b) { |
131 |
|
return (a != null && |
132 |
|
(b == null || |
133 |
< |
((Comparable)a).compareTo((Comparable)b) <= 0))? a : b; |
133 |
> |
((Comparable)a).compareTo((Comparable)b) <= 0)) ? a : b; |
134 |
|
} |
135 |
|
} |
136 |
|
|
187 |
|
(final DoubleComparator comparator) { |
188 |
|
return new DoubleReducer() { |
189 |
|
public double op(double a, double b) { |
190 |
< |
return (comparator.compare(a, b) >= 0)? a : b; |
190 |
> |
return (comparator.compare(a, b) >= 0) ? a : b; |
191 |
|
} |
192 |
|
}; |
193 |
|
} |
200 |
|
(final DoubleComparator comparator) { |
201 |
|
return new DoubleReducer() { |
202 |
|
public double op(double a, double b) { |
203 |
< |
return (comparator.compare(a, b) <= 0)? a : b; |
203 |
> |
return (comparator.compare(a, b) <= 0) ? a : b; |
204 |
|
} |
205 |
|
}; |
206 |
|
} |
216 |
|
static final NaturalLongComparator comparator = new |
217 |
|
NaturalLongComparator(); |
218 |
|
public int compare(long a, long b) { |
219 |
< |
return a < b? -1 : ((a > b)? 1 : 0); |
219 |
> |
return (a < b) ? -1 : ((a > b) ? 1 : 0); |
220 |
|
} |
221 |
|
} |
222 |
|
|
232 |
|
implements LongReducer { |
233 |
|
public static final NaturalLongMaxReducer max = |
234 |
|
new NaturalLongMaxReducer(); |
235 |
< |
public long op(long a, long b) { return a >= b? a : b; } |
235 |
> |
public long op(long a, long b) { return (a >= b) ? a : b; } |
236 |
|
} |
237 |
|
|
238 |
|
/** |
246 |
|
implements LongReducer { |
247 |
|
public static final NaturalLongMinReducer min = |
248 |
|
new NaturalLongMinReducer(); |
249 |
< |
public long op(long a, long b) { return a <= b? a : b; } |
249 |
> |
public long op(long a, long b) { return (a <= b) ? a : b; } |
250 |
|
} |
251 |
|
|
252 |
|
/** |
257 |
|
(final LongComparator comparator) { |
258 |
|
return new LongReducer() { |
259 |
|
public long op(long a, long b) { |
260 |
< |
return (comparator.compare(a, b) >= 0)? a : b; |
260 |
> |
return (comparator.compare(a, b) >= 0) ? a : b; |
261 |
|
} |
262 |
|
}; |
263 |
|
} |
270 |
|
(final LongComparator comparator) { |
271 |
|
return new LongReducer() { |
272 |
|
public long op(long a, long b) { |
273 |
< |
return (comparator.compare(a, b) <= 0)? a : b; |
273 |
> |
return (comparator.compare(a, b) <= 0) ? a : b; |
274 |
|
} |
275 |
|
}; |
276 |
|
} |
521 |
|
*/ |
522 |
|
public static <T> DoubleOp compoundOp |
523 |
|
(final DoubleToObject<? extends T> first, |
524 |
< |
final ObjectToDouble<? super T> second) { |
524 |
> |
final ObjectToDouble<? super T> second) { |
525 |
|
return new DoubleOp() { |
526 |
|
public final double op(double t) { return second.op(first.op(t)); } |
527 |
|
}; |
533 |
|
*/ |
534 |
|
public static <T> LongToDouble compoundOp |
535 |
|
(final LongToObject<? extends T> first, |
536 |
< |
final ObjectToDouble<? super T> second) { |
536 |
> |
final ObjectToDouble<? super T> second) { |
537 |
|
return new LongToDouble() { |
538 |
|
public final double op(long t) { return second.op(first.op(t)); } |
539 |
|
}; |
545 |
|
*/ |
546 |
|
public static <T> DoubleToLong compoundOp |
547 |
|
(final DoubleToObject<? extends T> first, |
548 |
< |
final ObjectToLong<? super T> second) { |
548 |
> |
final ObjectToLong<? super T> second) { |
549 |
|
return new DoubleToLong() { |
550 |
|
public final long op(double t) { return second.op(first.op(t)); } |
551 |
|
}; |
557 |
|
*/ |
558 |
|
public static <T> LongOp compoundOp |
559 |
|
(final LongToObject<? extends T> first, |
560 |
< |
final ObjectToLong<? super T> second) { |
560 |
> |
final ObjectToLong<? super T> second) { |
561 |
|
return new LongOp() { |
562 |
|
public final long op(long t) { return second.op(first.op(t)); } |
563 |
|
}; |
711 |
|
/** |
712 |
|
* Returns a predicate evaluating to true if its argument is non-null |
713 |
|
*/ |
714 |
< |
public static Predicate<Object> isNonNullPredicate() { |
714 |
> |
public static Predicate<Object> isNonNullPredicate() { |
715 |
|
return IsNonNullPredicate.predicate; |
716 |
|
} |
717 |
|
static final class IsNonNullPredicate implements Predicate<Object> { |
725 |
|
/** |
726 |
|
* Returns a predicate evaluating to true if its argument is null |
727 |
|
*/ |
728 |
< |
public static Predicate<Object> isNullPredicate() { |
728 |
> |
public static Predicate<Object> isNullPredicate() { |
729 |
|
return IsNullPredicate.predicate; |
730 |
|
} |
731 |
|
static final class IsNullPredicate implements Predicate<Object> { |
790 |
|
/** |
791 |
|
* Returns a generator producing uniform random values between |
792 |
|
* zero and one, with the same properties as {@link |
793 |
< |
* java.util.Random#nextDouble} but operating independently across |
794 |
< |
* ForkJoinWorkerThreads and usable only within forkjoin |
795 |
< |
* computations. |
793 |
> |
* java.util.Random#nextDouble} |
794 |
|
*/ |
795 |
|
public static DoubleGenerator doubleRandom() { |
796 |
|
return DoubleRandomGenerator.generator; |
799 |
|
static final DoubleRandomGenerator generator = |
800 |
|
new DoubleRandomGenerator(); |
801 |
|
public double op() { |
802 |
< |
return ForkJoinWorkerThread.nextRandomDouble(); |
802 |
> |
return ThreadLocalRandom.current().nextDouble(); |
803 |
|
} |
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 |
< |
* java.util.Random#nextDouble} but operating independently across |
812 |
< |
* ForkJoinWorkerThreads and usable only within forkjoin |
813 |
< |
* computations. |
809 |
> |
* java.util.Random#nextDouble}. |
810 |
|
* @param bound the upper bound (exclusive) of opd values |
811 |
|
*/ |
812 |
|
public static DoubleGenerator doubleRandom(double bound) { |
816 |
|
final double bound; |
817 |
|
DoubleBoundedRandomGenerator(double bound) { this.bound = bound; } |
818 |
|
public double op() { |
819 |
< |
return ForkJoinWorkerThread.nextRandomDouble() * bound; |
819 |
> |
return ThreadLocalRandom.current().nextDouble() * bound; |
820 |
|
} |
821 |
|
} |
822 |
|
|
823 |
|
/** |
824 |
|
* Returns a generator producing uniform random values between the |
825 |
< |
* given least value (inclusive) and bound (exclusive), operating |
830 |
< |
* independently across ForkJoinWorkerThreads and usable only |
831 |
< |
* within forkjoin computations. |
825 |
> |
* given least value (inclusive) and bound (exclusive) |
826 |
|
* @param least the least value returned |
827 |
|
* @param bound the upper bound (exclusive) of opd values |
828 |
|
*/ |
836 |
|
this.least = least; this.range = bound - least; |
837 |
|
} |
838 |
|
public double op() { |
839 |
< |
return ForkJoinWorkerThread.nextRandomDouble() * range + least; |
839 |
> |
return ThreadLocalRandom.current().nextDouble() * range + least; |
840 |
|
} |
841 |
|
} |
842 |
|
|
843 |
|
/** |
844 |
|
* Returns a generator producing uniform random values with the |
845 |
< |
* same properties as {@link java.util.Random#nextLong} but |
852 |
< |
* operating independently across ForkJoinWorkerThreads and usable |
853 |
< |
* only within forkjoin computations. |
845 |
> |
* same properties as {@link java.util.Random#nextLong} |
846 |
|
*/ |
847 |
|
public static LongGenerator longRandom() { |
848 |
|
return LongRandomGenerator.generator; |
851 |
|
static final LongRandomGenerator generator = |
852 |
|
new LongRandomGenerator(); |
853 |
|
public long op() { |
854 |
< |
return ForkJoinWorkerThread.nextRandomLong(); |
854 |
> |
return ThreadLocalRandom.current().nextLong(); |
855 |
|
} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
|
* Returns a generator producing uniform random values with the |
860 |
< |
* same properties as {@link java.util.Random#nextInt(int)} but |
869 |
< |
* operating independently across ForkJoinWorkerThreads and usable |
870 |
< |
* only within forkjoin computations. |
860 |
> |
* same properties as {@link java.util.Random#nextInt(int)} |
861 |
|
* @param bound the upper bound (exclusive) of opd values |
862 |
|
*/ |
863 |
|
public static LongGenerator longRandom(long bound) { |
869 |
|
final long bound; |
870 |
|
LongBoundedRandomGenerator(long bound) { this.bound = bound; } |
871 |
|
public long op() { |
872 |
< |
return ForkJoinWorkerThread.nextRandomLong(bound); |
872 |
> |
return ThreadLocalRandom.current().nextLong(bound); |
873 |
|
} |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
|
* Returns a generator producing uniform random values between the |
878 |
< |
* given least value (inclusive) and bound (exclusive), operating |
889 |
< |
* independently across ForkJoinWorkerThreads and usable only |
890 |
< |
* within forkjoin computations. |
878 |
> |
* given least value (inclusive) and bound (exclusive). |
879 |
|
* @param least the least value returned |
880 |
|
* @param bound the upper bound (exclusive) of opd values |
881 |
|
*/ |
891 |
|
this.least = least; this.range = bound - least; |
892 |
|
} |
893 |
|
public long op() { |
894 |
< |
return ForkJoinWorkerThread.nextRandomLong(range) + least; |
894 |
> |
return ThreadLocalRandom.current().nextLong(range) + least; |
895 |
|
} |
896 |
|
} |
897 |
|
|
898 |
|
/** |
899 |
|
* Returns a generator producing uniform random values with the |
900 |
< |
* same properties as {@link java.util.Random#nextInt} but |
913 |
< |
* operating independently across ForkJoinWorkerThreads and usable |
914 |
< |
* only within forkjoin computations. |
900 |
> |
* same properties as {@link java.util.Random#nextInt} |
901 |
|
*/ |
902 |
|
public static IntGenerator intRandom() { |
903 |
|
return IntRandomGenerator.generator; |
906 |
|
static final IntRandomGenerator generator = |
907 |
|
new IntRandomGenerator(); |
908 |
|
public int op() { |
909 |
< |
return ForkJoinWorkerThread.nextRandomInt(); |
909 |
> |
return ThreadLocalRandom.current().nextInt(); |
910 |
|
} |
911 |
|
} |
912 |
|
|
913 |
|
/** |
914 |
|
* Returns a generator producing uniform random values with the |
915 |
< |
* same properties as {@link java.util.Random#nextInt(int)} but |
930 |
< |
* operating independently across ForkJoinWorkerThreads and usable |
931 |
< |
* only within forkjoin computations. |
915 |
> |
* same properties as {@link java.util.Random#nextInt(int)} |
916 |
|
* @param bound the upper bound (exclusive) of opd values |
917 |
|
*/ |
918 |
|
public static IntGenerator intRandom(int bound) { |
924 |
|
final int bound; |
925 |
|
IntBoundedRandomGenerator(int bound) { this.bound = bound; } |
926 |
|
public int op() { |
927 |
< |
return ForkJoinWorkerThread.nextRandomInt(bound); |
927 |
> |
return ThreadLocalRandom.current().nextInt(bound); |
928 |
|
} |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
|
* Returns a generator producing uniform random values between the |
933 |
< |
* given least value (inclusive) and bound (exclusive), operating |
950 |
< |
* independently across ForkJoinWorkerThreads and usable only |
951 |
< |
* within forkjoin computations. |
933 |
> |
* given least value (inclusive) and bound (exclusive) |
934 |
|
* @param least the least value returned |
935 |
|
* @param bound the upper bound (exclusive) of opd values |
936 |
|
*/ |
946 |
|
this.least = least; this.range = bound - least; |
947 |
|
} |
948 |
|
public int op() { |
949 |
< |
return ForkJoinWorkerThread.nextRandomInt(range) + least; |
949 |
> |
return ThreadLocalRandom.current().nextInt(range) + least; |
950 |
|
} |
951 |
|
} |
952 |
|
|
953 |
|
/** |
954 |
|
* Returns a predicate evaluating to true if the |
955 |
< |
* first argument <tt>equals</tt> the second |
955 |
> |
* first argument {@code equals} the second |
956 |
|
*/ |
957 |
|
public static BinaryPredicate<Object, Object> equalityPredicate() { |
958 |
|
return EqualityPredicate.predicate; |
967 |
|
|
968 |
|
/** |
969 |
|
* Returns a predicate evaluating to true if the |
970 |
< |
* first argument <tt>==</tt> the second |
970 |
> |
* first argument {@code ==} the second |
971 |
|
*/ |
972 |
|
public static BinaryPredicate<Object, Object> identityPredicate() { |
973 |
|
return IdentityPredicate.predicate; |
982 |
|
|
983 |
|
/** |
984 |
|
* Returns a predicate evaluating to true if the |
985 |
< |
* first argument <tt>==</tt> the second |
985 |
> |
* first argument {@code ==} the second |
986 |
|
*/ |
987 |
|
public static BinaryIntPredicate intEqualityPredicate() { |
988 |
|
return IntEqualityPredicate.predicate; |
997 |
|
|
998 |
|
/** |
999 |
|
* Returns a predicate evaluating to true if the |
1000 |
< |
* first argument <tt>==</tt> the second |
1000 |
> |
* first argument {@code ==} the second |
1001 |
|
*/ |
1002 |
|
public static BinaryLongPredicate longEqualityPredicate() { |
1003 |
|
return LongEqualityPredicate.predicate; |
1012 |
|
|
1013 |
|
/** |
1014 |
|
* Returns a predicate evaluating to true if the |
1015 |
< |
* first argument <tt>==</tt> the second |
1015 |
> |
* first argument {@code ==} the second |
1016 |
|
*/ |
1017 |
|
public static BinaryDoublePredicate doubleEqualityPredicate() { |
1018 |
|
return DoubleEqualityPredicate.predicate; |
1028 |
|
|
1029 |
|
/** |
1030 |
|
* Returns a predicate evaluating to true if the |
1031 |
< |
* first argument <tt>!equals</tt> the second |
1031 |
> |
* first argument {@code !equals} the second |
1032 |
|
*/ |
1033 |
|
public static BinaryPredicate<Object, Object> inequalityPredicate() { |
1034 |
|
return InequalityPredicate.predicate; |
1043 |
|
|
1044 |
|
/** |
1045 |
|
* Returns a predicate evaluating to true if the |
1046 |
< |
* first argument <tt>!=</tt> the second |
1046 |
> |
* first argument {@code !=} the second |
1047 |
|
*/ |
1048 |
|
public static BinaryPredicate<Object, Object> nonidentityPredicate() { |
1049 |
|
return NonidentityPredicate.predicate; |
1058 |
|
|
1059 |
|
/** |
1060 |
|
* Returns a predicate evaluating to true if the |
1061 |
< |
* first argument <tt>!=</tt> the second |
1061 |
> |
* first argument {@code !=} the second |
1062 |
|
*/ |
1063 |
|
public static BinaryIntPredicate intInequalityPredicate() { |
1064 |
|
return IntInequalityPredicate.predicate; |
1073 |
|
|
1074 |
|
/** |
1075 |
|
* Returns a predicate evaluating to true if the |
1076 |
< |
* first argument <tt>==</tt> the second |
1076 |
> |
* first argument {@code ==} the second |
1077 |
|
*/ |
1078 |
|
public static BinaryLongPredicate longInequalityPredicate() { |
1079 |
|
return LongInequalityPredicate.predicate; |
1088 |
|
|
1089 |
|
/** |
1090 |
|
* Returns a predicate evaluating to true if the |
1091 |
< |
* first argument <tt>!=</tt> the second |
1091 |
> |
* first argument {@code !=} the second |
1092 |
|
*/ |
1093 |
|
public static BinaryDoublePredicate doubleInequalityPredicate() { |
1094 |
|
return DoubleInequalityPredicate.predicate; |