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

eu.lunisolar.magma.func.action.LActionX Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * 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.action;

import javax.annotation.Nonnull; // NOSONAR
import javax.annotation.Nullable; // NOSONAR
import java.util.Objects;// NOSONAR
import java.util.function.Predicate; //NOSONAR
import eu.lunisolar.magma.basics.*; //NOSONAR
import eu.lunisolar.magma.basics.exceptions.*; // NOSONAR
import eu.lunisolar.magma.func.*; // 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.consumer.*; // NOSONAR
import eu.lunisolar.magma.func.function.*; // NOSONAR
import eu.lunisolar.magma.func.supplier.*; // NOSONAR

/**
 * LActionX is a replacement for Runnable.
 *
 * - offers default methods
 * - do not rise warnings about Runnable being call directly
 * - two versions (throwing and non-throwing) have conversion methods that mirrors each other)
 *
 * Throwing functional interface (lambda) LActionX for Java 8.
 *
 * Type: action
 *
 * Domain (lvl: 0): none
 *
 * Co-domain: none
 *
 * @see LAction
 */
@FunctionalInterface
@SuppressWarnings("UnusedDeclaration")
public interface LActionX extends Runnable, MetaAction, MetaInterface.Throwing {

	static final String DESCRIPTION = "LActionX: void doExecute() throws X";

	/**
	 * Default implementation for JRE method that calls exception nesting method.
	 * @deprecated Calling this method via LActionX interface should be discouraged.
	 */
	@Override
	@Deprecated
	default void run() {
		this.nestingDoExecute();
	}

	void doExecute() throws X;

	/** Function call that handles exceptions by always nesting checked exceptions and propagating the otheres as is. */
	default void nestingDoExecute() {
		try {
			this.doExecute();
		} catch (RuntimeException | Error e) { // NOSONAR
			throw e;
		} catch (Throwable e) { // NOSONAR
			throw new NestedException(e);
		}
	}

	/** Function call that handles exceptions by always propagating them as is even when they are undeclared checked ones. */
	default void shovingDoExecute() {
		((LActionX) this).doExecute();
	}

	/** Function call that handles exceptions according to the instructions. */
	default  void handlingDoExecute(HandlingInstructions handling) throws Y {

		try {
			this.doExecute();
		} catch (Throwable e) { // NOSONAR
			throw Handler.handleOrNest(e, handling);
		}
	}

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

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

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

	// 

	/** Wraps JRE instance. */
	@Nonnull
	static  LActionX wrap(final Runnable other) {
		return other::run;
	}

	/** Wraps opposite (throwing vs non-throwing) instance. */
	@Nonnull
	static  LActionX wrapX(final @Nonnull LAction other) {
		return (LActionX) other;
	}

	// 

	// 

	/** Combines two actions together in a order. */
	@Nonnull
	default LActionX andThen(@Nonnull LActionX after) {
		Null.nonNullArg(after, "after");
		return () -> {
			this.doExecute();
			after.doExecute();
		};
	}

	//  // 

	/** Converts to non-throwing variant (if required). */
	@Nonnull
	default LAction nestingAct() {
		return this::nestingDoExecute;
	}

	/** Converts to throwing variant (RuntimeException). */
	@Nonnull
	default LActionX nestingActX() {
		return this::nestingDoExecute;
	}

	/** 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 LAction shovingAct() {
		return this::shovingDoExecute;
	}

	/** 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 LActionX shovingActX() {
		return this::shovingDoExecute;
	}

	// 

	// 

	/** Converts to function that handles exceptions according to the instructions. */
	@Nonnull
	default LAction handleAct(@Nonnull HandlingInstructions handling) {
		return () -> this.handlingDoExecute(handling);
	}

	/** Converts to function that handles exceptions according to the instructions. */
	@Nonnull
	default  LActionX handleActX(@Nonnull HandlingInstructions handling) {
		return () -> this.handlingDoExecute(handling);
	}

	// 

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy