All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.lunisolar.magma.func.function.LBiFunction Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is part of "lunisolar-magma".
 *
 * (C) Copyright 2014-2019 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.aType.*; // 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.IA;
import eu.lunisolar.magma.func.SA;
import eu.lunisolar.magma.func.*; // NOSONAR
import eu.lunisolar.magma.func.tuple.*; // NOSONAR
import java.util.function.*; // NOSONAR
import java.util.*; // NOSONAR
import java.lang.reflect.*;

import eu.lunisolar.magma.func.action.*; // 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.obj.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.tri.*; // NOSONAR
import eu.lunisolar.magma.func.function.*; // NOSONAR
import eu.lunisolar.magma.func.function.conversion.*; // NOSONAR
import eu.lunisolar.magma.func.function.from.*; // NOSONAR
import eu.lunisolar.magma.func.function.to.*; // NOSONAR
import eu.lunisolar.magma.func.operator.binary.*; // NOSONAR
import eu.lunisolar.magma.func.operator.ternary.*; // NOSONAR
import eu.lunisolar.magma.func.operator.unary.*; // NOSONAR
import eu.lunisolar.magma.func.predicate.*; // NOSONAR
import eu.lunisolar.magma.func.supplier.*; // NOSONAR

/**
 * Non-throwing functional interface (lambda) LBiFunction for Java 8.
 *
 * Type: function
 *
 * Domain (lvl: 2): T1 a1,T2 a2
 *
 * Co-domain: R
 *
 */
@FunctionalInterface
@SuppressWarnings("UnusedDeclaration")
public interface LBiFunction extends BiFunction, MetaFunction, MetaInterface.NonThrowing, Codomain>, Domain2, a> { // NOSONAR

	String DESCRIPTION = "LBiFunction: R apply(T1 a1,T2 a2)";

