eu.lunisolar.magma.func.function.LFunction Maven / Gradle / Ivy
/*
* This file is part of "lunisolar-magma".
*
* (C) Copyright 2015 Lunisolar (http://lunisolar.eu/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.lunisolar.magma.func.function;
import javax.annotation.Nonnull; // NOSONAR
import javax.annotation.Nullable; // NOSONAR
import java.util.Comparator; // NOSONAR
import java.util.Objects; // NOSONAR
import eu.lunisolar.magma.basics.*; //NOSONAR
import eu.lunisolar.magma.basics.builder.*; // NOSONAR
import eu.lunisolar.magma.basics.exceptions.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.functional.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.functional.type.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.functional.domain.*; // NOSONAR
import eu.lunisolar.magma.func.*; // NOSONAR
import eu.lunisolar.magma.func.operator.unary.*; // NOSONAR
import eu.lunisolar.magma.func.operator.binary.*; // NOSONAR
import eu.lunisolar.magma.func.operator.ternary.*; // NOSONAR
import eu.lunisolar.magma.func.function.*; // NOSONAR
import eu.lunisolar.magma.func.function.from.*; // NOSONAR
import eu.lunisolar.magma.func.function.to.*; // NOSONAR
import eu.lunisolar.magma.func.function.conversion.*; // NOSONAR
import eu.lunisolar.magma.func.predicate.*; // NOSONAR
import eu.lunisolar.magma.func.supplier.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.bi.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.tri.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.obj.*; // NOSONAR
import eu.lunisolar.magma.func.action.*; // NOSONAR
/**
* Non-throwing functional interface (lambda) LFunction for Java 8.
*
* Type: function
*
* Domain (lvl: 1): T t
*
* Co-domain: R
*
* @see LFunctionX
*/
@FunctionalInterface
@SuppressWarnings("UnusedDeclaration")
public interface LFunction extends LFunctionX, MetaFunction, MetaInterface.NonThrowing { // NOSONAR
static final String DESCRIPTION = "LFunction: R doApply(T t)";
/**
* Default implementation for JRE method that calls exception nesting method.
* @deprecated Calling this method via LFunction interface should be discouraged.
*/
@Override
@Deprecated
default R apply(T t) {
return this.nestingDoApply(t);
}
@Nullable
R doApply(T t);
/** Function call that handles exceptions by always nesting checked exceptions and propagating the otheres as is. */
default R nestingDoApply(T t) {
return this.doApply(t);
}
/** Function call that handles exceptions by always propagating them as is even when they are undeclared checked ones. */
default R shovingDoApply(T t) {
return this.doApply(t);
}
static final LSupplier NULL_VALUE_MESSAGE_SUPPLIER = () -> "Evaluated value by nonNullDoApply() method cannot be null (" + DESCRIPTION + ").";
/** Function call that ensures the result is not null */
@Nonnull
default R nonNullDoApply(T t) {
return Null.requireNonNull(doApply(t), NULL_VALUE_MESSAGE_SUPPLIER);
}
/** Returns description of the functional interface. */
@Nonnull
default String functionalInterfaceDescription() {
return LFunction.DESCRIPTION;
}
/** Captures arguments but delays the evaluation. */
default LSupplier captureFunc(T t) {
return () -> this.doApply(t);
}
/** Creates function that always returns the same value. */
static LFunction constant(R r) {
return t -> r;
}
/** Convenient method in case lambda expression is ambiguous for the compiler (that might happen for overloaded methods accepting different interfaces). */
@Nonnull
static LFunction l(final @Nonnull LFunction lambda) {
Null.nonNullArg(lambda, "lambda");
return lambda;
}
//
/** Wraps JRE instance. */
@Nonnull
static LFunction wrap(final java.util.function.Function other) {
return other::apply;
}
/** Wraps opposite (throwing vs non-throwing) instance. */
@Nonnull
static LFunction wrap(final @Nonnull LFunctionX other) {
return other::nestingDoApply;
}
//
//
/** Allows to manipulate the domain of the function. */
@Nonnull
default LFunction funcCompose(@Nonnull final LFunction super V1, ? extends T> before1) {
Null.nonNullArg(before1, "before1");
return v1 -> this.doApply(before1.doApply(v1));
}
//
//
/** Combines two functions together in a order. */
@Nonnull
default LFunction then(@Nonnull LFunction super R, ? extends V> after) {
Null.nonNullArg(after, "after");
return t -> after.doApply(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LConsumer then(@Nonnull LConsumer super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doAccept(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToByteFunction thenToByte(@Nonnull LToByteFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsByte(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToShortFunction thenToShort(@Nonnull LToShortFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsShort(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToIntFunction thenToInt(@Nonnull LToIntFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsInt(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToLongFunction thenToLong(@Nonnull LToLongFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsLong(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToFloatFunction thenToFloat(@Nonnull LToFloatFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsFloat(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToDoubleFunction thenToDouble(@Nonnull LToDoubleFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsDouble(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LToCharFunction thenToChar(@Nonnull LToCharFunction super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doApplyAsChar(this.doApply(t));
}
/** Combines two functions together in a order. */
@Nonnull
default LPredicate thenToBoolean(@Nonnull LPredicate super R> after) {
Null.nonNullArg(after, "after");
return t -> after.doTest(this.doApply(t));
}
//
/** Returns a function that always returns its input argument. */
@Nonnull
static LFunction identity() {
return t -> t;
}
//
/** Converts to non-throwing variant (if required). */
@Nonnull
default LFunction nestingFunc() {
return this;
}
/** Converts to throwing variant (RuntimeException). */
@Nonnull
default LFunctionX nestingFuncX() {
return this;
}
/** Converts to non-throwing variant that will propagate checked exception as it would be unchecked - there is no exception wrapping involved (at least not here). */
default LFunction shovingFunc() {
return this;
}
/** Converts to throwing variant (RuntimeException) that will propagate checked exception as it would be unchecked - there is no exception wrapping involved (at least not here). */
default LFunctionX shovingFuncX() {
return this;
}
//
/** Converts to function that makes sure that the result is not null. */
@Nonnull
default LFunction nonNullFunc() {
return this::nonNullDoApply;
}
}