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

functionalj.result.ResultStatusAddOn Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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 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 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 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 consumer) {
        useData(Helper.processIf(ResultStatus::isAbsent, consumer));
        return asResult();
    }
    
    public default Result ifAbsent(BiConsumer 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 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 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 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 consumer) {
        useData(Helper.processIf(ResultStatus::isNotValue, consumer));
        return asResult();
    }
    
    public default Result ifNotValue(BiConsumer 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 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 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 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 fallbackSupplier) {
        return mapValue(Helper.processWhenGet(ResultStatus::isInvalid, asResult(), fallbackSupplier));
    }
    public default Result whenInvalidApply(Func1 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 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 fallbackSupplier) {
        return mapValue(Helper.processWhenGet(ResultStatus::isNotExist, asResult(), fallbackSupplier));
    }
    public default Result whenNotExistApply(Function 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 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 fallbackSupplier) {
        return mapValue(Helper.processWhenGet(ResultStatus::isException, asResult(), fallbackSupplier));
    }
    public default Result whenExceptionApply(Function recoverFunction) {
        return mapValue(Helper.processWhenApply(ResultStatus::isException, asResult(), recoverFunction));
    }
    
    public default Result recover(Class 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 problemClass, Supplier 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 problemClass, Func1 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 fallbackSupplier) {
        return mapValue(Helper.processWhenGet(ResultStatus::isCancelled, asResult(), fallbackSupplier));
    }
    public default Result whenCancelledApply(Function 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 consumer) {
        useData(Helper.processIf(ResultStatus::isReady, consumer));
        return asResult();
    }
    
    public default Result ifReady(BiConsumer 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 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 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 fallbackSupplier) {
        return mapValue(Helper.processWhenGet(ResultStatus::isNotReady, asResult(), fallbackSupplier));
    }
    public default Result whenNotReadyApply(Function 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 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 fallbackSupplier) {
        return mapValue(Helper.processWhenGet(ResultStatus::isNoMore, asResult(), fallbackSupplier));
    }
    public default Result whenNoMoreApply(Function recoverFunction) {
        return mapValue(Helper.processWhenApply(ResultStatus::isNoMore, asResult(), recoverFunction));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy