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

functionalj.function.Func5 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.function;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import functionalj.functions.ThrowFuncs;
import functionalj.promise.DeferAction;
import functionalj.promise.HasPromise;
import functionalj.promise.Promise;
import functionalj.result.Result;
import functionalj.tuple.Tuple;
import functionalj.tuple.Tuple5;
import lombok.val;
import nullablej.nullable.Nullable;

/**
 * Function of five parameters.
 * 
 * @param   the first input data type.
 * @param   the second input data type.
 * @param   the third input data type.
 * @param   the third input data type.
 * @param   the third input data type.
 * @param   the output data type.
 * 
 * @author NawaMan -- [email protected]
 */
@FunctionalInterface
public interface Func5 {

    public static  Func5 of(Func5 func) {
        return func;
    }
    
    public OUTPUT applyUnsafe(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5) throws Exception;
    
    /**
     * Applies this function to the given input values.
     *
     * @param  input1  the first input.
     * @param  input2  the second input.
     * @param  input3  the third input.
     * @param  input4  the forth input.
     * @param  input5  the fifth input.
     * @return         the function result.
     */
    public default OUTPUT apply(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5) {
        try {
            return applyUnsafe(input1, input2, input3, input4, input5);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw ThrowFuncs.exceptionTransformer.value().apply(e);
        }
    }
    
    
    /**
     * Applies this function to the given input values.
     *
     * @param  input the tuple input.
     * @return       the function result.
     */
    public default OUTPUT applyTo(Tuple5 input) {
        return apply(input._1(), input._2(), input._3(), input._4(), input._5());
    }
    public default Func4 applyTo(INPUT1 input1) {
        return (input2, input3, input4, input5) -> apply(input1, input2, input3, input4, input5);
    }
    public default Result applyTo(Result input1, Result input2, Result input3, Result input4, Result input5) {
        return Result.ofResults(input1, input2, input3, input4, input5, this);
    }
    public default Optional applyTo(Optional input1, Optional input2, Optional input3, Optional input4, Optional input5) {
        return input1.flatMap(i1 -> {
            return input2.flatMap(i2 -> {
                return input3.flatMap(i3 -> {
                    return input4.flatMap(i4 -> {
                        return input5.map(i5 -> {
                            return Func5.this.apply(i1, i2, i3, i4, i5);
                        });
                    });
                });
            });
        });
    }
    public default Nullable applyTo(Nullable input1, Nullable input2, Nullable input3, Nullable input4, Nullable input5) {
        return input1.flatMap(i1 -> {
            return input2.flatMap(i2 -> {
                return input3.flatMap(i3 -> {
                    return input4.flatMap(i4 -> {
                        return input5.map(i5 -> {
                            return Func5.this.apply(i1, i2, i3, i4, i5);
                        });
                    });
                });
            });
        });
    }
    public default Promise applyTo(HasPromise input1, HasPromise input2, HasPromise input3, HasPromise input4, HasPromise input5) {
        return Promise.from(input1, input2, input3, input4, input5, this);
    }
    public default Func0 applyTo(Supplier input1, Supplier input2, Supplier input3, Supplier input4, Supplier input5) {
        return ()->apply(input1.get(), input2.get(), input3.get(), input4.get(), input5.get());
    }
    
    public default Result applySafely(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5) {
        try {
            val output = applyUnsafe(input1, input2, input3, input4, input5);
            return Result.valueOf(output);
        } catch (Exception exception) {
            return Result.ofException(exception);
        }
    }
    
    /**
     * Compose this function to the given function.
     * NOTE: Too bad the name 'compose' is already been taken :-(
     * 
     * @param    the target result value.
     * @param  after     the function to be run after this function.
     * @return           the composed function.
     */
    public default  Func5 then(Function after) {
        return (input1, input2, input3, input4, input5) -> {
            OUTPUT output = this.applyUnsafe(input1, input2, input3, input4, input5);
            TARGET target = Func.applyUnsafe(after, output);
            return target;
        };
    }
    public default  Func5 map(Function after) {
        return (input1, input2, input3, input4, input5) -> {
            OUTPUT output = this.applyUnsafe(input1, input2, input3, input4, input5);
            TARGET target = (output != null)
                          ? Func.applyUnsafe(after, output)
                          : null;
            return target;
        };
    }
    
    public default Func5 ifException(Consumer exceptionHandler) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                return outputValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return null;
            }
        };
    }
    public default Func5 ifExceptionThenPrint() {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                return outputValue;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        };
    }
    public default Func5 ifExceptionThenPrint(PrintStream printStream) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                return outputValue;
            } catch (Exception e) {
                e.printStackTrace(printStream);
                return null;
            }
        };
    }
    public default Func5 ifExceptionThenPrint(PrintWriter printWriter) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                return outputValue;
            } catch (Exception e) {
                e.printStackTrace(printWriter);
                return null;
            }
        };
    }
    
    public default Func5 whenAbsentUse(OUTPUT defaultValue) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultValue;
                return returnValue;
            } catch (Exception e) {
                return defaultValue;
            }
        };
    }
    public default Func5 whenAbsentGet(Supplier defaultSupplier) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultSupplier.get();
                return returnValue;
            } catch (Exception e) {
                return defaultSupplier.get();
            }
        };
    }
    public default Func5 whenAbsentApply(Func1 exceptionMapper) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(null);
                return returnValue;
            } catch (Exception e) {
                return exceptionMapper.apply(e);
            }
        };
    }
    public default Func5 whenAbsentApply(Func6 exceptionMapper) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(input1, input2, input3, input4, input5, null);
                return returnValue;
            } catch (Exception e) {
                return exceptionMapper.apply(input1, input2, input3, input4, input5, e);
            }
        };
    }
    public default Func5 whenAbsentApply(Func2, Exception, OUTPUT> exceptionMapper) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(Tuple.of(input1, input2, input3, input4, input5), null);
                return returnValue;
            } catch (Exception e) {
                return exceptionMapper.apply(Tuple.of(input1, input2, input3, input4, input5), e);
            }
        };
    }
    
    public default Func5 whenAbsentUse(Consumer exceptionHandler, OUTPUT defaultValue) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultValue;
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return defaultValue;
            }
        };
    }
    public default Func5 whenAbsentGet(Consumer exceptionHandler, Supplier defaultSupplier) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : defaultSupplier.get();
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return defaultSupplier.get();
            }
        };
    }
    public default Func5 whenAbsentApply(Consumer exceptionHandler, Func1 exceptionMapper) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(null);
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return exceptionMapper.apply(e);
            }
        };
    }
    public default Func5 whenAbsentApply(Consumer exceptionHandler, Func6 exceptionMapper) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(input1, input2, input3, input4, input5, null);
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return exceptionMapper.apply(input1, input2, input3, input4, input5, e);
            }
        };
    }
    public default Func5 whenAbsentApply(Consumer exceptionHandler, Func2, Exception, OUTPUT> exceptionMapper) {
        return (input1, input2, input3, input4, input5)->{
            try {
                val outputValue = this.applyUnsafe(input1, input2, input3, input4, input5);
                val returnValue 
                        = (outputValue != null)
                        ? outputValue
                        : exceptionMapper.apply(Tuple.of(input1, input2, input3, input4, input5), null);
                return returnValue;
            } catch (Exception e) {
                exceptionHandler.accept(e);
                return exceptionMapper.apply(Tuple.of(input1, input2, input3, input4, input5), e);
            }
        };
    }
    
    public default OUTPUT orElse(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5, OUTPUT defaultValue) {
        return applySafely(input1, input2, input3, input4, input5).orElse(defaultValue);
    }
    
    public default OUTPUT orGet(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5, Supplier defaultSupplier) {
        return applySafely(input1, input2, input3, input4, input5).orGet(defaultSupplier);
    }
    
    public default Func5> safely() {
        return Func.of(this::applySafely);
    }
    
    public default Func5> optionally() {
        return (input1, input2, input3, input4, input5) -> {
            try {
                return Optional.ofNullable(this.applyUnsafe(input1, input2, input3, input4, input5));
            } catch (Exception e) {
                return Optional.empty();
            }
        };
    }
    
    public default 

> Func5> async() { return (input1, input2, input3, input4, input5) -> { val supplier = (Func0)()->{ return this.applyUnsafe(input1, input2, input3, input4, input5); }; return DeferAction.from(supplier) .start().getPromise(); }; } public default Func5, HasPromise, HasPromise, HasPromise, HasPromise, Promise> defer() { return (promise1, promise2, promise3, promise4, promise5) -> { return Promise.from(promise1, promise2, promise3, promise4, promise5, this); }; } public default Func1, OUTPUT> wholly() { return t -> this.applyUnsafe(t._1(), t._2(), t._3(), t._4(), t._5()); } /** * Flip the parameter order. * * @return the Func5 with parameter in a flipped order. */ public default Func5 flip() { return (i5, i4, i3, i2, i1) -> this.applyUnsafe(i1, i2, i3, i4, i5); } public default Func4> elevate() { return (i2, i3, i4, i5) -> (i1) -> this.applyUnsafe(i1, i2, i3, i4, i5); } public default Func1 elevateWith(INPUT2 i2, INPUT3 i3, INPUT4 i4, INPUT5 i5) { return (i1) -> this.applyUnsafe(i1, i2, i3, i4, i5); } public default Func1> split() { return split1(); } public default Func1> split1() { return (i1) -> (i2, i3, i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } public default Func2> split2() { return (i1, i2) -> (i3, i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } public default Func3> split3() { return (i1, i2, i3) -> (i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } public default Func4> split4() { return (i1, i2, i3, i4) -> (i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } //== Partially apply functions == @SuppressWarnings("javadoc") public default Func0 bind(INPUT1 i1, INPUT2 i2, INPUT3 i3, INPUT4 i4, INPUT5 i5) { return () -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind1(INPUT1 i1) { return (i2,i3,i4,i5) -> this.applyUnsafe(i1, i2, i3,i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind2(INPUT2 i2) { return (i1,i3,i4,i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind3(INPUT3 i3) { return (i1,i2,i4,i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind4(INPUT4 i4) { return (i1,i2,i3,i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind5(INPUT5 i5) { return (i1,i2,i3,i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func1 bind(Absent a1, INPUT2 i2, INPUT3 i3, INPUT4 i4, INPUT5 i5) { return i1 -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func1 bind(INPUT1 i1, Absent a2, INPUT3 i3, INPUT4 i4, INPUT5 i5) { return i2 -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func1 bind(INPUT1 i1, INPUT2 i2, Absent a3, INPUT4 i4, INPUT5 i5) { return i3 -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func1 bind(INPUT1 i1, INPUT2 i2, INPUT3 i3, Absent a4, INPUT5 i5) { return i4 -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func1 bind(INPUT1 i1, INPUT2 i2, INPUT3 i3, INPUT4 i4, Absent a5) { return i5 -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(Absent a1, Absent a2, INPUT3 i3, INPUT4 i4, INPUT5 i5) { return (i1, i2) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(Absent a1, INPUT2 i2, Absent a3, INPUT4 i4, INPUT5 i5) { return (i1, i3) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(Absent a1, INPUT2 i2, INPUT3 i3, Absent a4, INPUT5 i5) { return (i1, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(Absent a1, INPUT2 i2, INPUT3 i3, INPUT4 i4, Absent a5) { return (i1, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(INPUT1 i1, Absent a2, Absent a3, INPUT4 i4, INPUT5 i5) { return (i2, i3) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(INPUT1 i1, Absent a2, INPUT3 i3, Absent a4, INPUT5 i5) { return (i2, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(INPUT1 i1, Absent a2, INPUT3 i3, INPUT4 i4, Absent a5) { return (i2, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(INPUT1 i1, INPUT2 i2, Absent a3, Absent a4, INPUT5 i5) { return (i3, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(INPUT1 i1, INPUT2 i2, Absent a3, INPUT4 i4, Absent a5) { return (i3, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func2 bind(INPUT1 i1, INPUT2 i2, INPUT3 i3, Absent a4, Absent a5) { return (i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(Absent a1, Absent a2, Absent a3, INPUT4 i4, INPUT5 i5) { return (i1, i2, i3) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(Absent a1, Absent a2, INPUT3 i3, Absent a4, INPUT5 i5) { return (i1, i2, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(Absent a1, Absent a2, INPUT3 i3, INPUT4 i4, Absent a5) { return (i1, i2, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(Absent a1, INPUT2 i2, Absent a3, Absent a4, INPUT5 i5) { return (i1, i3, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(Absent a1, INPUT2 i2, Absent a3, INPUT4 i4, Absent a5) { return (i1, i3, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(INPUT1 i1, Absent a2, Absent a3, Absent a4, INPUT5 i5) { return (i2, i3, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(INPUT1 i1, Absent a2, Absent a3, INPUT4 i4, Absent a5) { return (i2, i3, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func3 bind(INPUT1 i1, INPUT2 i2, Absent a3, Absent I4, Absent a5) { return (i3, i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind(Absent a1, Absent a2, Absent a3, Absent a4, INPUT5 i5) { return (i1, i2, i3, i4) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind(Absent a1, Absent a2, Absent a3, INPUT4 i4, Absent a5) { return (i1, i2, i3, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind(Absent a1, Absent a2, INPUT3 i3, Absent a4, Absent a5) { return (i1, i2, i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind(Absent a1, INPUT2 i2, Absent a3, Absent a4, Absent a5) { return (i1, i3, i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } @SuppressWarnings("javadoc") public default Func4 bind(INPUT1 i1, Absent a2, Absent a3, Absent a4, Absent a5) { return (i2, i3, i4, i5) -> this.applyUnsafe(i1, i2, i3, i4, i5); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy