From ea2fc5a632a28cf9dc55881edf3ddeb0524820f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Fri, 11 Sep 2015 15:01:54 +0200 Subject: [PATCH] Float functional interfaces These interfaces correspond to the JRE-provided primitive specialisations of functional interfaces for the float type. --- .../ebul/func/DoubleToFloatFunction.java | 27 ++++++ .../ebul/func/FloatBinaryOperator.java | 34 +++++++ .../info/ebenoit/ebul/func/FloatConsumer.java | 51 +++++++++++ .../info/ebenoit/ebul/func/FloatFunction.java | 30 +++++++ .../ebenoit/ebul/func/FloatPredicate.java | 88 +++++++++++++++++++ .../info/ebenoit/ebul/func/FloatSupplier.java | 28 ++++++ .../ebul/func/FloatToDoubleFunction.java | 27 ++++++ .../ebenoit/ebul/func/FloatToIntFunction.java | 27 ++++++ .../ebul/func/FloatToLongFunction.java | 27 ++++++ .../ebenoit/ebul/func/FloatUnaryOperator.java | 82 +++++++++++++++++ .../ebenoit/ebul/func/IntToFloatFunction.java | 27 ++++++ .../ebul/func/LongToFloatFunction.java | 27 ++++++ .../ebenoit/ebul/func/ObjFloatConsumer.java | 31 +++++++ .../ebenoit/ebul/func/ToFloatBiFunction.java | 35 ++++++++ .../ebenoit/ebul/func/ToFloatFunction.java | 29 ++++++ 15 files changed, 570 insertions(+) create mode 100644 src/main/java/info/ebenoit/ebul/func/DoubleToFloatFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatBinaryOperator.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatConsumer.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatPredicate.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatSupplier.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatToDoubleFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatToIntFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatToLongFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/FloatUnaryOperator.java create mode 100644 src/main/java/info/ebenoit/ebul/func/IntToFloatFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/LongToFloatFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/ObjFloatConsumer.java create mode 100644 src/main/java/info/ebenoit/ebul/func/ToFloatBiFunction.java create mode 100644 src/main/java/info/ebenoit/ebul/func/ToFloatFunction.java diff --git a/src/main/java/info/ebenoit/ebul/func/DoubleToFloatFunction.java b/src/main/java/info/ebenoit/ebul/func/DoubleToFloatFunction.java new file mode 100644 index 0000000..ba0fbf5 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/DoubleToFloatFunction.java @@ -0,0 +1,27 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code double}-valued argument and produces a {@code float}-valued result. This + * is the {@code double}-to-{@code float} primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(double)}. + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface DoubleToFloatFunction +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public float applyAsFloat( double value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatBinaryOperator.java b/src/main/java/info/ebenoit/ebul/func/FloatBinaryOperator.java new file mode 100644 index 0000000..227edab --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatBinaryOperator.java @@ -0,0 +1,34 @@ +package info.ebenoit.ebul.func; + + +import java.util.function.BinaryOperator; + + + +/** + * Represents an operation upon two float-valued operands and producing a float-valued result. + * This is the primitive type specialisation of {@link BinaryOperator} for float. + *

+ * This is a {@link java.util.function functional interface} whose functional method is + * {@link #applyAsFloat(float, float)}. + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatBinaryOperator +{ + + /** + * Applies this operator to the given operands. + * + * @param t + * the first operand + * @param u + * the second operand + * @return the result + * @throws FunctionException + * if a checked exception occurs + */ + public float applyAsFloat( float t , float u ); + +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatConsumer.java b/src/main/java/info/ebenoit/ebul/func/FloatConsumer.java new file mode 100644 index 0000000..2cb3a63 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatConsumer.java @@ -0,0 +1,51 @@ +package info.ebenoit.ebul.func; + + +import java.util.Objects; + + + +/** + * Represents an operation that accepts a single {@code float}-valued argument and returns no result. This is the + * primitive type specialisation of {@link java.util.function.Consumer} for {@code float}. Unlike most other functional + * interfaces, {@code FloatConsumer} is expected to operate via side-effects. + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #accept(float)}. + * + * @see java.util.function.Consumer + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatConsumer +{ + /** + * Performs this operation on the given argument. + * + * @param value + * the input argument + */ + public void accept( float t ); + + + /** + * Returns a composed {@code FloatConsumer} that performs, in sequence, this operation followed by the {@code after} + * operation. If performing either operation throws an exception, it is relayed to the caller of the composed + * operation. If performing this operation throws an exception, the {@code after} operation will not be performed. + * + * @param after + * the operation to perform after this operation + * @return a composed {@code FloatConsumer} that performs in sequence this operation followed by the {@code after} + * operation + * @throws NullPointerException + * if {@code after} is null + */ + default FloatConsumer andThen( final FloatConsumer after ) + { + Objects.requireNonNull( after ); + return ( final float t ) -> { + this.accept( t ); + after.accept( t ); + }; + } +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatFunction.java b/src/main/java/info/ebenoit/ebul/func/FloatFunction.java new file mode 100644 index 0000000..0264684 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatFunction.java @@ -0,0 +1,30 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code float}-valued argument and produces a result. This is the {@code float} + * -consuming primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #apply(float)}. + * + * @param + * the type of the result of the function + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatFunction< R > +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public R apply( float value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatPredicate.java b/src/main/java/info/ebenoit/ebul/func/FloatPredicate.java new file mode 100644 index 0000000..ef1fdcd --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatPredicate.java @@ -0,0 +1,88 @@ +package info.ebenoit.ebul.func; + + +import java.util.Objects; + + + +/** + * Represents a predicate (boolean-valued function) of one {@code float}-valued argument. This is the {@code float} + * -consuming primitive type specialisation of {@link java.util.function.Predicate}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #test(float)}. + * + * @see java.util.function.Predicate + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatPredicate +{ + + /** + * Evaluates this predicate on the given argument. + * + * @param value + * the input argument + * @return {@code true} if the input argument matches the predicate, otherwise {@code false} + */ + public boolean test( float value ); + + + /** + * Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. When + * evaluating the composed predicate, if this predicate is {@code false}, then the {@code other} predicate is not + * evaluated. + * + *

+ * Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of this + * predicate throws an exception, the {@code other} predicate will not be evaluated. + * + * @param other + * a predicate that will be logically-ANDed with this predicate + * @return a composed predicate that represents the short-circuiting logical AND of this predicate and the + * {@code other} predicate + * @throws NullPointerException + * if other is null + */ + default FloatPredicate and( final FloatPredicate other ) + { + Objects.requireNonNull( other ); + return ( value ) -> this.test( value ) && other.test( value ); + } + + + /** + * Returns a predicate that represents the logical negation of this predicate. + * + * @return a predicate that represents the logical negation of this predicate + */ + default FloatPredicate negate( ) + { + return ( value ) -> !this.test( value ); + } + + + /** + * Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. When + * evaluating the composed predicate, if this predicate is {@code true}, then the {@code other} predicate is not + * evaluated. + * + *

+ * Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of this + * predicate throws an exception, the {@code other} predicate will not be evaluated. + * + * @param other + * a predicate that will be logically-ORed with this predicate + * @return a composed predicate that represents the short-circuiting logical OR of this predicate and the + * {@code other} predicate + * @throws NullPointerException + * if other is null + */ + default FloatPredicate or( final FloatPredicate other ) + { + Objects.requireNonNull( other ); + return ( value ) -> this.test( value ) || other.test( value ); + } +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatSupplier.java b/src/main/java/info/ebenoit/ebul/func/FloatSupplier.java new file mode 100644 index 0000000..29a3bd8 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatSupplier.java @@ -0,0 +1,28 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a supplier of {@code float}-valued results. This is the {@code float}-producing primitive specialisation + * of {@link java.util.function.Supplier}. + * + *

+ * There is no requirement that a distinct result be returned each time the supplier is invoked. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #getAsFloat()}. + * + * @see java.util.function.Supplier + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatSupplier +{ + + /** + * Gets a result. + * + * @return a result + */ + public float getAsFloat( ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatToDoubleFunction.java b/src/main/java/info/ebenoit/ebul/func/FloatToDoubleFunction.java new file mode 100644 index 0000000..369dbed --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatToDoubleFunction.java @@ -0,0 +1,27 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code float}-valued argument and produces a (@code double}-valued result. This + * is the {@code float}-to-{@code double} primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsDouble(float)}. + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatToDoubleFunction +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public double applyAsDouble( float value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatToIntFunction.java b/src/main/java/info/ebenoit/ebul/func/FloatToIntFunction.java new file mode 100644 index 0000000..ec2df3d --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatToIntFunction.java @@ -0,0 +1,27 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code float}-valued argument and produces an {@code int}-valued result. This is + * the {@code float}-to-{@code int} primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsInt(float)}. + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatToIntFunction +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public int applyAsInt( float value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatToLongFunction.java b/src/main/java/info/ebenoit/ebul/func/FloatToLongFunction.java new file mode 100644 index 0000000..779c63f --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatToLongFunction.java @@ -0,0 +1,27 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code float}-valued argument and produces a (@code long}-valued result. This is + * the {@code float}-to-{@code long} primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsLong(float)}. + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatToLongFunction +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public long applyAsLong( float value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/FloatUnaryOperator.java b/src/main/java/info/ebenoit/ebul/func/FloatUnaryOperator.java new file mode 100644 index 0000000..64bbb3d --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/FloatUnaryOperator.java @@ -0,0 +1,82 @@ +package info.ebenoit.ebul.func; + + +import java.util.Objects; + + + +/** + * Represents an operation on a single {@code float}-valued operand that produces a {@code float}-valued result. This is + * the primitive type specialisation of {@link java.util.function.UnaryOperator} for {@code float}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(float)}. + * + * @see java.util.function.UnaryOperator + * + * @author E. Benoît + */ +@FunctionalInterface +public interface FloatUnaryOperator +{ + + /** + * Applies this operator to the given operand. + * + * @param operand + * the operand + * @return the operator result + */ + public float applyAsFloat( float operand ); + + + /** + * Returns a composed operator that first applies the {@code before} operator to its input, and then applies this + * operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the + * composed operator. + * + * @param before + * the operator to apply before this operator is applied + * @return a composed operator that first applies the {@code before} operator and then applies this operator + * @throws NullPointerException + * if before is null + * + * @see #andThen(FloatUnaryOperator) + */ + default FloatUnaryOperator compose( final FloatUnaryOperator before ) + { + Objects.requireNonNull( before ); + return ( final float v ) -> this.applyAsFloat( before.applyAsFloat( v ) ); + } + + + /** + * Returns a composed operator that first applies this operator to its input, and then applies the {@code after} + * operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the + * composed operator. + * + * @param after + * the operator to apply after this operator is applied + * @return a composed operator that first applies this operator and then applies the {@code after} operator + * @throws NullPointerException + * if after is null + * + * @see #compose(FloatUnaryOperator) + */ + default FloatUnaryOperator andThen( final FloatUnaryOperator after ) + { + Objects.requireNonNull( after ); + return ( final float t ) -> after.applyAsFloat( this.applyAsFloat( t ) ); + } + + + /** + * Returns a unary operator that always returns its input argument. + * + * @return a unary operator that always returns its input argument + */ + public static FloatUnaryOperator identity( ) + { + return t -> t; + } +} diff --git a/src/main/java/info/ebenoit/ebul/func/IntToFloatFunction.java b/src/main/java/info/ebenoit/ebul/func/IntToFloatFunction.java new file mode 100644 index 0000000..05baf13 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/IntToFloatFunction.java @@ -0,0 +1,27 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code int}-valued argument and produces a {@code float}-valued result. This is + * the {@code int}-to-{@code float} primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(int)}. + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface IntToFloatFunction +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public float applyAsFloat( int value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/LongToFloatFunction.java b/src/main/java/info/ebenoit/ebul/func/LongToFloatFunction.java new file mode 100644 index 0000000..97da5c6 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/LongToFloatFunction.java @@ -0,0 +1,27 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts a {@code long}-valued argument and produces a {@code float}-valued result. This is + * the {@code long}-to-{@code float} primitive specialisation for {@link java.util.function.Function}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(long)}. + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface LongToFloatFunction +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public float applyAsFloat( long value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/ObjFloatConsumer.java b/src/main/java/info/ebenoit/ebul/func/ObjFloatConsumer.java new file mode 100644 index 0000000..d76d2b3 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/ObjFloatConsumer.java @@ -0,0 +1,31 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents an operation that accepts an object-valued and a {@code float}-valued argument, and returns no result. + * This is the {@code (reference, float)} specialisation of {@link java.util.function.BiConsumer}. Unlike most other + * functional interfaces, {@code ObjDoubleConsumer} is expected to operate via side-effects. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is {@link #accept(Object, float)}. + * + * @param + * the type of the object argument to the operation + * + * @see BiConsumer + * @since 1.8 + */ +@FunctionalInterface +public interface ObjFloatConsumer< T > +{ + + /** + * Performs this operation on the given arguments. + * + * @param t + * the first input argument + * @param value + * the second input argument + */ + public void accept( T t , float value ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/ToFloatBiFunction.java b/src/main/java/info/ebenoit/ebul/func/ToFloatBiFunction.java new file mode 100644 index 0000000..5f41c0e --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/ToFloatBiFunction.java @@ -0,0 +1,35 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that accepts two arguments and produces a {@code float}-valued result. This is the + * {@code float}-producing primitive specialisation for {@link java.util.function.BiFunction}. + * + *

+ * This is a {@link java.util.function functional interface} whose functional method is + * {@link #applyAsFloat(Object, Object)}. + * + * @param + * the type of the first argument to the function + * @param + * the type of the second argument to the function + * + * @see java.util.function.BiFunction + * + * @author E. Benoît + */ +@FunctionalInterface +public interface ToFloatBiFunction< T , U > +{ + + /** + * Applies this function to the given arguments. + * + * @param t + * the first function argument + * @param u + * the second function argument + * @return the function result + */ + public float applyAsFloat( T t , U u ); +} diff --git a/src/main/java/info/ebenoit/ebul/func/ToFloatFunction.java b/src/main/java/info/ebenoit/ebul/func/ToFloatFunction.java new file mode 100644 index 0000000..da8414c --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/func/ToFloatFunction.java @@ -0,0 +1,29 @@ +package info.ebenoit.ebul.func; + + +/** + * Represents a function that produces a {@code float}-valued result. This is the {@code float}-producing primitive + * specialisation for {@link java.util.function.Function}. + * + * This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(Object)}. + * + * @param + * the type of the input to the function + * + * @see java.util.function.Function + * + * @author E. Benoît + */ +@FunctionalInterface +public interface ToFloatFunction< T > +{ + + /** + * Applies this function to the given argument. + * + * @param value + * the function argument + * @return the function result + */ + public float applyAsFloat( T value ); +}