--- jsr166/src/extra166y/CommonOps.java 2009/01/06 14:30:57 1.1 +++ jsr166/src/extra166y/CommonOps.java 2015/01/18 20:17:32 1.8 @@ -1,10 +1,11 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ package extra166y; + import jsr166y.*; import static extra166y.Ops.*; import java.util.*; @@ -17,7 +18,7 @@ public class CommonOps { private CommonOps() {} // disable construction /** - * Returns a Comparator for Comparable objects + * Returns a Comparator for Comparable objects. */ public static > Comparator naturalComparator(Class type) { @@ -35,7 +36,7 @@ public class CommonOps { return new Reducer() { public T op(T a, T b) { return (a != null && - (b == null || a.compareTo(b) >= 0))? a : b; + (b == null || a.compareTo(b) >= 0)) ? a : b; } }; } @@ -49,7 +50,7 @@ public class CommonOps { return new Reducer() { public T op(T a, T b) { return (a != null && - (b == null || a.compareTo(b) <= 0))? a : b; + (b == null || a.compareTo(b) <= 0)) ? a : b; } }; } @@ -64,7 +65,7 @@ public class CommonOps { return new Reducer() { public T op(T a, T b) { return (a != null && - (b == null || comparator.compare(a, b) >= 0))? a : b; + (b == null || comparator.compare(a, b) >= 0)) ? a : b; } }; } @@ -79,7 +80,7 @@ public class CommonOps { return new Reducer() { public T op(T a, T b) { return (a != null && - (b == null || comparator.compare(a, b) <= 0))? a : b; + (b == null || comparator.compare(a, b) <= 0)) ? a : b; } }; } @@ -100,7 +101,7 @@ public class CommonOps { /** * Returns a reducer returning maximum of two values, or - * null if both arguments are null, and that casts + * {@code null} if both arguments are null, and that casts * its arguments as Comparable on each comparison, throwing * ClassCastException on failure. */ @@ -112,13 +113,13 @@ public class CommonOps { public Object op(Object a, Object b) { return (a != null && (b == null || - ((Comparable)a).compareTo((Comparable)b) >= 0))? a : b; + ((Comparable)a).compareTo((Comparable)b) >= 0)) ? a : b; } } /** * Returns a reducer returning minimum of two values, or - * null if both arguments are null, and that casts + * {@code null} if both arguments are null, and that casts * its arguments as Comparable on each comparison, throwing * ClassCastException on failure. */ @@ -130,13 +131,13 @@ public class CommonOps { public Object op(Object a, Object b) { return (a != null && (b == null || - ((Comparable)a).compareTo((Comparable)b) <= 0))? a : b; + ((Comparable)a).compareTo((Comparable)b) <= 0)) ? a : b; } } /** - * Returns a comparator for doubles relying on natural ordering + * Returns a comparator for doubles relying on natural ordering. */ public static DoubleComparator naturalDoubleComparator() { return NaturalDoubleComparator.comparator; @@ -152,7 +153,7 @@ public class CommonOps { /** * Returns a reducer returning the maximum of two double elements, - * using natural comparator + * using natural comparator. */ public static DoubleReducer naturalDoubleMaxReducer() { return NaturalDoubleMaxReducer.max; @@ -167,7 +168,7 @@ public class CommonOps { /** * Returns a reducer returning the minimum of two double elements, - * using natural comparator + * using natural comparator. */ public static DoubleReducer naturalDoubleMinReducer() { return NaturalDoubleMinReducer.min; @@ -181,32 +182,32 @@ public class CommonOps { /** * Returns a reducer returning the maximum of two double elements, - * using the given comparator + * using the given comparator. */ public static DoubleReducer doubleMaxReducer (final DoubleComparator comparator) { return new DoubleReducer() { public double op(double a, double b) { - return (comparator.compare(a, b) >= 0)? a : b; + return (comparator.compare(a, b) >= 0) ? a : b; } }; } /** * Returns a reducer returning the minimum of two double elements, - * using the given comparator + * using the given comparator. */ public static DoubleReducer doubleMinReducer (final DoubleComparator comparator) { return new DoubleReducer() { public double op(double a, double b) { - return (comparator.compare(a, b) <= 0)? a : b; + return (comparator.compare(a, b) <= 0) ? a : b; } }; } /** - * Returns a comparator for longs relying on natural ordering + * Returns a comparator for longs relying on natural ordering. */ public static LongComparator naturalLongComparator() { return NaturalLongComparator.comparator; @@ -216,13 +217,13 @@ public class CommonOps { static final NaturalLongComparator comparator = new NaturalLongComparator(); public int compare(long a, long b) { - return a < b? -1 : ((a > b)? 1 : 0); + return (a < b) ? -1 : ((a > b) ? 1 : 0); } } /** * Returns a reducer returning the maximum of two long elements, - * using natural comparator + * using natural comparator. */ public static LongReducer naturalLongMaxReducer() { return NaturalLongMaxReducer.max; @@ -232,12 +233,12 @@ public class CommonOps { implements LongReducer { public static final NaturalLongMaxReducer max = new NaturalLongMaxReducer(); - public long op(long a, long b) { return a >= b? a : b; } + public long op(long a, long b) { return (a >= b) ? a : b; } } /** * A reducer returning the minimum of two long elements, - * using natural comparator + * using natural comparator. */ public static LongReducer naturalLongMinReducer() { return NaturalLongMinReducer.min; @@ -246,38 +247,38 @@ public class CommonOps { implements LongReducer { public static final NaturalLongMinReducer min = new NaturalLongMinReducer(); - public long op(long a, long b) { return a <= b? a : b; } + public long op(long a, long b) { return (a <= b) ? a : b; } } /** * Returns a reducer returning the maximum of two long elements, - * using the given comparator + * using the given comparator. */ public static LongReducer longMaxReducer (final LongComparator comparator) { return new LongReducer() { public long op(long a, long b) { - return (comparator.compare(a, b) >= 0)? a : b; + return (comparator.compare(a, b) >= 0) ? a : b; } }; } /** * Returns a reducer returning the minimum of two long elements, - * using the given comparator + * using the given comparator. */ public static LongReducer longMinReducer (final LongComparator comparator) { return new LongReducer() { public long op(long a, long b) { - return (comparator.compare(a, b) <= 0)? a : b; + return (comparator.compare(a, b) <= 0) ? a : b; } }; } /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static Op compoundOp (final Op first, @@ -289,7 +290,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static Op compoundOp (final ObjectToDouble first, @@ -301,7 +302,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static Op compoundOp (final ObjectToLong first, @@ -313,7 +314,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleToObject compoundOp (final DoubleToObject first, @@ -325,7 +326,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongToObject compoundOp (final LongToObject first, @@ -337,7 +338,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static ObjectToDouble compoundOp (final Op first, @@ -349,7 +350,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static ObjectToLong compoundOp (final Op first, @@ -361,7 +362,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static ObjectToDouble compoundOp (final ObjectToDouble first, @@ -373,7 +374,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static ObjectToLong compoundOp (final ObjectToDouble first, @@ -385,7 +386,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static ObjectToLong compoundOp (final ObjectToLong first, @@ -397,7 +398,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static ObjectToDouble compoundOp (final ObjectToLong first, @@ -409,7 +410,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleOp compoundOp (final DoubleOp first, @@ -421,7 +422,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleToLong compoundOp (final DoubleOp first, @@ -433,7 +434,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleToLong compoundOp (final DoubleToLong first, @@ -445,7 +446,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleToObject compoundOp (final DoubleToLong first, @@ -457,7 +458,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongToObject compoundOp (final LongToDouble first, @@ -469,7 +470,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongToDouble compoundOp (final LongOp first, @@ -481,7 +482,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongToDouble compoundOp (final LongToDouble first, @@ -493,7 +494,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleToObject compoundOp (final DoubleOp first, @@ -505,7 +506,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongToObject compoundOp (final LongOp first, @@ -517,11 +518,11 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleOp compoundOp (final DoubleToObject first, - final ObjectToDouble second) { + final ObjectToDouble second) { return new DoubleOp() { public final double op(double t) { return second.op(first.op(t)); } }; @@ -529,11 +530,11 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongToDouble compoundOp (final LongToObject first, - final ObjectToDouble second) { + final ObjectToDouble second) { return new LongToDouble() { public final double op(long t) { return second.op(first.op(t)); } }; @@ -541,11 +542,11 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleToLong compoundOp (final DoubleToObject first, - final ObjectToLong second) { + final ObjectToLong second) { return new DoubleToLong() { public final long op(double t) { return second.op(first.op(t)); } }; @@ -553,11 +554,11 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongOp compoundOp (final LongToObject first, - final ObjectToLong second) { + final ObjectToLong second) { return new LongOp() { public final long op(long t) { return second.op(first.op(t)); } }; @@ -565,7 +566,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongOp compoundOp (final LongOp first, @@ -577,7 +578,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static DoubleOp compoundOp (final DoubleToLong first, @@ -589,7 +590,7 @@ public class CommonOps { /** * Returns a composite mapper that applies a second mapper to the results - * of applying the first one + * of applying the first one. */ public static LongOp compoundOp (final LongToDouble first, @@ -600,7 +601,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the negation of its contained predicate + * Returns a predicate evaluating to the negation of its contained predicate. */ public static Predicate notPredicate (final Predicate pred) { @@ -610,7 +611,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the negation of its contained predicate + * Returns a predicate evaluating to the negation of its contained predicate. */ public static DoublePredicate notPredicate (final DoublePredicate pred) { @@ -620,7 +621,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the negation of its contained predicate + * Returns a predicate evaluating to the negation of its contained predicate. */ public static LongPredicate notPredicate (final LongPredicate pred) { @@ -630,7 +631,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the conjunction of its contained predicates + * Returns a predicate evaluating to the conjunction of its contained predicates. */ public static Predicate andPredicate (final Predicate first, @@ -643,7 +644,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the disjunction of its contained predicates + * Returns a predicate evaluating to the disjunction of its contained predicates. */ public static Predicate orPredicate (final Predicate first, @@ -656,7 +657,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the conjunction of its contained predicates + * Returns a predicate evaluating to the conjunction of its contained predicates. */ public static DoublePredicate andPredicate (final DoublePredicate first, @@ -669,7 +670,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the disjunction of its contained predicates + * Returns a predicate evaluating to the disjunction of its contained predicates. */ public static DoublePredicate orPredicate (final DoublePredicate first, @@ -683,7 +684,7 @@ public class CommonOps { /** - * Returns a predicate evaluating to the conjunction of its contained predicates + * Returns a predicate evaluating to the conjunction of its contained predicates. */ public static LongPredicate andPredicate (final LongPredicate first, @@ -696,7 +697,7 @@ public class CommonOps { } /** - * Returns a predicate evaluating to the disjunction of its contained predicates + * Returns a predicate evaluating to the disjunction of its contained predicates. */ public static LongPredicate orPredicate (final LongPredicate first, @@ -709,9 +710,9 @@ public class CommonOps { } /** - * Returns a predicate evaluating to true if its argument is non-null + * Returns a predicate evaluating to true if its argument is non-null. */ - public static Predicate isNonNullPredicate() { + public static Predicate isNonNullPredicate() { return IsNonNullPredicate.predicate; } static final class IsNonNullPredicate implements Predicate { @@ -723,9 +724,9 @@ public class CommonOps { } /** - * Returns a predicate evaluating to true if its argument is null + * Returns a predicate evaluating to true if its argument is null. */ - public static Predicate isNullPredicate() { + public static Predicate isNullPredicate() { return IsNullPredicate.predicate; } static final class IsNullPredicate implements Predicate { @@ -761,7 +762,7 @@ public class CommonOps { } /** - * Returns a reducer that adds two double elements + * Returns a reducer that adds two double elements. */ public static DoubleReducer doubleAdder() { return DoubleAdder.adder; } static final class DoubleAdder implements DoubleReducer { @@ -770,7 +771,7 @@ public class CommonOps { } /** - * Returns a reducer that adds two long elements + * Returns a reducer that adds two long elements. */ public static LongReducer longAdder() { return LongAdder.adder; } static final class LongAdder implements LongReducer { @@ -779,7 +780,7 @@ public class CommonOps { } /** - * Returns a reducer that adds two int elements + * Returns a reducer that adds two int elements. */ public static IntReducer intAdder() { return IntAdder.adder; } static final class IntAdder implements IntReducer { @@ -790,9 +791,7 @@ public class CommonOps { /** * Returns a generator producing uniform random values between * zero and one, with the same properties as {@link - * java.util.Random#nextDouble} but operating independently across - * ForkJoinWorkerThreads and usable only within forkjoin - * computations. + * java.util.Random#nextDouble}. */ public static DoubleGenerator doubleRandom() { return DoubleRandomGenerator.generator; @@ -801,16 +800,14 @@ public class CommonOps { static final DoubleRandomGenerator generator = new DoubleRandomGenerator(); public double op() { - return ForkJoinWorkerThread.nextRandomDouble(); + return ThreadLocalRandom.current().nextDouble(); } } /** * Returns a generator producing uniform random values between * zero and the given bound, with the same properties as {@link - * java.util.Random#nextDouble} but operating independently across - * ForkJoinWorkerThreads and usable only within forkjoin - * computations. + * java.util.Random#nextDouble}. * @param bound the upper bound (exclusive) of opd values */ public static DoubleGenerator doubleRandom(double bound) { @@ -820,15 +817,13 @@ public class CommonOps { final double bound; DoubleBoundedRandomGenerator(double bound) { this.bound = bound; } public double op() { - return ForkJoinWorkerThread.nextRandomDouble() * bound; + return ThreadLocalRandom.current().nextDouble() * bound; } } /** * Returns a generator producing uniform random values between the - * given least value (inclusive) and bound (exclusive), operating - * independently across ForkJoinWorkerThreads and usable only - * within forkjoin computations. + * given least value (inclusive) and bound (exclusive). * @param least the least value returned * @param bound the upper bound (exclusive) of opd values */ @@ -842,15 +837,13 @@ public class CommonOps { this.least = least; this.range = bound - least; } public double op() { - return ForkJoinWorkerThread.nextRandomDouble() * range + least; + return ThreadLocalRandom.current().nextDouble() * range + least; } } /** * Returns a generator producing uniform random values with the - * same properties as {@link java.util.Random#nextLong} but - * operating independently across ForkJoinWorkerThreads and usable - * only within forkjoin computations. + * same properties as {@link java.util.Random#nextLong}. */ public static LongGenerator longRandom() { return LongRandomGenerator.generator; @@ -859,15 +852,13 @@ public class CommonOps { static final LongRandomGenerator generator = new LongRandomGenerator(); public long op() { - return ForkJoinWorkerThread.nextRandomLong(); + return ThreadLocalRandom.current().nextLong(); } } /** * Returns a generator producing uniform random values with the - * same properties as {@link java.util.Random#nextInt(int)} but - * operating independently across ForkJoinWorkerThreads and usable - * only within forkjoin computations. + * same properties as {@link java.util.Random#nextInt(int)}. * @param bound the upper bound (exclusive) of opd values */ public static LongGenerator longRandom(long bound) { @@ -879,15 +870,13 @@ public class CommonOps { final long bound; LongBoundedRandomGenerator(long bound) { this.bound = bound; } public long op() { - return ForkJoinWorkerThread.nextRandomLong(bound); + return ThreadLocalRandom.current().nextLong(bound); } } /** * Returns a generator producing uniform random values between the - * given least value (inclusive) and bound (exclusive), operating - * independently across ForkJoinWorkerThreads and usable only - * within forkjoin computations. + * given least value (inclusive) and bound (exclusive). * @param least the least value returned * @param bound the upper bound (exclusive) of opd values */ @@ -903,15 +892,13 @@ public class CommonOps { this.least = least; this.range = bound - least; } public long op() { - return ForkJoinWorkerThread.nextRandomLong(range) + least; + return ThreadLocalRandom.current().nextLong(range) + least; } } /** * Returns a generator producing uniform random values with the - * same properties as {@link java.util.Random#nextInt} but - * operating independently across ForkJoinWorkerThreads and usable - * only within forkjoin computations. + * same properties as {@link java.util.Random#nextInt}. */ public static IntGenerator intRandom() { return IntRandomGenerator.generator; @@ -920,15 +907,13 @@ public class CommonOps { static final IntRandomGenerator generator = new IntRandomGenerator(); public int op() { - return ForkJoinWorkerThread.nextRandomInt(); + return ThreadLocalRandom.current().nextInt(); } } /** * Returns a generator producing uniform random values with the - * same properties as {@link java.util.Random#nextInt(int)} but - * operating independently across ForkJoinWorkerThreads and usable - * only within forkjoin computations. + * same properties as {@link java.util.Random#nextInt(int)}. * @param bound the upper bound (exclusive) of opd values */ public static IntGenerator intRandom(int bound) { @@ -940,15 +925,13 @@ public class CommonOps { final int bound; IntBoundedRandomGenerator(int bound) { this.bound = bound; } public int op() { - return ForkJoinWorkerThread.nextRandomInt(bound); + return ThreadLocalRandom.current().nextInt(bound); } } /** * Returns a generator producing uniform random values between the - * given least value (inclusive) and bound (exclusive), operating - * independently across ForkJoinWorkerThreads and usable only - * within forkjoin computations. + * given least value (inclusive) and bound (exclusive). * @param least the least value returned * @param bound the upper bound (exclusive) of opd values */ @@ -964,13 +947,13 @@ public class CommonOps { this.least = least; this.range = bound - least; } public int op() { - return ForkJoinWorkerThread.nextRandomInt(range) + least; + return ThreadLocalRandom.current().nextInt(range) + least; } } /** * Returns a predicate evaluating to true if the - * first argument equals the second + * first argument {@code equals} the second. */ public static BinaryPredicate equalityPredicate() { return EqualityPredicate.predicate; @@ -985,7 +968,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument == the second + * first argument {@code ==} the second. */ public static BinaryPredicate identityPredicate() { return IdentityPredicate.predicate; @@ -1000,7 +983,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument == the second + * first argument {@code ==} the second. */ public static BinaryIntPredicate intEqualityPredicate() { return IntEqualityPredicate.predicate; @@ -1015,7 +998,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument == the second + * first argument {@code ==} the second. */ public static BinaryLongPredicate longEqualityPredicate() { return LongEqualityPredicate.predicate; @@ -1030,7 +1013,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument == the second + * first argument {@code ==} the second. */ public static BinaryDoublePredicate doubleEqualityPredicate() { return DoubleEqualityPredicate.predicate; @@ -1046,7 +1029,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument !equals the second + * first argument {@code !equals} the second. */ public static BinaryPredicate inequalityPredicate() { return InequalityPredicate.predicate; @@ -1061,7 +1044,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument != the second + * first argument {@code !=} the second. */ public static BinaryPredicate nonidentityPredicate() { return NonidentityPredicate.predicate; @@ -1076,7 +1059,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument != the second + * first argument {@code !=} the second. */ public static BinaryIntPredicate intInequalityPredicate() { return IntInequalityPredicate.predicate; @@ -1091,7 +1074,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument == the second + * first argument {@code ==} the second. */ public static BinaryLongPredicate longInequalityPredicate() { return LongInequalityPredicate.predicate; @@ -1106,7 +1089,7 @@ public class CommonOps { /** * Returns a predicate evaluating to true if the - * first argument != the second + * first argument {@code !=} the second. */ public static BinaryDoublePredicate doubleInequalityPredicate() { return DoubleInequalityPredicate.predicate;