eu.lunisolar.magma.func.action.LAction 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.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
/**
* LAction 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)
*
* Non-throwing functional interface (lambda) LAction for Java 8.
*
* Type: action
*
* Domain (lvl: 0): none
*
* Co-domain: none
*
* @see LActionX
*/
@FunctionalInterface
@SuppressWarnings("UnusedDeclaration")
public interface LAction extends LActionX, MetaAction, MetaInterface.NonThrowing {
static final String DESCRIPTION = "LAction: void doExecute()";
/**
* Default implementation for JRE method that calls exception nesting method.
* @deprecated Calling this method via LAction interface should be discouraged.
*/
@Override
@Deprecated
default void run() {
this.nestingDoExecute();
}
void doExecute();
/** Function call that handles exceptions by always nesting checked exceptions and propagating the otheres as is. */
default void nestingDoExecute() {
this.doExecute();
}
/** Function call that handles exceptions by always propagating them as is even when they are undeclared checked ones. */
default void shovingDoExecute() {
this.doExecute();
}
/** Returns description of the functional interface. */
@Nonnull
default String functionalInterfaceDescription() {
return LAction.DESCRIPTION;
}
/** Convenient method in case lambda expression is ambiguous for the compiler (that might happen for overloaded methods accepting different interfaces). */
@Nonnull
static LAction l(final @Nonnull LAction lambda) {
Null.nonNullArg(lambda, "lambda");
return lambda;
}
//
/** Wraps JRE instance. */
@Nonnull
static LAction wrap(final Runnable other) {
return other::run;
}
/** Wraps opposite (throwing vs non-throwing) instance. */
@Nonnull
static LAction wrap(final @Nonnull LActionX other) {
return other::nestingDoExecute;
}
//
//
/** Combines two actions together in a order. */
@Nonnull
default LAction andThen(@Nonnull LAction after) {
Null.nonNullArg(after, "after");
return () -> {
this.doExecute();
after.doExecute();
};
}
// //
/** Converts to non-throwing variant (if required). */
@Nonnull
default LAction nestingAct() {
return this;
}
/** Converts to throwing variant (RuntimeException). */
@Nonnull
default LActionX nestingActX() {
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 LAction shovingAct() {
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 LActionX shovingActX() {
return this;
}
//
}