functionalj.result.ResultStatusAddOn Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.result;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import functionalj.function.Func0;
import functionalj.function.Func1;
import functionalj.function.Func2;
import functionalj.function.FuncUnit2;
public interface ResultStatusAddOn {
public static Func1 returnFalse() {
return e -> false;
}
public static Func1> returnValueException() {
return e -> Result.ofException(e);
}
public static Func1 returnTrue() {
return e -> false;
}
public static Func1 throwException() {
return e -> { throw e; };
}
public T mapData(Func1 exceptionGet, Func2 processor);
public Result mapValue(BiFunction> processor);
public Result asResult();
public default Result useData(FuncUnit2 processor) {
mapData(throwException(), (value, exception)->{
processor.accept(value, exception);
return null;
});
return asResult();
}
//== Status ==
public default boolean isStatus(ResultStatus status) {
return mapData(returnFalse(), Helper.processIs(status::equals));
}
public default Result ifStatusRun(ResultStatus status, Runnable runnable) {
useData(Helper.processIf(status::equals, runnable));
return asResult();
}
public default Result ifStatusAccept(ResultStatus status, Consumer super DATA> consumer) {
useData(Helper.processIf(status::equals, consumer));
return asResult();
}
public default Result whenStatusUse(ResultStatus status, DATA fallbackValue) {
return mapValue(Helper.processWhenUse(status::equals, asResult(), fallbackValue));
}
public default Result whenStatusGet(ResultStatus status, Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(status::equals, asResult(), fallbackSupplier));
}
public default Result whenStatusApply(ResultStatus status, BiFunction recoverFunction) {
return mapValue(Helper.processWhenApply(status::equals, asResult(), recoverFunction));
}
//== Present ==
public default boolean isPresent() {
return mapData(returnFalse(), Helper.processIs(ResultStatus::isPresent));
}
public default Result ifPresent(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isPresent, runnable));
return asResult();
}
public default Result ifPresent(Consumer super DATA> consumer) {
useData(Helper.processIf(ResultStatus::isPresent, consumer));
return asResult();
}
//== Absent ==
public default boolean isAbsent() {
return mapData(returnFalse(), Helper.processIs(ResultStatus::isAbsent));
}
public default Result ifAbsent(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isAbsent, runnable));
return asResult();
}
public default Result ifAbsent(Consumer super DATA> consumer) {
useData(Helper.processIf(ResultStatus::isAbsent, consumer));
return asResult();
}
public default Result ifAbsent(BiConsumer super DATA, ? super Exception> consumer) {
useData(Helper.processIf(ResultStatus::isAbsent, consumer));
return asResult();
}
public default Result whenAbsentUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isAbsent, asResult(), fallbackValue));
}
public default Result whenAbsentGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isAbsent, asResult(), fallbackSupplier));
}
public default Result whenAbsentApply(BiFunction recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isAbsent, asResult(), recoverFunction));
}
//== Null ==
public default boolean isNull() {
return mapData(returnTrue(), Helper.processIs(ResultStatus::isNull));
}
public default Result ifNull(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isNull, runnable));
return asResult();
}
public default Result whenNullUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isNull, asResult(), fallbackValue));
}
public default Result whenNullGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isNull, asResult(), fallbackSupplier));
}
//== Value ==
public default boolean isValue() {
return mapData(returnFalse(), Helper.processIs(ResultStatus::isValue));
}
public default Result ifValue(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isValue, runnable));
return asResult();
}
public default Result ifValue(Consumer super DATA> consumer) {
useData(Helper.processIf(ResultStatus::isValue, consumer));
return asResult();
}
//== NotValue ==
public default boolean isNotValue() {
return mapData(returnFalse(), Helper.processIs(ResultStatus::isNotValue));
}
public default Result ifNotValue(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isNotValue, runnable));
return asResult();
}
public default Result ifNotValue(Consumer super DATA> consumer) {
useData(Helper.processIf(ResultStatus::isNotValue, consumer));
return asResult();
}
public default Result ifNotValue(BiConsumer super DATA, ? super Exception> consumer) {
useData(Helper.processIf(ResultStatus::isNotValue, consumer));
return asResult();
}
public default Result whenNotValueUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isNotValue, asResult(), fallbackValue));
}
public default Result whenNotValueGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isNotValue, asResult(), fallbackSupplier));
}
public default Result whenNotValueApply(BiFunction recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isNotValue, asResult(), recoverFunction));
}
//== Valid ==
public default boolean isValid() {
return isValue();
}
public default Result ifValid(Consumer super DATA> consumer) {
return ifValue(consumer);
}
//== Invalid ==
public default boolean isInvalid() {
return mapData(returnFalse(), Helper.processIs(ResultStatus::isInvalid));
}
public default Result ifInvalid(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isInvalid, runnable));
return asResult();
}
public default Result ifInvalid(Consumer super Exception> consumer) {
useData(Helper.processIfException(ResultStatus::isInvalid, consumer));
return asResult();
}
public default Result whenInvalidUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isInvalid, asResult(), fallbackValue));
}
public default Result whenInvalidGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isInvalid, asResult(), fallbackSupplier));
}
public default Result whenInvalidApply(Func1 super Exception,? extends DATA> recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isInvalid, asResult(), recoverFunction));
}
//== NotExist ==
public default boolean isNotExist() {
return mapData(returnTrue(), Helper.processIs(ResultStatus::isNotExist));
}
public default Result ifNotExist(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isNotExist, runnable));
return asResult();
}
public default Result ifNotExist(Consumer super Exception> consumer) {
useData(Helper.processIfException(ResultStatus::isNotExist, consumer));
return asResult();
}
public default Result whenNotExistUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isNotExist, asResult(), fallbackValue));
}
public default Result whenNotExistGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isNotExist, asResult(), fallbackSupplier));
}
public default Result whenNotExistApply(Function super Exception,? extends DATA> recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isNotExist, asResult(), recoverFunction));
}
//== Exception ==
public default boolean isException() {
return mapData(returnFalse(), Helper.processIs(ResultStatus::isException));
}
public default Result ifException(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isException, runnable));
return asResult();
}
public default Result ifException(Consumer super Exception> consumer) {
useData(Helper.processIfException(ResultStatus::isException, consumer));
return asResult();
}
public default Result ifExceptionThenPrint() {
useData(Helper.processIfException(ResultStatus::isException, exception -> exception.printStackTrace()));
return asResult();
}
public default Result ifExceptionThenPrint(PrintStream printStream) {
useData(Helper.processIfException(ResultStatus::isException, exception -> exception.printStackTrace(printStream)));
return asResult();
}
public default Result ifExceptionThenPrint(PrintWriter printWriter) {
useData(Helper.processIfException(ResultStatus::isException, exception -> exception.printStackTrace(printWriter)));
return asResult();
}
public default Result whenExceptionUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isException, asResult(), fallbackValue));
}
public default Result whenExceptionGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isException, asResult(), fallbackSupplier));
}
public default Result whenExceptionApply(Function super Exception,? extends DATA> recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isException, asResult(), recoverFunction));
}
public default Result recover(Class extends Throwable> problemClass, DATA fallbackValue) {
return mapValue((data, exception)->{
if (exception == null)
return asResult();
if (!problemClass.isInstance(exception))
return asResult();
return Result.valueOf(fallbackValue);
});
}
public default Result recover(Class extends Throwable> problemClass, Supplier extends DATA> fallbackSupplier) {
return mapValue((data, exception)->{
if (exception == null)
return asResult();
if (!problemClass.isInstance(exception))
return asResult();
return Result.of(Func0.from(fallbackSupplier));
});
}
public default Result recover(Class extends Throwable> problemClass, Func1 super Exception,? extends DATA> recoverFunction) {
return mapValue((data, exception)->{
if (exception == null)
return asResult();
if (!problemClass.isInstance(exception))
return asResult();
return Result.valueOf(exception).map(recoverFunction);
});
}
//== Cancelled ==
public default boolean isCancelled() {
return mapData(returnTrue(), Helper.processIs(ResultStatus::isCancelled));
}
public default Result ifCancelled(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isCancelled, runnable));
return asResult();
}
public default Result whenCancelledUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isCancelled, asResult(), fallbackValue));
}
public default Result whenCancelledGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isCancelled, asResult(), fallbackSupplier));
}
public default Result whenCancelledApply(Function super Exception,? extends DATA> recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isCancelled, asResult(), recoverFunction));
}
//== Ready ==
public default boolean isReady() {
return mapData(returnTrue(), Helper.processIs(ResultStatus::isReady));
}
public default Result ifReady(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isReady, runnable));
return asResult();
}
public default Result ifReady(Consumer super DATA> consumer) {
useData(Helper.processIf(ResultStatus::isReady, consumer));
return asResult();
}
public default Result ifReady(BiConsumer super DATA, ? super Exception> consumer) {
useData(Helper.processIf(ResultStatus::isReady, consumer));
return asResult();
}
public default Result whenReadyUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isReady, asResult(), fallbackValue));
}
public default Result whenReadyGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isReady, asResult(), fallbackSupplier));
}
public default Result whenNotReadyApply(BiFunction recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isReady, asResult(), recoverFunction));
}
//== Not Ready ==
public default boolean isNotReady() {
return mapData(returnTrue(), Helper.processIs(ResultStatus::isNotReady));
}
public default Result ifNotReady(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isNotReady, runnable));
return asResult();
}
public default Result ifNotReady(Consumer super Exception> consumer) {
useData(Helper.processIfException(ResultStatus::isNotReady, consumer));
return asResult();
}
public default Result whenNotReadyUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isNotReady, asResult(), fallbackValue));
}
public default Result whenNotReadyGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isNotReady, asResult(), fallbackSupplier));
}
public default Result whenNotReadyApply(Function super Exception,? extends DATA> recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isNotReady, asResult(), recoverFunction));
}
//== No More Result ==
public default boolean isNoMore() {
return mapData(returnTrue(), Helper.processIs(ResultStatus::isNoMore));
}
public default Result ifNoMore(Runnable runnable) {
useData(Helper.processIf(ResultStatus::isNoMore, runnable));
return asResult();
}
public default Result ifNoMore(Consumer super Exception> consumer) {
useData(Helper.processIfException(ResultStatus::isNoMore, consumer));
return asResult();
}
public default Result whenNoMoreUse(DATA fallbackValue) {
return mapValue(Helper.processWhenUse(ResultStatus::isNoMore, asResult(), fallbackValue));
}
public default Result whenNoMoreGet(Supplier extends DATA> fallbackSupplier) {
return mapValue(Helper.processWhenGet(ResultStatus::isNoMore, asResult(), fallbackSupplier));
}
public default Result whenNoMoreApply(Function super Exception,? extends DATA> recoverFunction) {
return mapValue(Helper.processWhenApply(ResultStatus::isNoMore, asResult(), recoverFunction));
}
}