	@Nullable
	// R apply(T1 a1,T2 a2) ;
	default R apply(T1 a1, T2 a2) {
		// return nestingApply(a1,a2);
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			throw Handling.nestCheckedAndThrow(e);
		}
	}

	/**
	 * Implement this, but call apply(T1 a1,T2 a2)
	 */
	R applyX(T1 a1, T2 a2) throws Throwable;

	default R tupleApply(LPair args) {
		return apply(args.first(), args.second());
	}

	/** Function call that handles exceptions according to the instructions. */
	default R handlingApply(T1 a1, T2 a2, HandlingInstructions handling) {
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			throw Handler.handleOrNest(e, handling);
		}
	}

	default LBiFunction handling(HandlingInstructions handling) {
		return (a1, a2) -> handlingApply(a1, a2, handling);
	}

	default R apply(T1 a1, T2 a2, @Nonnull ExWMF exF, @Nonnull String newMessage, @Nullable Object... messageParams) {
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			throw Handling.wrap(e, exF, newMessage, messageParams);
		}
	}

	default LBiFunction trying(@Nonnull ExWMF exF, @Nonnull String newMessage, @Nullable Object... messageParams) {
		return (a1, a2) -> apply(a1, a2, exF, newMessage, messageParams);
	}

	default R apply(T1 a1, T2 a2, @Nonnull ExWF exF) {
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			throw Handling.wrap(e, exF);
		}
	}

	default LBiFunction trying(@Nonnull ExWF exF) {
		return (a1, a2) -> apply(a1, a2, exF);
	}

	default R applyThen(T1 a1, T2 a2, @Nonnull LFunction handler) {
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			Handling.handleErrors(e);
			return handler.apply(e);
		}
	}

	default LBiFunction tryingThen(@Nonnull LFunction handler) {
		return (a1, a2) -> applyThen(a1, a2, handler);
	}

	/** Function call that handles exceptions by always nesting checked exceptions and propagating the others as is. */
	default R nestingApply(T1 a1, T2 a2) {
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			throw Handling.nestCheckedAndThrow(e);
		}
	}

	/** Function call that handles exceptions by always propagating them as is, even when they are undeclared checked ones. */
	default R shovingApply(T1 a1, T2 a2) {
		try {
			return this.applyX(a1, a2);
		} catch (Throwable e) { // NOSONAR
			throw Handling.shoveIt(e);
		}
	}

	static  R handlingApply(T1 a1, T2 a2, LBiFunction func, HandlingInstructions handling) { // <-
		Null.nonNullArg(func, "func");
		return func.handlingApply(a1, a2, handling);
	}

	static  R tryApply(T1 a1, T2 a2, LBiFunction func) {
		Null.nonNullArg(func, "func");
		return func.nestingApply(a1, a2);
	}

	static  R tryApply(T1 a1, T2 a2, LBiFunction func, @Nonnull ExWMF exF, @Nonnull String newMessage, @Nullable Object... messageParams) {
		Null.nonNullArg(func, "func");
		return func.apply(a1, a2, exF, newMessage, messageParams);
	}

	static  R tryApply(T1 a1, T2 a2, LBiFunction func, @Nonnull ExWF exF) {
		Null.nonNullArg(func, "func");
		return func.apply(a1, a2, exF);
	}

	static  R tryApplyThen(T1 a1, T2 a2, LBiFunction func, @Nonnull LFunction handler) {
		Null.nonNullArg(func, "func");
		return func.applyThen(a1, a2, handler);
	}

	default R failSafeApply(T1 a1, T2 a2, @Nonnull LBiFunction failSafe) {
		try {
			return apply(a1, a2);
		} catch (Throwable e) { // NOSONAR
			Handling.handleErrors(e);
			return failSafe.apply(a1, a2);
		}
	}

	static  R failSafeApply(T1 a1, T2 a2, LBiFunction func, @Nonnull LBiFunction failSafe) {
		Null.nonNullArg(failSafe, "failSafe");
		if (func == null) {
			return failSafe.apply(a1, a2);
		} else {
			return func.failSafeApply(a1, a2, failSafe);
		}
	}

	static  LBiFunction failSafe(LBiFunction func, @Nonnull LBiFunction failSafe) {
		Null.nonNullArg(failSafe, "failSafe");
		return (a1, a2) -> failSafeApply(a1, a2, func, failSafe);
	}

	LSupplier NULL_VALUE_MESSAGE_SUPPLIER = () -> "Evaluated value by nonNullApply() method cannot be null (" + DESCRIPTION + ").";

	/** Function call that ensures the result is not null */
	@Nonnull
	default R nonNullApply(T1 a1, T2 a2) {
		return Null.requireNonNull(apply(a1, a2), NULL_VALUE_MESSAGE_SUPPLIER);
	}

	/** Returns description of the functional interface. */
	@Nonnull
	default String functionalInterfaceDescription() {
		return LBiFunction.DESCRIPTION;
	}

	/** From-To. Intended to be used with non-capturing lambda. */
	public static  void fromTo(int min_i, int max_i, T1 a1, T2 a2, LBiFunction func) {
		Null.nonNullArg(func, "func");
		if (min_i <= max_i) {
			for (int i = min_i; i <= max_i; i++) {
				func.apply(a1, a2);
			}
		} else {
			for (int i = min_i; i >= max_i; i--) {
				func.apply(a1, a2);
			}
		}
	}

	/** From-To. Intended to be used with non-capturing lambda. */
	public static  void fromTill(int min_i, int max_i, T1 a1, T2 a2, LBiFunction func) {
		Null.nonNullArg(func, "func");
		if (min_i <= max_i) {
			for (int i = min_i; i < max_i; i++) {
				func.apply(a1, a2);
			}
		} else {
			for (int i = min_i; i > max_i; i--) {
				func.apply(a1, a2);
			}
		}
	}

	/** From-To. Intended to be used with non-capturing lambda. */
	public static  void times(int max_i, T1 a1, T2 a2, LBiFunction func) {
		if (max_i < 0)
			return;
		fromTill(0, max_i, a1, a2, func);
	}

	public default LFunction lShrink(LFunction left) {
		return a2 -> apply(left.apply(a2), a2);
	}

	public default LFunction lShrinkc(T1 a1) {
		return a2 -> apply(a1, a2);
	}

	public static  LFunction lShrinked(LFunction left, LBiFunction func) {
		return func.lShrink(left);
	}

	public static  LFunction lShrinkedc(T1 a1, LBiFunction func) {
		return func.lShrinkc(a1);
	}

	public default LFunction rShrink(LFunction right) {
		return a1 -> apply(a1, right.apply(a1));
	}

	public default LFunction rShrinkc(T2 a2) {
		return a1 -> apply(a1, a2);
	}

	public static  LFunction rShrinked(LFunction right, LBiFunction func) {
		return func.rShrink(right);
	}

	public static  LFunction rShrinkedc(T2 a2, LBiFunction func) {
		return func.rShrinkc(a2);
	}

	/**  */
	public static  LBiFunction uncurry(LFunction> func) {
		return (T1 a1, T2 a2) -> func.apply(a1).apply(a2);
	}

	/** Cast that removes generics. */
	public default LBiFunction untyped() {
		return this;
	}

	/** Cast that replace generics. */
	public default  LBiFunction cast() {
		return untyped();
	}

	/** Cast that replace generics. */
	public static  LBiFunction cast(LBiFunction function) {
		return (LBiFunction) function;
	}

	/** Captures arguments but delays the evaluation. */
	default LSupplier capture(T1 a1, T2 a2) {
		return () -> this.apply(a1, a2);
	}

	/** Creates function that always returns the same value. */
	static  LBiFunction constant(R r) {
		return (a1, a2) -> r;
	}

	/** Captures single parameter function into this interface where only 1st parameter will be used. */
	@Nonnull
	static  LBiFunction apply1st(@Nonnull LFunction func) {
		return (a1, a2) -> func.apply(a1);
	}

	/** Captures single parameter function into this interface where only 2nd parameter will be used. */
	@Nonnull
	static  LBiFunction apply2nd(@Nonnull LFunction func) {
		return (a1, a2) -> func.apply(a2);
	}

	/** Convenient method in case lambda expression is ambiguous for the compiler (that might happen for overloaded methods accepting different interfaces). */
	@Nonnull
	static  LBiFunction biFunc(final @Nonnull LBiFunction lambda) {
		Null.nonNullArg(lambda, "lambda");
		return lambda;
	}

	@Nonnull
	static  LBiFunction recursive(final @Nonnull LFunction, LBiFunction> selfLambda) {
		final LBiFunctionSingle single = new LBiFunctionSingle();
		LBiFunction func = selfLambda.apply(single);
		single.target = func;
		return func;
	}

	final class LBiFunctionSingle implements LSingle>, LBiFunction {
		private LBiFunction target = null;

		@Override
		public R applyX(T1 a1, T2 a2) throws Throwable {
			return target.applyX(a1, a2);
		}

		@Override
		public LBiFunction value() {
			return target;
		}
	}

	@Nonnull
	static  LBiFunction biFuncThrowing(final @Nonnull ExF exF) {
		Null.nonNullArg(exF, "exF");
		return (a1, a2) -> {
			throw exF.produce();
		};
	}

	@Nonnull
	static  LBiFunction biFuncThrowing(final String message, final @Nonnull ExMF exF) {
		Null.nonNullArg(exF, "exF");
		return (a1, a2) -> {
			throw exF.produce(message);
		};
	}

	// 

	/** Convenient method in case lambda expression is ambiguous for the compiler (that might happen for overloaded methods accepting different interfaces). */
	@Nonnull
	static  LObj1Obj0Func obj1Obj0Func(final @Nonnull LObj1Obj0Func lambda) {
		Null.nonNullArg(lambda, "lambda");
		return lambda;
	}

	// 

	static  R call(T1 a1, T2 a2, final @Nonnull LBiFunction lambda) {
		Null.nonNullArg(lambda, "lambda");
		return lambda.apply(a1, a2);
	}

	// 

	/** Wraps JRE instance. */
	@Nonnull
	static  LBiFunction wrap(final BiFunction other) {
		return other::apply;
	}
	// 

	// 

	/** Safe instance. That always returns the same value (as produce). */
	@Nonnull
	static  LBiFunction safe() {
		return LBiFunction::produce;
	}

	/** Safe instance supplier. Returns supplier of safe() instance. */
	@Nonnull
	static  LSupplier> safeSupplier() {
		return () -> safe();
	}

	/** Safe wrapping. Either argument function is returned (if it is not null) or safe() instance. */
	@Nonnull
	static  LBiFunction safe(final @Nullable LBiFunction other) {
		if (other == null) {
			return safe();
		} else {
			return other;
		}
	}

	/** Safe supplier. Either argument supplier is returned (if it is not null) or supplier of safe() instance. */
	@Nonnull
	static  LSupplier> safeSupplier(final @Nullable LSupplier> supplier) {
		if (supplier == null) {
			return safeSupplier();
		} else {
			return supplier;
		}
	}

	// 

	// 

	/** Allows to manipulate the domain of the function. */
	@Nonnull
	default  LBiFunction compose(@Nonnull final LFunction before1, @Nonnull final LFunction before2) {
		Null.nonNullArg(before1, "before1");
		Null.nonNullArg(before2, "before2");
		return (v1, v2) -> this.apply(before1.apply(v1), before2.apply(v2));
	}

	public static  LBiFunction composed(@Nonnull final LFunction before1, @Nonnull final LFunction before2, LBiFunction after) {
		return after.compose(before1, before2);
	}

	// 

	// 

	/** Combines two functions together in a order. */
	@Nonnull
	default  LBiFunction then(@Nonnull LFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.apply(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LBiConsumer thenConsume(@Nonnull LConsumer after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.accept(this.apply(a1, a2));
	}

	@Nonnull
	default LBiFunction before(@Nonnull LBiConsumer before) {
		Null.nonNullArg(before, "before");
		return (a1, a2) -> {
			before.accept(a1, a2);
			return this.apply(a1, a2);
		};
	}

	@Nonnull
	default LBiFunction after(@Nonnull LConsumer after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> {
			R result = this.apply(a1, a2);
			after.accept(result);
			return result;
		};
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToByteBiFunction thenToByte(@Nonnull LToByteFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsByte(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToSrtBiFunction thenToSrt(@Nonnull LToSrtFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsSrt(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToIntBiFunction thenToInt(@Nonnull LToIntFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsInt(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToLongBiFunction thenToLong(@Nonnull LToLongFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsLong(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToFltBiFunction thenToFlt(@Nonnull LToFltFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsFlt(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToDblBiFunction thenToDbl(@Nonnull LToDblFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsDbl(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LToCharBiFunction thenToChar(@Nonnull LToCharFunction after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.applyAsChar(this.apply(a1, a2));
	}

	/** Combines two functions together in a order. */
	@Nonnull
	default LBiPredicate thenToBool(@Nonnull LPredicate after) {
		Null.nonNullArg(after, "after");
		return (a1, a2) -> after.test(this.apply(a1, a2));
	}

	// 

	// 

	// 

	/** Converts to function that makes sure that the result is not null. */
	@Nonnull
	default LBiFunction nonNullable() {
		return this::nonNullApply;
	}

	// 

	/** Permutation of LBiFunction for method references. */
	@FunctionalInterface
	interface LObj1Obj0Func extends LBiFunction {
		@Nullable
		R applyObj1Obj0(T2 a2, T1 a1);

		@Override
		default R applyX(T1 a1, T2 a2) {
			return this.applyObj1Obj0(a2, a1);
		}
	}

	// 

	/** Does nothing (LBiFunction) Function */
	public static  R produce(T1 a1, T2 a2) {
		return (R) Function4U.defaultObject;
	}

	/**
	* For each element (or tuple) from arguments, calls the function and passes the result to consumer.
	* Thread safety, fail-fast, fail-safety of this method is not expected.
	*/
	default  void forEach(IndexedRead> ia1, C1 source1, IndexedRead> ia2, C2 source2, LConsumer consumer) {
		int size = ia1.size(source1);
		LOiFunction oiFunc1 = (LOiFunction) ia1.getter();
		size = Integer.min(size, ia2.size(source2));
		LOiFunction oiFunc2 = (LOiFunction) ia2.getter();
		int i = 0;
		for (; i < size; i++) {
			T1 a1 = oiFunc1.apply(source1, i);
			T2 a2 = oiFunc2.apply(source2, i);
			consumer.accept(this.apply(a1, a2));
		}
	}

	/**
	* For each element (or tuple) from arguments, calls the function and passes the result to consumer.
	* Thread safety, fail-fast, fail-safety of this method is not expected.
	*/
	default  void iterate(SequentialRead> sa1, C1 source1, IndexedRead> ia2, C2 source2, LConsumer consumer) {
		Object iterator1 = ((LFunction) sa1.adapter()).apply(source1);
		LPredicate testFunc1 = (LPredicate) sa1.tester();
		LFunction nextFunc1 = (LFunction) sa1.supplier();
		int size = ia2.size(source2);
		LOiFunction oiFunc2 = (LOiFunction) ia2.getter();
		int i = 0;
		while (testFunc1.test(iterator1) && i < size) {
			T1 a1 = nextFunc1.apply(iterator1);
			T2 a2 = oiFunc2.apply(source2, i);
			consumer.accept(this.apply(a1, a2));
			i++;
		}
	}

	/**
	* For each element (or tuple) from arguments, calls the function and passes the result to consumer.
	* Thread safety, fail-fast, fail-safety of this method is not expected.
	*/
	default  void iterate(IndexedRead> ia1, C1 source1, SequentialRead> sa2, C2 source2, LConsumer consumer) {
		int size = ia1.size(source1);
		LOiFunction oiFunc1 = (LOiFunction) ia1.getter();
		Object iterator2 = ((LFunction) sa2.adapter()).apply(source2);
		LPredicate testFunc2 = (LPredicate) sa2.tester();
		LFunction nextFunc2 = (LFunction) sa2.supplier();
		int i = 0;
		while (i < size && testFunc2.test(iterator2)) {
			T1 a1 = oiFunc1.apply(source1, i);
			T2 a2 = nextFunc2.apply(iterator2);
			consumer.accept(this.apply(a1, a2));
			i++;
		}
	}

	/**
	* For each element (or tuple) from arguments, calls the function and passes the result to consumer.
	* Thread safety, fail-fast, fail-safety of this method depends highly on the arguments.
	*/
	default  void iterate(SequentialRead> sa1, C1 source1, SequentialRead> sa2, C2 source2, LConsumer consumer) {
		Object iterator1 = ((LFunction) sa1.adapter()).apply(source1);
		LPredicate testFunc1 = (LPredicate) sa1.tester();
		LFunction nextFunc1 = (LFunction) sa1.supplier();
		Object iterator2 = ((LFunction) sa2.adapter()).apply(source2);
		LPredicate testFunc2 = (LPredicate) sa2.tester();
		LFunction nextFunc2 = (LFunction) sa2.supplier();
		while (testFunc1.test(iterator1) && testFunc2.test(iterator2)) {
			T1 a1 = nextFunc1.apply(iterator1);
			T2 a2 = nextFunc2.apply(iterator2);
			consumer.accept(this.apply(a1, a2));
		}
	}

}