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-2019 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.Consumer;
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(Func2> processor);
    
    public Result asResult();
    
    
    public default Result useData(FuncUnit2 processor) {
        mapData(throwException(), (value, exception)->{
            processor.accept(value, exception);
            return null;
        });
        return asResult();
    }
    
    //== 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(Func2 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(Func2 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(Func1 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(Func1 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 ResultStatus(Runnable runnable) {
        useData(helper.processIf(ResultStatus::isCancelled, runnable));
        return asResult();
    }
    
    
    public default Result ResultStatus(Consumer consumer) {
        useData(helper.processIfException(ResultStatus::isCancelled, consumer));
        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(Func1 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(Func2 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(Func1 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(Func1 recoverFunction) {
        return mapValue(helper.processWhenApply(ResultStatus::isNoMore, asResult(), recoverFunction));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy