--- jsr166/src/extra166y/CommonOps.java 2009/01/06 14:30:57 1.1 +++ jsr166/src/extra166y/CommonOps.java 2013/02/05 17:46:29 1.7 @@ -1,7 +1,7 @@ /* * 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; @@ -17,7 +17,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 +35,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 +49,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 +64,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 +79,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 +100,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 +112,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 +130,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 +152,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 +167,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 +181,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 +216,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 +232,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 +246,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 +289,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 +301,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 +313,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 +325,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 +337,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 +349,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 +361,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 +373,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 +385,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 +397,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 +409,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 +421,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 +433,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 +445,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 +457,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 +469,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 +481,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 +493,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 +505,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 +517,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 +529,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 +541,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 +553,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 +565,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 +577,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 +589,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 +600,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 +610,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 +620,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 +630,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 +643,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 +656,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 +669,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 +683,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 +696,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 +709,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 +723,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 +761,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 +770,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 +779,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 +790,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 +799,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 +816,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 +836,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 +851,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 +869,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 +891,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 +906,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 +924,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 +946,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 +967,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 +982,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 +997,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 +1012,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 +1028,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 +1043,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 +1058,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 +1073,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 +1088,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;