From 7ab4a402ff4c0e874bbd84d4a358b0b0df287317 Mon Sep 17 00:00:00 2001 From: mduigou Date: Tue, 19 Feb 2013 11:56:49 -0800 Subject: [PATCH] 8004561: Additional functional interfaces, extension methods and name changes Summary: Adds additional functional interfaces for primitives and "Bi" (two operand). Adds utility extension methods. Includes some name changes for existing functional interfaces per EG decisions. Reviewed-by: briangoetz, darcy, chegar, dholmes --- .../time/chrono/HijrahDeviationReader.java | 8 +- .../util/concurrent/atomic/AtomicInteger.java | 8 +- .../concurrent/atomic/AtomicIntegerArray.java | 8 +- .../atomic/AtomicIntegerFieldUpdater.java | 8 +- .../util/concurrent/atomic/AtomicLong.java | 8 +- .../concurrent/atomic/AtomicLongArray.java | 8 +- .../atomic/AtomicLongFieldUpdater.java | 8 +- .../concurrent/atomic/AtomicReference.java | 8 +- .../atomic/AtomicReferenceArray.java | 8 +- .../atomic/AtomicReferenceFieldUpdater.java | 8 +- .../concurrent/atomic/DoubleAccumulator.java | 8 +- .../concurrent/atomic/LongAccumulator.java | 8 +- .../util/concurrent/atomic/Striped64.java | 8 +- .../java/util/function/BiConsumer.java | 50 +++++++++ .../java/util/function/BiFunction.java | 51 +++++++++ .../java/util/function/BiPredicate.java | 103 ++++++++++++++++++ .../java/util/function/BinaryOperator.java | 20 +--- .../java/util/function/BooleanSupplier.java | 44 ++++++++ .../function/{Block.java => Consumer.java} | 10 +- .../util/function/DoubleBinaryOperator.java | 13 +-- .../{DoubleBlock.java => DoubleConsumer.java} | 19 ++-- .../java/util/function/DoubleFunction.java | 19 ++-- .../java/util/function/DoublePredicate.java | 101 +++++++++++++++++ .../java/util/function/DoubleSupplier.java | 7 +- .../util/function/DoubleUnaryOperator.java | 8 +- .../classes/java/util/function/Function.java | 10 +- .../java/util/function/IntBinaryOperator.java | 5 +- .../{IntBlock.java => IntConsumer.java} | 19 ++-- .../java/util/function/IntFunction.java | 19 ++-- .../java/util/function/IntPredicate.java | 100 +++++++++++++++++ .../java/util/function/IntSupplier.java | 7 +- .../java/util/function/IntUnaryOperator.java | 9 +- .../util/function/LongBinaryOperator.java | 5 +- .../{LongBlock.java => LongConsumer.java} | 19 ++-- .../java/util/function/LongFunction.java | 19 ++-- .../java/util/function/LongPredicate.java | 100 +++++++++++++++++ .../java/util/function/LongSupplier.java | 9 +- .../java/util/function/LongUnaryOperator.java | 5 +- .../java/util/function/ObjDoubleConsumer.java | 48 ++++++++ .../java/util/function/ObjIntConsumer.java | 48 ++++++++ .../java/util/function/ObjLongConsumer.java | 48 ++++++++ .../classes/java/util/function/Predicate.java | 57 +++++++++- .../util/function/ToDoubleBiFunction.java | 50 +++++++++ .../java/util/function/ToDoubleFunction.java | 46 ++++++++ .../java/util/function/ToIntBiFunction.java | 50 +++++++++ .../java/util/function/ToIntFunction.java | 46 ++++++++ .../java/util/function/ToLongBiFunction.java | 50 +++++++++ .../java/util/function/ToLongFunction.java | 46 ++++++++ .../java/util/function/UnaryOperator.java | 16 +-- .../java/util/function/package-info.java | 69 +++++++++--- test/java/lang/PrimitiveSumMinMaxTest.java | 66 +++++------ 51 files changed, 1290 insertions(+), 225 deletions(-) create mode 100644 src/share/classes/java/util/function/BiConsumer.java create mode 100644 src/share/classes/java/util/function/BiFunction.java create mode 100644 src/share/classes/java/util/function/BiPredicate.java create mode 100644 src/share/classes/java/util/function/BooleanSupplier.java rename src/share/classes/java/util/function/{Block.java => Consumer.java} (84%) rename src/share/classes/java/util/function/{DoubleBlock.java => DoubleConsumer.java} (75%) create mode 100644 src/share/classes/java/util/function/DoublePredicate.java rename src/share/classes/java/util/function/{IntBlock.java => IntConsumer.java} (75%) create mode 100644 src/share/classes/java/util/function/IntPredicate.java rename src/share/classes/java/util/function/{LongBlock.java => LongConsumer.java} (75%) create mode 100644 src/share/classes/java/util/function/LongPredicate.java create mode 100644 src/share/classes/java/util/function/ObjDoubleConsumer.java create mode 100644 src/share/classes/java/util/function/ObjIntConsumer.java create mode 100644 src/share/classes/java/util/function/ObjLongConsumer.java create mode 100644 src/share/classes/java/util/function/ToDoubleBiFunction.java create mode 100644 src/share/classes/java/util/function/ToDoubleFunction.java create mode 100644 src/share/classes/java/util/function/ToIntBiFunction.java create mode 100644 src/share/classes/java/util/function/ToIntFunction.java create mode 100644 src/share/classes/java/util/function/ToLongBiFunction.java create mode 100644 src/share/classes/java/util/function/ToLongFunction.java diff --git a/src/share/classes/java/time/chrono/HijrahDeviationReader.java b/src/share/classes/java/time/chrono/HijrahDeviationReader.java index 67495d040..0c207af62 100644 --- a/src/share/classes/java/time/chrono/HijrahDeviationReader.java +++ b/src/share/classes/java/time/chrono/HijrahDeviationReader.java @@ -70,7 +70,7 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.util.Arrays; -import java.util.function.Block; +import java.util.function.Consumer; /** * A reader for Hijrah Deviation files. @@ -126,13 +126,13 @@ final class HijrahDeviationReader { * @param typeId the name of the calendar * @param calendarType the calendar type * @return {@code true} if the file was read and each entry accepted by the - * Block; else {@code false} no configuration was done + * Consumer; else {@code false} no configuration was done * * @throws IOException for zip/jar file handling exception. * @throws ParseException if the format of the configuration file is wrong. */ static boolean readDeviation(String typeId, String calendarType, - Block block) throws IOException, ParseException { + Consumer consumer) throws IOException, ParseException { InputStream is = getConfigFileInputStream(typeId); if (is != null) { try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { @@ -142,7 +142,7 @@ final class HijrahDeviationReader { num++; HijrahChronology.Deviation entry = parseLine(line, num); if (entry != null) { - block.accept(entry); + consumer.accept(entry); } } } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicInteger.java b/src/share/classes/java/util/concurrent/atomic/AtomicInteger.java index b48ef72df..8fed658a5 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicInteger.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicInteger.java @@ -219,7 +219,7 @@ public class AtomicInteger extends Number implements java.io.Serializable { int prev, next; do { prev = get(); - next = updateFunction.operateAsInt(prev); + next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return prev; } @@ -238,7 +238,7 @@ public class AtomicInteger extends Number implements java.io.Serializable { int prev, next; do { prev = get(); - next = updateFunction.operateAsInt(prev); + next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return next; } @@ -262,7 +262,7 @@ public class AtomicInteger extends Number implements java.io.Serializable { int prev, next; do { prev = get(); - next = accumulatorFunction.operateAsInt(prev, x); + next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSet(prev, next)); return prev; } @@ -286,7 +286,7 @@ public class AtomicInteger extends Number implements java.io.Serializable { int prev, next; do { prev = get(); - next = accumulatorFunction.operateAsInt(prev, x); + next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSet(prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java b/src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java index 6e4d226ba..7c259fee0 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java @@ -263,7 +263,7 @@ public class AtomicIntegerArray implements java.io.Serializable { int prev, next; do { prev = getRaw(offset); - next = updateFunction.operateAsInt(prev); + next = updateFunction.applyAsInt(prev); } while (!compareAndSetRaw(offset, prev, next)); return prev; } @@ -284,7 +284,7 @@ public class AtomicIntegerArray implements java.io.Serializable { int prev, next; do { prev = getRaw(offset); - next = updateFunction.operateAsInt(prev); + next = updateFunction.applyAsInt(prev); } while (!compareAndSetRaw(offset, prev, next)); return next; } @@ -310,7 +310,7 @@ public class AtomicIntegerArray implements java.io.Serializable { int prev, next; do { prev = getRaw(offset); - next = accumulatorFunction.operateAsInt(prev, x); + next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return prev; } @@ -336,7 +336,7 @@ public class AtomicIntegerArray implements java.io.Serializable { int prev, next; do { prev = getRaw(offset); - next = accumulatorFunction.operateAsInt(prev, x); + next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java b/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java index fcd5dd05f..4a8d1df5a 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java @@ -281,7 +281,7 @@ public abstract class AtomicIntegerFieldUpdater { int prev, next; do { prev = get(obj); - next = updateFunction.operateAsInt(prev); + next = updateFunction.applyAsInt(prev); } while (!compareAndSet(obj, prev, next)); return prev; } @@ -301,7 +301,7 @@ public abstract class AtomicIntegerFieldUpdater { int prev, next; do { prev = get(obj); - next = updateFunction.operateAsInt(prev); + next = updateFunction.applyAsInt(prev); } while (!compareAndSet(obj, prev, next)); return next; } @@ -326,7 +326,7 @@ public abstract class AtomicIntegerFieldUpdater { int prev, next; do { prev = get(obj); - next = accumulatorFunction.operateAsInt(prev, x); + next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSet(obj, prev, next)); return prev; } @@ -351,7 +351,7 @@ public abstract class AtomicIntegerFieldUpdater { int prev, next; do { prev = get(obj); - next = accumulatorFunction.operateAsInt(prev, x); + next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSet(obj, prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicLong.java b/src/share/classes/java/util/concurrent/atomic/AtomicLong.java index 2e58d29b2..35af7759a 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicLong.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicLong.java @@ -233,7 +233,7 @@ public class AtomicLong extends Number implements java.io.Serializable { long prev, next; do { prev = get(); - next = updateFunction.operateAsLong(prev); + next = updateFunction.applyAsLong(prev); } while (!compareAndSet(prev, next)); return prev; } @@ -252,7 +252,7 @@ public class AtomicLong extends Number implements java.io.Serializable { long prev, next; do { prev = get(); - next = updateFunction.operateAsLong(prev); + next = updateFunction.applyAsLong(prev); } while (!compareAndSet(prev, next)); return next; } @@ -276,7 +276,7 @@ public class AtomicLong extends Number implements java.io.Serializable { long prev, next; do { prev = get(); - next = accumulatorFunction.operateAsLong(prev, x); + next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSet(prev, next)); return prev; } @@ -300,7 +300,7 @@ public class AtomicLong extends Number implements java.io.Serializable { long prev, next; do { prev = get(); - next = accumulatorFunction.operateAsLong(prev, x); + next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSet(prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicLongArray.java b/src/share/classes/java/util/concurrent/atomic/AtomicLongArray.java index 2536a9b8e..216479abe 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicLongArray.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicLongArray.java @@ -262,7 +262,7 @@ public class AtomicLongArray implements java.io.Serializable { long prev, next; do { prev = getRaw(offset); - next = updateFunction.operateAsLong(prev); + next = updateFunction.applyAsLong(prev); } while (!compareAndSetRaw(offset, prev, next)); return prev; } @@ -283,7 +283,7 @@ public class AtomicLongArray implements java.io.Serializable { long prev, next; do { prev = getRaw(offset); - next = updateFunction.operateAsLong(prev); + next = updateFunction.applyAsLong(prev); } while (!compareAndSetRaw(offset, prev, next)); return next; } @@ -309,7 +309,7 @@ public class AtomicLongArray implements java.io.Serializable { long prev, next; do { prev = getRaw(offset); - next = accumulatorFunction.operateAsLong(prev, x); + next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return prev; } @@ -335,7 +335,7 @@ public class AtomicLongArray implements java.io.Serializable { long prev, next; do { prev = getRaw(offset); - next = accumulatorFunction.operateAsLong(prev, x); + next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java b/src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java index a7672263a..e5c6e8a00 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java @@ -284,7 +284,7 @@ public abstract class AtomicLongFieldUpdater { long prev, next; do { prev = get(obj); - next = updateFunction.operateAsLong(prev); + next = updateFunction.applyAsLong(prev); } while (!compareAndSet(obj, prev, next)); return prev; } @@ -304,7 +304,7 @@ public abstract class AtomicLongFieldUpdater { long prev, next; do { prev = get(obj); - next = updateFunction.operateAsLong(prev); + next = updateFunction.applyAsLong(prev); } while (!compareAndSet(obj, prev, next)); return next; } @@ -329,7 +329,7 @@ public abstract class AtomicLongFieldUpdater { long prev, next; do { prev = get(obj); - next = accumulatorFunction.operateAsLong(prev, x); + next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSet(obj, prev, next)); return prev; } @@ -354,7 +354,7 @@ public abstract class AtomicLongFieldUpdater { long prev, next; do { prev = get(obj); - next = accumulatorFunction.operateAsLong(prev, x); + next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSet(obj, prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicReference.java b/src/share/classes/java/util/concurrent/atomic/AtomicReference.java index 95b88d127..ddce07343 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicReference.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicReference.java @@ -157,7 +157,7 @@ public class AtomicReference implements java.io.Serializable { V prev, next; do { prev = get(); - next = updateFunction.operate(prev); + next = updateFunction.apply(prev); } while (!compareAndSet(prev, next)); return prev; } @@ -176,7 +176,7 @@ public class AtomicReference implements java.io.Serializable { V prev, next; do { prev = get(); - next = updateFunction.operate(prev); + next = updateFunction.apply(prev); } while (!compareAndSet(prev, next)); return next; } @@ -200,7 +200,7 @@ public class AtomicReference implements java.io.Serializable { V prev, next; do { prev = get(); - next = accumulatorFunction.operate(prev, x); + next = accumulatorFunction.apply(prev, x); } while (!compareAndSet(prev, next)); return prev; } @@ -224,7 +224,7 @@ public class AtomicReference implements java.io.Serializable { V prev, next; do { prev = get(); - next = accumulatorFunction.operate(prev, x); + next = accumulatorFunction.apply(prev, x); } while (!compareAndSet(prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java b/src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java index 3e6ce9698..f8787ba1d 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java @@ -217,7 +217,7 @@ public class AtomicReferenceArray implements java.io.Serializable { E prev, next; do { prev = getRaw(offset); - next = updateFunction.operate(prev); + next = updateFunction.apply(prev); } while (!compareAndSetRaw(offset, prev, next)); return prev; } @@ -238,7 +238,7 @@ public class AtomicReferenceArray implements java.io.Serializable { E prev, next; do { prev = getRaw(offset); - next = updateFunction.operate(prev); + next = updateFunction.apply(prev); } while (!compareAndSetRaw(offset, prev, next)); return next; } @@ -264,7 +264,7 @@ public class AtomicReferenceArray implements java.io.Serializable { E prev, next; do { prev = getRaw(offset); - next = accumulatorFunction.operate(prev, x); + next = accumulatorFunction.apply(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return prev; } @@ -290,7 +290,7 @@ public class AtomicReferenceArray implements java.io.Serializable { E prev, next; do { prev = getRaw(offset); - next = accumulatorFunction.operate(prev, x); + next = accumulatorFunction.apply(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java b/src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java index a92d914ff..933e5e4fc 100644 --- a/src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java +++ b/src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java @@ -200,7 +200,7 @@ public abstract class AtomicReferenceFieldUpdater { V prev, next; do { prev = get(obj); - next = updateFunction.operate(prev); + next = updateFunction.apply(prev); } while (!compareAndSet(obj, prev, next)); return prev; } @@ -220,7 +220,7 @@ public abstract class AtomicReferenceFieldUpdater { V prev, next; do { prev = get(obj); - next = updateFunction.operate(prev); + next = updateFunction.apply(prev); } while (!compareAndSet(obj, prev, next)); return next; } @@ -245,7 +245,7 @@ public abstract class AtomicReferenceFieldUpdater { V prev, next; do { prev = get(obj); - next = accumulatorFunction.operate(prev, x); + next = accumulatorFunction.apply(prev, x); } while (!compareAndSet(obj, prev, next)); return prev; } @@ -270,7 +270,7 @@ public abstract class AtomicReferenceFieldUpdater { V prev, next; do { prev = get(obj); - next = accumulatorFunction.operate(prev, x); + next = accumulatorFunction.apply(prev, x); } while (!compareAndSet(obj, prev, next)); return next; } diff --git a/src/share/classes/java/util/concurrent/atomic/DoubleAccumulator.java b/src/share/classes/java/util/concurrent/atomic/DoubleAccumulator.java index 17556500a..277f185f8 100644 --- a/src/share/classes/java/util/concurrent/atomic/DoubleAccumulator.java +++ b/src/share/classes/java/util/concurrent/atomic/DoubleAccumulator.java @@ -100,14 +100,14 @@ public class DoubleAccumulator extends Striped64 implements Serializable { Cell[] as; long b, v, r; int m; Cell a; if ((as = cells) != null || (r = Double.doubleToRawLongBits - (function.operateAsDouble + (function.applyAsDouble (Double.longBitsToDouble(b = base), x))) != b && !casBase(b, r)) { boolean uncontended = true; if (as == null || (m = as.length - 1) < 0 || (a = as[getProbe() & m]) == null || !(uncontended = (r = Double.doubleToRawLongBits - (function.operateAsDouble + (function.applyAsDouble (Double.longBitsToDouble(v = a.value), x))) == v || a.cas(v, r))) doubleAccumulate(x, function, uncontended); @@ -129,7 +129,7 @@ public class DoubleAccumulator extends Striped64 implements Serializable { if (as != null) { for (int i = 0; i < as.length; ++i) { if ((a = as[i]) != null) - result = function.operateAsDouble + result = function.applyAsDouble (result, Double.longBitsToDouble(a.value)); } } @@ -174,7 +174,7 @@ public class DoubleAccumulator extends Striped64 implements Serializable { if ((a = as[i]) != null) { double v = Double.longBitsToDouble(a.value); a.value = identity; - result = function.operateAsDouble(result, v); + result = function.applyAsDouble(result, v); } } } diff --git a/src/share/classes/java/util/concurrent/atomic/LongAccumulator.java b/src/share/classes/java/util/concurrent/atomic/LongAccumulator.java index 855b09e44..bfddcd3dc 100644 --- a/src/share/classes/java/util/concurrent/atomic/LongAccumulator.java +++ b/src/share/classes/java/util/concurrent/atomic/LongAccumulator.java @@ -101,12 +101,12 @@ public class LongAccumulator extends Striped64 implements Serializable { public void accumulate(long x) { Cell[] as; long b, v, r; int m; Cell a; if ((as = cells) != null || - (r = function.operateAsLong(b = base, x)) != b && !casBase(b, r)) { + (r = function.applyAsLong(b = base, x)) != b && !casBase(b, r)) { boolean uncontended = true; if (as == null || (m = as.length - 1) < 0 || (a = as[getProbe() & m]) == null || !(uncontended = - (r = function.operateAsLong(v = a.value, x)) == v || + (r = function.applyAsLong(v = a.value, x)) == v || a.cas(v, r))) longAccumulate(x, function, uncontended); } @@ -127,7 +127,7 @@ public class LongAccumulator extends Striped64 implements Serializable { if (as != null) { for (int i = 0; i < as.length; ++i) { if ((a = as[i]) != null) - result = function.operateAsLong(result, a.value); + result = function.applyAsLong(result, a.value); } } return result; @@ -171,7 +171,7 @@ public class LongAccumulator extends Striped64 implements Serializable { if ((a = as[i]) != null) { long v = a.value; a.value = identity; - result = function.operateAsLong(result, v); + result = function.applyAsLong(result, v); } } } diff --git a/src/share/classes/java/util/concurrent/atomic/Striped64.java b/src/share/classes/java/util/concurrent/atomic/Striped64.java index 03969d76d..708b4c257 100644 --- a/src/share/classes/java/util/concurrent/atomic/Striped64.java +++ b/src/share/classes/java/util/concurrent/atomic/Striped64.java @@ -253,7 +253,7 @@ abstract class Striped64 extends Number { else if (!wasUncontended) // CAS already known to fail wasUncontended = true; // Continue after rehash else if (a.cas(v = a.value, ((fn == null) ? v + x : - fn.operateAsLong(v, x)))) + fn.applyAsLong(v, x)))) break; else if (n >= NCPU || cells != as) collide = false; // At max size or stale @@ -291,7 +291,7 @@ abstract class Striped64 extends Number { break; } else if (casBase(v = base, ((fn == null) ? v + x : - fn.operateAsLong(v, x)))) + fn.applyAsLong(v, x)))) break; // Fall back on using base } } @@ -344,7 +344,7 @@ abstract class Striped64 extends Number { Double.doubleToRawLongBits (Double.longBitsToDouble(v) + x) : Double.doubleToRawLongBits - (fn.operateAsDouble + (fn.applyAsDouble (Double.longBitsToDouble(v), x))))) break; else if (n >= NCPU || cells != as) @@ -387,7 +387,7 @@ abstract class Striped64 extends Number { Double.doubleToRawLongBits (Double.longBitsToDouble(v) + x) : Double.doubleToRawLongBits - (fn.operateAsDouble + (fn.applyAsDouble (Double.longBitsToDouble(v), x))))) break; // Fall back on using base } diff --git a/src/share/classes/java/util/function/BiConsumer.java b/src/share/classes/java/util/function/BiConsumer.java new file mode 100644 index 000000000..80bdcfa3b --- /dev/null +++ b/src/share/classes/java/util/function/BiConsumer.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * An operation which accepts two input arguments and returns no result. This is + * the two-arity specialization of {@link Consumer}. Unlike most other + * functional interfaces, {@code BiConsumer} is expected to operate via + * side-effects. + * + * @param the type of the first argument to the {@code accept} operation. + * @param the type of the second argument to the {@code accept} operation. + * + * @see Consumer + * @since 1.8 + */ +@FunctionalInterface +public interface BiConsumer { + + /** + * Performs operations upon the provided objects which may modify those + * objects and/or external state. + * + * @param t an input object + * @param u an input object + */ + void accept(T t, U u); +} diff --git a/src/share/classes/java/util/function/BiFunction.java b/src/share/classes/java/util/function/BiFunction.java new file mode 100644 index 000000000..85dc2a0a3 --- /dev/null +++ b/src/share/classes/java/util/function/BiFunction.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input arguments, yielding an appropriate result. This + * is the two-arity specialization of {@link Function}. A function may + * variously provide a mapping between types, object instances or keys and + * values or any other form of transformation upon the input. + * + * @param the type of the first argument to the {@code apply} operation. + * @param the type of the second argument to the {@code apply} operation. + * @param the type of results returned by the {@code apply} operation. + * + * @see Function + * @since 1.8 + */ +@FunctionalInterface +public interface BiFunction { + + /** + * Compute the result of applying the function to the input arguments + * + * @param t an input object + * @param u an input object + * @return the function result + */ + R apply(T t, U u); +} diff --git a/src/share/classes/java/util/function/BiPredicate.java b/src/share/classes/java/util/function/BiPredicate.java new file mode 100644 index 000000000..9a8665fa1 --- /dev/null +++ b/src/share/classes/java/util/function/BiPredicate.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +import java.util.Objects; + +/** + * Determines if the input objects match some criteria. This is the two-arity + * specialization of {@link Predicate}. + * + * @param the type of the first argument to {@code test}. + * @param the type of the second argument to {@code test}. + * + * @see Predicate + * @since 1.8 + */ +@FunctionalInterface +public interface BiPredicate { + + /** + * Return {@code true} if the inputs match some criteria. + * + * @param t an input object. + * @param u an input object. + * @return {@code true} if the inputs match some criteria. + */ + boolean test(T t, U u); + + /** + * Returns a predicate which evaluates to {@code true} only if this + * predicate and the provided predicate both evaluate to {@code true}. If + * this predicate returns {@code false} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ANDed with this predicate. + * @return a new predicate which returns {@code true} only if both + * predicates return {@code true}. + */ + public default BiPredicate and(BiPredicate p) { + Objects.requireNonNull(p); + return (T t, U u) -> test(t, u) && p.test(t, u); + } + + /** + * Returns a predicate which negates the result of this predicate. + * + * @return a new predicate who's result is always the opposite of this + * predicate. + */ + public default BiPredicate negate() { + return (T t, U u) -> !test(t, u); + } + + /** + * Returns a predicate which evaluates to {@code true} if either this + * predicate or the provided predicate evaluates to {@code true}. If this + * predicate returns {@code true} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ORed with this predicate. + * @return a new predicate which returns {@code true} if either predicate + * returns {@code true}. + */ + public default BiPredicate or(BiPredicate p) { + Objects.requireNonNull(p); + return (T t, U u) -> test(t, u) || p.test(t, u); + } + + /** + * Returns a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + * + * @param p a predicate which will be logically-XORed with this predicate. + * @return a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + */ + public default BiPredicate xor(BiPredicate p) { + Objects.requireNonNull(p); + return (T t, U u) -> test(t, u) ^ p.test(t, u); + } +} diff --git a/src/share/classes/java/util/function/BinaryOperator.java b/src/share/classes/java/util/function/BinaryOperator.java index 2e9050e59..5f1d5a41d 100644 --- a/src/share/classes/java/util/function/BinaryOperator.java +++ b/src/share/classes/java/util/function/BinaryOperator.java @@ -25,24 +25,14 @@ package java.util.function; /** - * An operation upon two operands yielding a result. The operands and the result - * are all of the same type. + * An operation upon two operands yielding a result. This is a specialization of + * {@code BiFunction} where the operands and the result are all of the same type. * - * @param the type of operands to {@code operate} and of the result + * @param the type of operands to {@code apply} and of the result * + * @see BiFunction. * @since 1.8 */ @FunctionalInterface -public interface BinaryOperator { - - /** - * Returns the result of the operation upon the operands. - * The operands are named {@code left} and {@code right} for operations - * where the order of operands matters. - * - * @param left the left operand - * @param right the right operand - * @return the result of the operation - */ - public T operate(T left, T right); +public interface BinaryOperator extends BiFunction { } diff --git a/src/share/classes/java/util/function/BooleanSupplier.java b/src/share/classes/java/util/function/BooleanSupplier.java new file mode 100644 index 000000000..f145b6211 --- /dev/null +++ b/src/share/classes/java/util/function/BooleanSupplier.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + + +/** + * A supplier of {@code boolean} values. This is the {@code boolean}-providing + * primitive specialization of {@link Supplier}. + * + * @see Supplier + * @since 1.8 + */ +@FunctionalInterface +public interface BooleanSupplier { + + /** + * Returns a {@code boolean} value. + * + * @return a {@code boolean} value + */ + public boolean getAsBoolean(); +} diff --git a/src/share/classes/java/util/function/Block.java b/src/share/classes/java/util/function/Consumer.java similarity index 84% rename from src/share/classes/java/util/function/Block.java rename to src/share/classes/java/util/function/Consumer.java index c41e9a45a..e9cb175d5 100644 --- a/src/share/classes/java/util/function/Block.java +++ b/src/share/classes/java/util/function/Consumer.java @@ -25,19 +25,19 @@ package java.util.function; /** - * An operation upon an input object. The operation may modify that object or - * external state (other objects). + * An operation which accepts a single input argument and returns no result. + * Unlike most other functional interfaces, {@code Consumer} is expected to + * operate via side-effects. * * @param The type of input objects to {@code accept} * * @since 1.8 */ @FunctionalInterface -public interface Block { +public interface Consumer { /** - * Use the input object in operations which may modify that object or - * external state (other objects). + * Accept an input value. * * @param t the input object */ diff --git a/src/share/classes/java/util/function/DoubleBinaryOperator.java b/src/share/classes/java/util/function/DoubleBinaryOperator.java index f63403bb3..de9855bd4 100644 --- a/src/share/classes/java/util/function/DoubleBinaryOperator.java +++ b/src/share/classes/java/util/function/DoubleBinaryOperator.java @@ -26,23 +26,22 @@ package java.util.function; /** * An operation on two {@code double} operands yielding a {@code double} result. + * This is the primitive type specialization of {@link BinaryOperator} for + * {@code double}. * + * @see BinaryOperator * @since 1.8 */ @FunctionalInterface -public interface DoubleBinaryOperator /* extends BinaryOperator */ { -// -// @Override -// public default Double operate(Double left, Double right) { return operateAsDouble((double) left, (double) right); } - +public interface DoubleBinaryOperator { /** * Returns the {@code double} result of the operation upon the * {@code double} operands. The parameters are named {@code left} and * {@code right} for operations where the order of parameters matters. * * @param left the left operand value - * @param right the right operand value + * @param right the right operand value * @return the result of the operation */ - public double operateAsDouble(double left, double right); + public double applyAsDouble(double left, double right); } diff --git a/src/share/classes/java/util/function/DoubleBlock.java b/src/share/classes/java/util/function/DoubleConsumer.java similarity index 75% rename from src/share/classes/java/util/function/DoubleBlock.java rename to src/share/classes/java/util/function/DoubleConsumer.java index ae000841a..2ea357497 100644 --- a/src/share/classes/java/util/function/DoubleBlock.java +++ b/src/share/classes/java/util/function/DoubleConsumer.java @@ -25,22 +25,21 @@ package java.util.function; /** - * An operation upon a {@code double} input value. The operation may modify - * external state. - * - *

This is the primitive type specialization of {@link Block} for - * {@code double} and also may be used as a {@code Block}. + * An operation which accepts a single double argument and returns no result. + * This is the primitive type specialization of {@link Consumer} for + * {@code double}. Unlike most other functional interfaces, + * {@code DoubleConsumer} is expected to operate via side-effects. * + * @see Consumer * @since 1.8 */ @FunctionalInterface -public interface DoubleBlock { +public interface DoubleConsumer { /** - * Use the {@code double} input value in an operation which may modify - * external state. + * Accept an input value. * - * @param t the input value + * @param value the input value */ - public void accept(double t); + public void accept(double value); } diff --git a/src/share/classes/java/util/function/DoubleFunction.java b/src/share/classes/java/util/function/DoubleFunction.java index d9c522c8e..fcc6bb9df 100644 --- a/src/share/classes/java/util/function/DoubleFunction.java +++ b/src/share/classes/java/util/function/DoubleFunction.java @@ -25,22 +25,23 @@ package java.util.function; /** - * Apply a function to the input object yielding an appropriate {@code double} - * value; this is the {@code double}-bearing specialization for {@link Function}. + * Apply a function to the double-valued input argument, yielding an appropriate + * result. This is the {@code double}-consuming primitive specialization for + * {@link Function}. * - * @param the type of input objects to the function + * @param the type of output objects from the function * + * @see Function * @since 1.8 */ @FunctionalInterface -public interface DoubleFunction { +public interface DoubleFunction { /** - * Apply a function to the input object yielding an appropriate - * {@code double} value. + * Compute the result of applying the function to the input argument * - * @param t the input object - * @return the function result value + * @param value the input value + * @return the function result */ - public double applyAsDouble(T t); + public R apply(double value); } diff --git a/src/share/classes/java/util/function/DoublePredicate.java b/src/share/classes/java/util/function/DoublePredicate.java new file mode 100644 index 000000000..e132f9b74 --- /dev/null +++ b/src/share/classes/java/util/function/DoublePredicate.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +import java.util.Objects; + +/** + * Determines if the {@code double} input value matches some criteria. This is + * the {@code double}-consuming primitive type specialization of + * {@link Predicate}. + * + * @see Predicate + * @since 1.8 + */ +@FunctionalInterface +public interface DoublePredicate { + + /** + * Returns {@code true} if the input value matches some criteria. + * + * @param value the value to be tested. + * @return {@code true} if the input value matches some criteria, otherwise + * {@code false}. + */ + public boolean test(double value); + + /** + * Returns a predicate which evaluates to {@code true} only if this + * predicate and the provided predicate both evaluate to {@code true}. If + * this predicate returns {@code false} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ANDed with this predicate. + * @return a new predicate which returns {@code true} only if both + * predicates return {@code true}. + */ + public default DoublePredicate and(DoublePredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) && p.test(value); + } + + /** + * Returns a predicate which negates the result of this predicate. + * + * @return a new predicate who's result is always the opposite of this + * predicate. + */ + public default DoublePredicate negate() { + return (value) -> !test(value); + } + + /** + * Returns a predicate which evaluates to {@code true} if either this + * predicate or the provided predicate evaluates to {@code true}. If this + * predicate returns {@code true} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ANDed with this predicate. + * @return a new predicate which returns {@code true} if either predicate + * returns {@code true}. + */ + public default DoublePredicate or(DoublePredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) || p.test(value); + } + + /** + * Returns a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + * + * @param p a predicate which will be logically-XORed with this predicate. + * @return a predicate that evaluates to {@code true} if all or none of the + * component predicates evaluate to {@code true}. + */ + public default DoublePredicate xor(DoublePredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) ^ p.test(value); + } +} diff --git a/src/share/classes/java/util/function/DoubleSupplier.java b/src/share/classes/java/util/function/DoubleSupplier.java index 19d753530..9f9ab4e9f 100644 --- a/src/share/classes/java/util/function/DoubleSupplier.java +++ b/src/share/classes/java/util/function/DoubleSupplier.java @@ -25,11 +25,10 @@ package java.util.function; /** - * A supplier of {@code double} values. - * - *

This is the primitive type specialization of {@link Supplier} for - * {@code double} and also may be used as a {@code Supplier}. + * A supplier of {@code double} values. This is the {@code double}-providing + * primitive specialization of {@link Supplier}. * + * @see Supplier * @since 1.8 */ @FunctionalInterface diff --git a/src/share/classes/java/util/function/DoubleUnaryOperator.java b/src/share/classes/java/util/function/DoubleUnaryOperator.java index 3843f8bc7..e9be79f5b 100644 --- a/src/share/classes/java/util/function/DoubleUnaryOperator.java +++ b/src/share/classes/java/util/function/DoubleUnaryOperator.java @@ -25,9 +25,11 @@ package java.util.function; /** - * An operation on a single {@code double} operand yielding a {@code double} - * result. + * An operation on a {@code double} operand yielding a {@code double} + * result. This is the primitive type specialization of {@link UnaryOperator} + * for {@code double}. * + * @see UnaryOperator * @since 1.8 */ @FunctionalInterface @@ -40,5 +42,5 @@ public interface DoubleUnaryOperator { * @param operand the operand value * @return the operation result value */ - public double operateAsDouble(double operand); + public double applyAsDouble(double operand); } diff --git a/src/share/classes/java/util/function/Function.java b/src/share/classes/java/util/function/Function.java index 82bae0170..ff4b538c1 100644 --- a/src/share/classes/java/util/function/Function.java +++ b/src/share/classes/java/util/function/Function.java @@ -24,14 +24,14 @@ */ package java.util.function; + /** - * Apply a function to the input object yielding an appropriate result object. A + * Apply a function to the input argument, yielding an appropriate result. A * function may variously provide a mapping between types, object instances or * keys and values or any other form of transformation upon the input. * - * @param the type of input objects to the {@code apply} operation - * @param the type of result objects from the {@code apply} operation. May - * be the same type as {@code }. + * @param the type of the input to the {@code apply} operation. + * @param the type of the result of the {@code apply} operation. * * @since 1.8 */ @@ -39,7 +39,7 @@ package java.util.function; public interface Function { /** - * Yield an appropriate result object for the input object. + * Compute the result of applying the function to the input argument * * @param t the input object * @return the function result diff --git a/src/share/classes/java/util/function/IntBinaryOperator.java b/src/share/classes/java/util/function/IntBinaryOperator.java index e25c2ba10..f139118e3 100644 --- a/src/share/classes/java/util/function/IntBinaryOperator.java +++ b/src/share/classes/java/util/function/IntBinaryOperator.java @@ -26,7 +26,10 @@ package java.util.function; /** * An operation on two {@code int} operands yielding an {@code int} result. + * This is the primitive type specialization of {@link BinaryOperator} for + * {@code int}. * + * @see BinaryOperator * @since 1.8 */ @FunctionalInterface @@ -41,5 +44,5 @@ public interface IntBinaryOperator { * @param right the right operand value * @return the result of the operation */ - public int operateAsInt(int left, int right); + public int applyAsInt(int left, int right); } diff --git a/src/share/classes/java/util/function/IntBlock.java b/src/share/classes/java/util/function/IntConsumer.java similarity index 75% rename from src/share/classes/java/util/function/IntBlock.java rename to src/share/classes/java/util/function/IntConsumer.java index 33cdbe79f..28a6ec46e 100644 --- a/src/share/classes/java/util/function/IntBlock.java +++ b/src/share/classes/java/util/function/IntConsumer.java @@ -25,22 +25,21 @@ package java.util.function; /** - * An operation upon an {@code int} input value. The operation may modify - * external state. - * - *

This is the primitive type specialization of {@link Block} for - * {@code int} and also may be used as a {@code Block}. + * An operation which accepts a single integer argument and returns no result. + * This is the primitive type specialization of {@link Consumer} for {@code int}. + * Unlike most other functional interfaces, {@code IntConsumer} is expected to + * operate via side-effects. * + * @see Consumer * @since 1.8 */ @FunctionalInterface -public interface IntBlock { +public interface IntConsumer { /** - * Use the {@code int} input value in an operation which may modify external - * state. + * Accept an input value. * - * @param t the input value + * @param value the input value */ - public void accept(int t); + public void accept(int value); } diff --git a/src/share/classes/java/util/function/IntFunction.java b/src/share/classes/java/util/function/IntFunction.java index 6e1418533..b5faecc0e 100644 --- a/src/share/classes/java/util/function/IntFunction.java +++ b/src/share/classes/java/util/function/IntFunction.java @@ -25,22 +25,23 @@ package java.util.function; /** - * Apply a function to the input object yielding an appropriate {@code int} - * value; this is the {@code int}-bearing specialization for {@link Function}. + * Apply a function to the integer-valued input argument, yielding an + * appropriate result. This is the {@code int}-consuming primitive + * specialization for {@link Function}. * - * @param the type of input objects to the function + * @param the type of output objects from the function * + * @see Function * @since 1.8 */ @FunctionalInterface -public interface IntFunction { +public interface IntFunction { /** - * Apply a function to the input object yielding an appropriate {@code int} - * value. + * Compute the result of applying the function to the input argument * - * @param t the input object - * @return the function result value + * @param value the input value + * @return the function result */ - public int applyAsInt(T t); + public R apply(int value); } diff --git a/src/share/classes/java/util/function/IntPredicate.java b/src/share/classes/java/util/function/IntPredicate.java new file mode 100644 index 000000000..17cc49c75 --- /dev/null +++ b/src/share/classes/java/util/function/IntPredicate.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +import java.util.Objects; + +/** + * Determines if the {@code int} input value matches some criteria. This is the + * {@code int}-consuming primitive type specialization of {@link Predicate}. + * + * @see Predicate + * @since 1.8 + */ +@FunctionalInterface +public interface IntPredicate { + + /** + * Returns {@code true} if the input value matches some criteria. + * + * @param value the value to be tested. + * @return {@code true} if the input value matches some criteria, otherwise + * {@code false} + */ + public boolean test(int value); + + /** + * Returns a predicate which evaluates to {@code true} only if this + * predicate and the provided predicate both evaluate to {@code true}. If + * this predicate returns {@code false} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ANDed with this predicate. + * @return a new predicate which returns {@code true} only if both + * predicates return {@code true}. + */ + public default IntPredicate and(IntPredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) && p.test(value); + } + + /** + * Returns a predicate which negates the result of this predicate. + * + * @return a new predicate who's result is always the opposite of this + * predicate. + */ + public default IntPredicate negate() { + return (value) -> !test(value); + } + + /** + * Returns a predicate which evaluates to {@code true} if either this + * predicate or the provided predicate evaluates to {@code true}. If this + * predicate returns {@code true} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ORed with this predicate. + * @return a new predicate which returns {@code true} if either predicate + * returns {@code true}. + */ + public default IntPredicate or(IntPredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) || p.test(value); + } + + /** + * Returns a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + * + * @param p a predicate which will be logically-XORed with this predicate. + * @return a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true} + */ + public default IntPredicate xor(IntPredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) ^ p.test(value); + } +} diff --git a/src/share/classes/java/util/function/IntSupplier.java b/src/share/classes/java/util/function/IntSupplier.java index c73fc84e3..d857b7882 100644 --- a/src/share/classes/java/util/function/IntSupplier.java +++ b/src/share/classes/java/util/function/IntSupplier.java @@ -25,11 +25,10 @@ package java.util.function; /** - * A supplier of {@code int} values. - * - *

This is the primitive type specialization of {@link Supplier} for - * {@code int} and also may be used as a {@code Supplier}. + * A supplier of {@code int} values. This is the {@code int}-providing + * primitive specialization of {@link Supplier}. * + * @see Supplier * @since 1.8 */ @FunctionalInterface diff --git a/src/share/classes/java/util/function/IntUnaryOperator.java b/src/share/classes/java/util/function/IntUnaryOperator.java index 2c429951c..a3b868dff 100644 --- a/src/share/classes/java/util/function/IntUnaryOperator.java +++ b/src/share/classes/java/util/function/IntUnaryOperator.java @@ -26,18 +26,21 @@ package java.util.function; /** * An operation on a single {@code int} operand yielding an {@code int} result. + * This is the primitive type specialization of {@link UnaryOperator} for + * {@code int}. * + * @see UnaryOperator * @since 1.8 */ @FunctionalInterface public interface IntUnaryOperator { /** - * Returns the {@code int} result of the operation upon the {@code int} - * operand. + * Returns the {@code int} value result of the operation upon the + * {@code int} operand. * * @param operand the operand value * @return the operation result value */ - public int operateAsInt(int operand); + public int applyAsInt(int operand); } diff --git a/src/share/classes/java/util/function/LongBinaryOperator.java b/src/share/classes/java/util/function/LongBinaryOperator.java index f767c92a3..d08271d30 100644 --- a/src/share/classes/java/util/function/LongBinaryOperator.java +++ b/src/share/classes/java/util/function/LongBinaryOperator.java @@ -26,7 +26,10 @@ package java.util.function; /** * An operation on two {@code long} operands yielding a {@code long} result. + * This is the primitive type specialization of {@link BinaryOperator} for + * {@code long}. * + * @see BinaryOperator * @since 1.8 */ @FunctionalInterface @@ -41,5 +44,5 @@ public interface LongBinaryOperator { * @param right the right operand value * @return the result of the operation */ - public long operateAsLong(long left, long right); + public long applyAsLong(long left, long right); } diff --git a/src/share/classes/java/util/function/LongBlock.java b/src/share/classes/java/util/function/LongConsumer.java similarity index 75% rename from src/share/classes/java/util/function/LongBlock.java rename to src/share/classes/java/util/function/LongConsumer.java index 71606ecf9..072c0972a 100644 --- a/src/share/classes/java/util/function/LongBlock.java +++ b/src/share/classes/java/util/function/LongConsumer.java @@ -25,22 +25,21 @@ package java.util.function; /** - * An operation upon a {@code long} input value. The operation may modify - * external state. - * - *

This is the primitive type specialization of {@link Block} for - * {@code long} and also may be used as a {@code Block}. + * An operation which accepts a single long argument and returns no result. + * This is the {@code long}-consuming primitive type specialization of + * {@link Consumer}. Unlike most other functional interfaces, {@code LongConsumer} + * is expected to operate via side-effects. * + * @see Consumer * @since 1.8 */ @FunctionalInterface -public interface LongBlock { +public interface LongConsumer { /** - * Use the {@code long} input value in an operation which may modify - * external state. + * Accept an input value. * - * @param t the input value + * @param value the input value */ - public void accept(long t); + public void accept(long value); } diff --git a/src/share/classes/java/util/function/LongFunction.java b/src/share/classes/java/util/function/LongFunction.java index 61c4fbf64..924eb43be 100644 --- a/src/share/classes/java/util/function/LongFunction.java +++ b/src/share/classes/java/util/function/LongFunction.java @@ -25,22 +25,23 @@ package java.util.function; /** - * Apply a function to the input object yielding an appropriate {@code long} - * value; this is the {@code long}-bearing specialization for {@link Function}. + * Apply a function to the long-valued input argument, yielding an appropriate + * result. This is the {@code long}-consuming primitive specialization for + * {@link Function}. * - * @param the type of input objects to the function + * @param the type of output objects from the function * + * @see Function * @since 1.8 */ @FunctionalInterface -public interface LongFunction { +public interface LongFunction { /** - * Apply a function to the input object yielding an appropriate {@code long} - * value. + * Compute the result of applying the function to the input argument * - * @param t the input object - * @return the function result value + * @param value the input value + * @return the function result */ - public long applyAsLong(T t); + public R apply(long value); } diff --git a/src/share/classes/java/util/function/LongPredicate.java b/src/share/classes/java/util/function/LongPredicate.java new file mode 100644 index 000000000..ca542a412 --- /dev/null +++ b/src/share/classes/java/util/function/LongPredicate.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +import java.util.Objects; + +/** + * Determines if the {@code long} input value matches some criteria. This is the + * {@code long}-consuming primitive type specialization of {@link Predicate}. + * + * @see Predicate + * @since 1.8 + */ +@FunctionalInterface +public interface LongPredicate { + + /** + * Returns {@code true} if the input value matches some criteria. + * + * @param value the value to be tested. + * @return {@code true} if the input value matches some criteria, otherwise + * {@code false}. + */ + public boolean test(long value); + + /** + * Returns a predicate which evaluates to {@code true} only if this + * predicate and the provided predicate both evaluate to {@code true}. If + * this predicate returns {@code false} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ANDed with this predicate. + * @return a new predicate which returns {@code true} only if both + * predicates return {@code true}. + */ + public default LongPredicate and(LongPredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) && p.test(value); + } + + /** + * Returns a predicate which negates the result of this predicate. + * + * @return a new predicate who's result is always the opposite of this + * predicate. + */ + public default LongPredicate negate() { + return (value) -> !test(value); + } + + /** + * Returns a predicate which evaluates to {@code true} if either this + * predicate or the provided predicate evaluates to {@code true}. If this + * predicate returns {@code true} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ORed with this predicate. + * @return a new predicate which returns {@code true} if either predicate + * returns {@code true}. + */ + public default LongPredicate or(LongPredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) || p.test(value); + } + + /** + * Returns a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + * + * @param p a predicate which will be logically-XORed with this predicate. + * @return a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + */ + public default LongPredicate xor(LongPredicate p) { + Objects.requireNonNull(p); + return (value) -> test(value) ^ p.test(value); + } +} diff --git a/src/share/classes/java/util/function/LongSupplier.java b/src/share/classes/java/util/function/LongSupplier.java index f56ad7df2..2d5a3def1 100644 --- a/src/share/classes/java/util/function/LongSupplier.java +++ b/src/share/classes/java/util/function/LongSupplier.java @@ -25,11 +25,10 @@ package java.util.function; /** - * A supplier of {@code long} values. - * - *

This is the primitive type specialization of {@link Supplier} for - * {@code long} and also may be used as a {@code Supplier}. + * A supplier of {@code long} values. This is the {@code long}-providing + * primitive specialization of {@link Supplier}. * + * @see Supplier * @since 1.8 */ @FunctionalInterface @@ -38,7 +37,7 @@ public interface LongSupplier { /** * Returns a {@code long} value. * - * @return a {@code long} value. + * @return a {@code long} value */ public long getAsLong(); } diff --git a/src/share/classes/java/util/function/LongUnaryOperator.java b/src/share/classes/java/util/function/LongUnaryOperator.java index f27ddf5a8..3632e2a11 100644 --- a/src/share/classes/java/util/function/LongUnaryOperator.java +++ b/src/share/classes/java/util/function/LongUnaryOperator.java @@ -26,7 +26,10 @@ package java.util.function; /** * An operation on a single {@code long} operand yielding a {@code long} result. + * This is the primitive type specialization of {@link UnaryOperator} for + * {@code long}. * + * @see UnaryOperator * @since 1.8 */ @FunctionalInterface @@ -39,5 +42,5 @@ public interface LongUnaryOperator { * @param operand the operand value * @return the operation result value */ - public long operateAsLong(long operand); + public long applyAsLong(long operand); } diff --git a/src/share/classes/java/util/function/ObjDoubleConsumer.java b/src/share/classes/java/util/function/ObjDoubleConsumer.java new file mode 100644 index 000000000..24e4c2932 --- /dev/null +++ b/src/share/classes/java/util/function/ObjDoubleConsumer.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * An operation which accepts an object reference and a double, and returns no + * result. This is the {@code (reference, double)} specialization of + * {@link BiConsumer}. Unlike most other functional interfaces, + * {@code ObjDoubleConsumer} is expected to operate via side-effects. + * + * @param Type of reference argument to {@code accept()}. + * + * @see BiConsumer + * @since 1.8 + */ +@FunctionalInterface +public interface ObjDoubleConsumer { + + /** + * Accept a set of input values. + * + * @param t an input object + * @param value an input value + */ + public void accept(T t, double value); +} diff --git a/src/share/classes/java/util/function/ObjIntConsumer.java b/src/share/classes/java/util/function/ObjIntConsumer.java new file mode 100644 index 000000000..fa63af5cf --- /dev/null +++ b/src/share/classes/java/util/function/ObjIntConsumer.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2012, 2013 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * An operation which accepts an object reference and an int, and returns no + * result. This is the {@code (reference, int)} specialization of + * {@link BiConsumer}. Unlike most other functional interfaces, + * {@code ObjIntConsumer} is expected to operate via side-effects. + * + * @param Type of reference argument to {@code accept()}. + * + * @see BiConsumer + * @since 1.8 + */ +@FunctionalInterface +public interface ObjIntConsumer { + + /** + * Accept a set of input values. + * + * @param t an input object + * @param value an input value + */ + public void accept(T t, int value); +} diff --git a/src/share/classes/java/util/function/ObjLongConsumer.java b/src/share/classes/java/util/function/ObjLongConsumer.java new file mode 100644 index 000000000..c2f19fba7 --- /dev/null +++ b/src/share/classes/java/util/function/ObjLongConsumer.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * An operation which accepts an object reference and a long, and returns no + * result. This is the {@code (reference, long)} specialization of + * {@link BiConsumer}. Unlike most other functional interfaces, + * {@code ObjLongConsumer} is expected to operate via side-effects. + * + * @param Type of reference argument to {@code accept()}. + * + * @see BiConsumer + * @since 1.8 + */ +@FunctionalInterface +public interface ObjLongConsumer { + + /** + * Accept a set of input values. + * + * @param t an input object + * @param value an input value + */ + public void accept(T t, long value); +} diff --git a/src/share/classes/java/util/function/Predicate.java b/src/share/classes/java/util/function/Predicate.java index 9b4407874..627771e26 100644 --- a/src/share/classes/java/util/function/Predicate.java +++ b/src/share/classes/java/util/function/Predicate.java @@ -24,10 +24,12 @@ */ package java.util.function; +import java.util.Objects; + /** * Determines if the input object matches some criteria. * - * @param the type of input objects to {@code test} + * @param the type of argument to {@code test} * * @since 1.8 */ @@ -42,4 +44,57 @@ public interface Predicate { * {@code false} */ public boolean test(T t); + + /** + * Returns a predicate which evaluates to {@code true} only if this + * predicate and the provided predicate both evaluate to {@code true}. If + * this predicate returns {@code false} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ANDed with this predicate. + * @return a new predicate which returns {@code true} only if both + * predicates return {@code true}. + */ + public default Predicate and(Predicate p) { + Objects.requireNonNull(p); + return (t) -> test(t) && p.test(t); + } + + /** + * Returns a predicate which negates the result of this predicate. + * + * @return a new predicate who's result is always the opposite of this + * predicate. + */ + public default Predicate negate() { + return (t) -> !test(t); + } + + /** + * Returns a predicate which evaluates to {@code true} if either this + * predicate or the provided predicate evaluates to {@code true}. If this + * predicate returns {@code true} then the remaining predicate is not + * evaluated. + * + * @param p a predicate which will be logically-ORed with this predicate. + * @return a new predicate which returns {@code true} if either predicate + * returns {@code true}. + */ + public default Predicate or(Predicate p) { + Objects.requireNonNull(p); + return (t) -> test(t) || p.test(t); + } + + /** + * Returns a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + * + * @param p a predicate which will be logically-XORed with this predicte. + * @return a predicate that evaluates to {@code true} if both or neither of + * the component predicates evaluate to {@code true}. + */ + public default Predicate xor(Predicate p) { + Objects.requireNonNull(p); + return (t) -> test(t) ^ p.test(t); + } } diff --git a/src/share/classes/java/util/function/ToDoubleBiFunction.java b/src/share/classes/java/util/function/ToDoubleBiFunction.java new file mode 100644 index 000000000..df299aa58 --- /dev/null +++ b/src/share/classes/java/util/function/ToDoubleBiFunction.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2012, 2013 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input arguments, yielding an appropriate result. + * This is the {@code double}-bearing specialization for {@link BiFunction}. + * + * @param the type of the first argument to the {@code applyAsDouble} + * operation. + * @param the type of the second argument to the {@code applyAsDouble} + * operation. + * + * @see BiFunction. + * @since 1.8 + */ +@FunctionalInterface +public interface ToDoubleBiFunction { + + /** + * Compute the result of applying the function to the input arguments + * + * @param t an input object + * @param u an input object + * @return the function result value + */ + public double applyAsDouble(T t, U u); +} diff --git a/src/share/classes/java/util/function/ToDoubleFunction.java b/src/share/classes/java/util/function/ToDoubleFunction.java new file mode 100644 index 000000000..87f19fce4 --- /dev/null +++ b/src/share/classes/java/util/function/ToDoubleFunction.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input argument, yielding an appropriate result. + * This is the {@code double}-bearing specialization for {@link Function}. + * + * @param the type of input objects to the function + * + * @see Function + * @since 1.8 + */ +@FunctionalInterface +public interface ToDoubleFunction { + + /** + * Compute the result of applying the function to the input argument + * + * @param t the input object + * @return the function result value + */ + public double applyAsDouble(T t); +} diff --git a/src/share/classes/java/util/function/ToIntBiFunction.java b/src/share/classes/java/util/function/ToIntBiFunction.java new file mode 100644 index 000000000..d7c727fa3 --- /dev/null +++ b/src/share/classes/java/util/function/ToIntBiFunction.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input arguments, yielding an appropriate result. + * This is the {@code int}-bearing specialization for {@link BiFunction}. + * + * @param the type of the first argument to the {@code applyAsLong} + * operation. + * @param the type of the second argument to the {@code applyAsLong} + * operation. + * + * @see BiFunction + * @since 1.8 + */ +@FunctionalInterface +public interface ToIntBiFunction { + + /** + * Compute the result of applying the function to the input arguments + * + * @param t an input object + * @param u an input object + * @return the function result value + */ + public int applyAsInt(T t, U u); +} diff --git a/src/share/classes/java/util/function/ToIntFunction.java b/src/share/classes/java/util/function/ToIntFunction.java new file mode 100644 index 000000000..848a7b448 --- /dev/null +++ b/src/share/classes/java/util/function/ToIntFunction.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input argument, yielding an appropriate result. + * This is the {@code int}-bearing specialization for {@link Function}. + * + * @param the type of input objects to the function + * + * @see Function + * @since 1.8 + */ +@FunctionalInterface +public interface ToIntFunction { + + /** + * Compute the result of applying the function to the input arguments + * + * @param t the input object + * @return the function result value + */ + public int applyAsInt(T t); +} diff --git a/src/share/classes/java/util/function/ToLongBiFunction.java b/src/share/classes/java/util/function/ToLongBiFunction.java new file mode 100644 index 000000000..994fa49a5 --- /dev/null +++ b/src/share/classes/java/util/function/ToLongBiFunction.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input arguments, yielding an appropriate result. + * This is the {@code long}-bearing specialization for {@link BiFunction}. + * + * @param the type of the first argument to the {@code applyAsLong} + * operation. + * @param the type of the second argument to the {@code applyAsLong} + * operation. + * + * @see BiFunction + * @since 1.8 + */ +@FunctionalInterface +public interface ToLongBiFunction { + + /** + * Compute the result of applying the function to the input arguments. + * + * @param t an input object + * @param u an input object + * @return the function result value + */ + public long applyAsLong(T t, U u); +} diff --git a/src/share/classes/java/util/function/ToLongFunction.java b/src/share/classes/java/util/function/ToLongFunction.java new file mode 100644 index 000000000..6d5877b68 --- /dev/null +++ b/src/share/classes/java/util/function/ToLongFunction.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.function; + +/** + * Apply a function to the input argument, yielding an appropriate result. + * This is the {@code long}-bearing specialization for {@link Function}. + * + * @param the type of input objects to the function + * + * @see Function + * @since 1.8 + */ +@FunctionalInterface +public interface ToLongFunction { + + /** + * Compute the result of applying the function to the input arguments. + * + * @param t the input object + * @return the function result value + */ + public long applyAsLong(T t); +} diff --git a/src/share/classes/java/util/function/UnaryOperator.java b/src/share/classes/java/util/function/UnaryOperator.java index a8aaa0dcd..8e9229188 100644 --- a/src/share/classes/java/util/function/UnaryOperator.java +++ b/src/share/classes/java/util/function/UnaryOperator.java @@ -26,20 +26,14 @@ package java.util.function; /** * An operation upon a single operand yielding a result. The operand and the - * result are of the same type. + * result are of the same type. This is a specialization of {@code Function} for + * the case where the operand and result are of the same type. * - * @param the type of operand to {@code operate} and of the result + * @param the type of operand to {@code apply} and of the result * + * @see Function * @since 1.8 */ @FunctionalInterface -public interface UnaryOperator { - - /** - * Returns the result of the operation upon the operand. - * - * @param operand the operand - * @return the operation result - */ - public T operate(T operand); +public interface UnaryOperator extends Function { } diff --git a/src/share/classes/java/util/function/package-info.java b/src/share/classes/java/util/function/package-info.java index 0d1eecf51..36eda330d 100644 --- a/src/share/classes/java/util/function/package-info.java +++ b/src/share/classes/java/util/function/package-info.java @@ -22,27 +22,62 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + /** - * Functional interfaces provide typing for lambda expressions. Each - * functional interface provides a single abstract method to which the lambda - * expression's parameter and return types are matched. + * Functional interfaces provide target types for lambda expressions + * and method references. Each functional interface has a single abstract method + * to which the lambda expression's parameter and return types are matched or + * adapted. Functional interfaces can provide a target type in multiple contexts, + * such as assignment context, method invocation, or cast context: * - *

The interfaces in this package are all functional interfaces used with the - * collections and streams frameworks. The operation identified by each - * interface is generally applied to a collection or stream of objects. + *

+ *     Predicate<String> p = String::isEmpty;
+ *
+ *     stream.filter(e -> e.getSize() > 10)...
+ *
+ *     stream.map((ToIntFunction) e -> e.getSize())...
+ * 
+ * + *

The interfaces in this package are functional interfaces used by the JDK, + * and are available to be used by user code as well. While they do not identify + * a complete set of function shapes to which lambda expressions might be adapted, + * they provide enough to cover common requirements. + * + *

The interfaces in this package are annotated with @{link FunctionalInterface}. + * This annotation is not a requirement for the compiler to recognize an interface + * as a functional interface, but merely an aid to capture design intent and enlist the + * help of the compiler in identifying accidental violations of design intent. + * + *

The functional interfaces in this package follow an extensible naming convention, + * as follows: * - *

All functional interface implementations are expected to ensure that: *

    - *
  • When used for aggregate operations upon many elements it should not be - * assumed that the operation will be called upon elements in any specific order. - *
  • - *
  • {@code null} values are accepted and returned by these functional - * interfaces according to the constraints of the specification in which the - * functional interfaces are used. The functional interfaces themselves do not - * constrain or mandate use of {@code null} values. Most usages of the - * functional interfaces will define the role, if any, of {@code null} for that - * context. - *
  • + *
  • There are several basic function shapes, including {@link java.util.function.Function} ({@code T -> R}), + * {@link java.util.function.Consumer} ({@code T -> void}), + * {@link java.util.function.Predicate} ({@code T -> boolean}), + * and {@link java.util.function.Supplier} ({@code () -> T}). + *
  • + *
  • Function shapes have a natural arity based on how they are most commonly used. + * The basic shapes can be modified by an arity prefix to indicate a different arity, + * such as {@link java.util.function.BiFunction} ({@code (T, U) -> R}). + *
  • + *
  • There are additional derived function shapes which extend the basic function + * shapes, including {@link java.util.function.UnaryOperator} (extends {@code Function}) and + * {@link java.util.function.BinaryOperator} (extends {@code BiFunction}). + *
  • + *
  • Type parameters of functional interfaces can be specialized to primitives with + * additional type prefixes. To specialize the return type for a type that has both + * generic return type and generic arguments, we prefix {@code ToXxx}, as in + * {@link java.util.function.ToIntFunction}. Otherwise, type arguments are specialized left-to-right, + * as in {@link java.util.function.DoubleConsumer} or {@link java.util.function.ObjIntConsumer}. + * (The type prefix {@code Obj} is used to indicate that we don't want to specialize this parameter, + * but want to move on to the next parameter.) These schemes can be combined as in {@code IntToDoubleFunction}. + *
  • + *
  • If there are specialization prefixes for all arguments, the arity prefix may be left + * out (as in {@link java.util.function.ObjIntConsumer}). + *
  • *
+ * + * @see java.lang.FunctionalInterface */ package java.util.function; diff --git a/test/java/lang/PrimitiveSumMinMaxTest.java b/test/java/lang/PrimitiveSumMinMaxTest.java index 1c44109c8..56ab6740a 100644 --- a/test/java/lang/PrimitiveSumMinMaxTest.java +++ b/test/java/lang/PrimitiveSumMinMaxTest.java @@ -50,20 +50,20 @@ public class PrimitiveSumMinMaxTest { BinaryOperator xor = Boolean::logicalXor; Comparator cmp = Boolean::compare; - assertTrue(and.operate(true, true)); - assertFalse(and.operate(true, false)); - assertFalse(and.operate(false, true)); - assertFalse(and.operate(false, false)); + assertTrue(and.apply(true, true)); + assertFalse(and.apply(true, false)); + assertFalse(and.apply(false, true)); + assertFalse(and.apply(false, false)); - assertTrue(or.operate(true, true)); - assertTrue(or.operate(true, false)); - assertTrue(or.operate(false, true)); - assertFalse(or.operate(false, false)); + assertTrue(or.apply(true, true)); + assertTrue(or.apply(true, false)); + assertTrue(or.apply(false, true)); + assertFalse(or.apply(false, false)); - assertFalse(xor.operate(true, true)); - assertTrue(xor.operate(true, false)); - assertTrue(xor.operate(false, true)); - assertFalse(xor.operate(false, false)); + assertFalse(xor.apply(true, true)); + assertTrue(xor.apply(true, false)); + assertTrue(xor.apply(false, true)); + assertFalse(xor.apply(false, false)); assertEquals(Boolean.TRUE.compareTo(Boolean.TRUE), cmp.compare(true, true)); assertEquals(Boolean.TRUE.compareTo(Boolean.FALSE), cmp.compare(true, false)); @@ -83,12 +83,12 @@ public class PrimitiveSumMinMaxTest { int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE }; for (int i : numbers) { for (int j : numbers) { - assertEquals(i+j, (int) sum1.operate(i, j)); - assertEquals(i+j, sum2.operateAsInt(i, j)); - assertEquals(Math.max(i,j), (int) max1.operate(i, j)); - assertEquals(Math.max(i,j), max2.operateAsInt(i, j)); - assertEquals(Math.min(i,j), (int) min1.operate(i, j)); - assertEquals(Math.min(i,j), min2.operateAsInt(i, j)); + assertEquals(i+j, (int) sum1.apply(i, j)); + assertEquals(i+j, sum2.applyAsInt(i, j)); + assertEquals(Math.max(i,j), (int) max1.apply(i, j)); + assertEquals(Math.max(i,j), max2.applyAsInt(i, j)); + assertEquals(Math.min(i,j), (int) min1.apply(i, j)); + assertEquals(Math.min(i,j), min2.applyAsInt(i, j)); assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j)); } } @@ -106,12 +106,12 @@ public class PrimitiveSumMinMaxTest { long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE }; for (long i : numbers) { for (long j : numbers) { - assertEquals(i+j, (long) sum1.operate(i, j)); - assertEquals(i+j, sum2.operateAsLong(i, j)); - assertEquals(Math.max(i,j), (long) max1.operate(i, j)); - assertEquals(Math.max(i,j), max2.operateAsLong(i, j)); - assertEquals(Math.min(i,j), (long) min1.operate(i, j)); - assertEquals(Math.min(i,j), min2.operateAsLong(i, j)); + assertEquals(i+j, (long) sum1.apply(i, j)); + assertEquals(i+j, sum2.applyAsLong(i, j)); + assertEquals(Math.max(i,j), (long) max1.apply(i, j)); + assertEquals(Math.max(i,j), max2.applyAsLong(i, j)); + assertEquals(Math.min(i,j), (long) min1.apply(i, j)); + assertEquals(Math.min(i,j), min2.applyAsLong(i, j)); assertEquals(((Long) i).compareTo(j), cmp.compare(i, j)); } } @@ -126,9 +126,9 @@ public class PrimitiveSumMinMaxTest { float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE }; for (float i : numbers) { for (float j : numbers) { - assertEquals(i+j, (float) sum1.operate(i, j)); - assertEquals(Math.max(i,j), (float) max1.operate(i, j)); - assertEquals(Math.min(i,j), (float) min1.operate(i, j)); + assertEquals(i+j, (float) sum1.apply(i, j)); + assertEquals(Math.max(i,j), (float) max1.apply(i, j)); + assertEquals(Math.min(i,j), (float) min1.apply(i, j)); assertEquals(((Float) i).compareTo(j), cmp.compare(i, j)); } } @@ -146,12 +146,12 @@ public class PrimitiveSumMinMaxTest { double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE }; for (double i : numbers) { for (double j : numbers) { - assertEquals(i+j, (double) sum1.operate(i, j)); - assertEquals(i+j, sum2.operateAsDouble(i, j)); - assertEquals(Math.max(i,j), (double) max1.operate(i, j)); - assertEquals(Math.max(i,j), max2.operateAsDouble(i, j)); - assertEquals(Math.min(i,j), (double) min1.operate(i, j)); - assertEquals(Math.min(i,j), min2.operateAsDouble(i, j)); + assertEquals(i+j, (double) sum1.apply(i, j)); + assertEquals(i+j, sum2.applyAsDouble(i, j)); + assertEquals(Math.max(i,j), (double) max1.apply(i, j)); + assertEquals(Math.max(i,j), max2.applyAsDouble(i, j)); + assertEquals(Math.min(i,j), (double) min1.apply(i, j)); + assertEquals(Math.min(i,j), min2.applyAsDouble(i, j)); assertEquals(((Double) i).compareTo(j), cmp.compare(i, j)); } } -- GitLab