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

functionalj.function.Func0 Maven / Gradle / Ivy

// ============================================================================
// 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.function;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Supplier;

import functionalj.functions.ThrowFuncs;
import functionalj.promise.DeferAction;
import functionalj.ref.ComputeBody;
import functionalj.result.Result;
import lombok.val;

/**
 * Function of zeroth parameter - a supplier.
 * 
 * @param   the output data type.
 * 
 * @author NawaMan -- [email protected]
 */
@FunctionalInterface
public interface Func0 extends Supplier, ComputeBody {
    
    public static  Func0 of(Func0 func0) {
        return func0;
    }
    
    public static  Func0 from(Supplier supplier) {
        if (supplier instanceof Func0)
            return (Func0)supplier;
        
        return supplier::get;
    }
    public static  Func0 from(IntFunction generatorFunction) {
        return from(0, generatorFunction);
    }
    public static  Func0 from(int start, IntFunction generatorFunction) {
        val counter = new AtomicInteger(start);
        return ()-> generatorFunction.apply(counter.getAndIncrement());
    }
    
    public OUTPUT applyUnsafe() throws Exception;
    
    public default OUTPUT getUnsafe() throws Exception {
        return applyUnsafe();
    }
    
    public default OUTPUT get() {
        try {
            return applyUnsafe();
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw ThrowFuncs.exceptionTransformer.value().apply(e);
        }
    }
    
    public default OUTPUT compute() throws RuntimeException {
        return apply();
    }
    
    public default OUTPUT apply() {
        return get();
    }
    
    public default Result applySafely() {
        return Result.of(this);
    }
    
    public default Result getSafely() {
        return Result.of(this);
    }
    
    public default Func0 memoize() {
        return Func0.from(Func.lazy(this));
    }
    
    public default  Func0 then(Func1 mapper) {
        return ()->{
            val output = this.applyUnsafe();
            val target = Func.applyUnsafe(mapper, output);
            return target;
        };
    }
    public default  Func0 map(Func1 mapper) {
        return ()->{
            val output = this.applyUnsafe();
            val target = (output != null) 
                       ? Func.applyUnsafe(mapper, output)
                       : null;
            return target;
        };
    }
    
    public default Func0 ifException(Consumer exceptionHandler) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                return outputValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return null;
            }
        };
    }
    public default Func0 ifExceptionThenPrint() {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                return outputValue;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        };
    }
    public default Func0 ifExceptionThenPrint(PrintStream printStream) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                return outputValue;
            } catch (Exception e) {
                e.printStackTrace(printStream);
                return null;
            }
        };
    }
    public default Func0 ifExceptionThenPrint(PrintWriter printWriter) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                return outputValue;
            } catch (Exception e) {
                e.printStackTrace(printWriter);
                return null;
            }
        };
    }
    
    public default Func0 whenAbsentUse(OUTPUT defaultValue) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultValue;
                return returnValue;
            } catch (Exception e) {
                return defaultValue;
            }
        };
    }
    public default Func0 whenAbsentGet(Supplier defaultSupplier) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultSupplier.get();
                return returnValue;
            } catch (Exception e) {
                return defaultSupplier.get();
            }
        };
    }
    public default Func0 whenAbsentApply(Func1 exceptionMapper) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(null);
                return returnValue;
            } catch (Exception e) {
                return exceptionMapper.apply(e);
            }
        };
    }
    
    public default Func0 whenAbsentUse(Consumer exceptionHandler, OUTPUT defaultValue) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultValue;
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return defaultValue;
            }
        };
    }
    public default Func0 whenAbsentGet(Consumer exceptionHandler, Supplier defaultSupplier) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultSupplier.get();
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return defaultSupplier.get();
            }
        };
    }
    public default Func0 whenAbsentApply(Consumer exceptionHandler, Func1 exceptionMapper) {
        return ()->{
            try {
                val outputValue = this.applyUnsafe();
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(null);
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return exceptionMapper.apply(e);
            }
        };
    }
    
    public default OUTPUT orElse(OUTPUT defaultValue) {
        return getSafely().orElse(defaultValue);
    }
    
    public default OUTPUT orGet(Supplier defaultSupplier) {
        return getSafely().orGet(defaultSupplier);
    }
    
    public default Func0> safely() {
        return Func.of(this::applySafely);
    }
    
    public default Func0> optionally() {
        return () -> {
            try {
                return Optional.ofNullable(this.applyUnsafe());
            } catch (Exception e) {
                return Optional.empty();
            }
        };
    }
    
    public default DeferAction async() {
        return defer();
    }
    public default DeferAction defer() {
        return DeferAction.from(this);
    }
    
    public default FuncUnit0 ignoreResult() {
        return FuncUnit0.of(()->applyUnsafe());
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy