Float functional interfaces

These interfaces correspond to the JRE-provided primitive
specialisations of functional interfaces for the float type.
This commit is contained in:
Emmanuel BENOîT 2015-09-11 15:01:54 +02:00
parent ec2c870f7b
commit ea2fc5a632
15 changed files with 570 additions and 0 deletions

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(double)}.
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -0,0 +1,34 @@
package info.ebenoit.ebul.func;
import java.util.function.BinaryOperator;
/**
* Represents an operation upon two <code>float</code>-valued operands and producing a <code>float</code>-valued result.
* This is the primitive type specialisation of {@link BinaryOperator} for <code>float</code>.
* <p>
* This is a {@link java.util.function functional interface} whose functional method is
* {@link #applyAsFloat(float, float)}.
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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.
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #accept(float)}.
*
* @see java.util.function.Consumer
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
};
}
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #apply(float)}.
*
* @param <R>
* the type of the result of the function
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #test(float)}.
*
* @see java.util.function.Predicate
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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.
*
* <p>
* 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.
*
* <p>
* 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 );
}
}

View file

@ -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}.
*
* <p>
* There is no requirement that a distinct result be returned each time the supplier is invoked.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #getAsFloat()}.
*
* @see java.util.function.Supplier
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@FunctionalInterface
public interface FloatSupplier
{
/**
* Gets a result.
*
* @return a result
*/
public float getAsFloat( );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsDouble(float)}.
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsInt(float)}.
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsLong(float)}.
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(float)}.
*
* @see java.util.function.UnaryOperator
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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;
}
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(int)}.
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #applyAsFloat(long)}.
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is {@link #accept(Object, float)}.
*
* @param <T>
* 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 );
}

View file

@ -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}.
*
* <p>
* This is a {@link java.util.function functional interface} whose functional method is
* {@link #applyAsFloat(Object, Object)}.
*
* @param <T>
* the type of the first argument to the function
* @param <U>
* the type of the second argument to the function
*
* @see java.util.function.BiFunction
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}

View file

@ -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 <T>
* the type of the input to the function
*
* @see java.util.function.Function
*
* @author <a href="mailto:ebenoit@ebenoit.info">E. Benoît</a>
*/
@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 );
}