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

functionalj.function.FuncUnit2 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 static java.util.Objects.requireNonNull;

import java.util.function.BiConsumer;

import functionalj.functions.ThrowFuncs;
import functionalj.promise.HasPromise;
import functionalj.promise.Promise;
import functionalj.tuple.Tuple2;
import lombok.val;

@FunctionalInterface
public interface FuncUnit2 extends BiConsumer {
    
    public static  FuncUnit2 of(FuncUnit2 consumer) {
        return consumer;
    }
    public static  FuncUnit2 funcUnit2(FuncUnit2 consumer) {
        return consumer;
    }
    public static  FuncUnit2 from(BiConsumer consumer) {
        return consumer::accept;
    }
    
    public void acceptUnsafe(INPUT1 input1, INPUT2 input2) throws Exception;
    
    public default void accept(INPUT1 input1, INPUT2 input2) {
        try {
            acceptUnsafe(input1, input2);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw ThrowFuncs.exceptionTransformer.value().apply(e);
        }
    }
    
    public default void acceptCarelessly(INPUT1 input1, INPUT2 input2) {
        try {
            acceptUnsafe(input1, input2);
        } catch (Exception e) {
        }
    }
    
    public default FuncUnit2 then(FuncUnit0 after) {
        requireNonNull(after);
        return (input1, input2) -> {
            acceptUnsafe(input1, input2);
            after.runUnsafe();
        };
    }
    public default FuncUnit2 then(FuncUnit2 after) {
        requireNonNull(after);
        return (input1, input2) -> {
            acceptUnsafe(input1, input2);
            after.acceptUnsafe(input1, input2);
        };
    }
    
    public default  Func2 thenReturnNull() {
        return thenReturn(null);
    }
    public default  Func2 thenReturn(T value) {
        return (input1, input2) -> {
            acceptUnsafe(input1, input2);
            return value;
        };
    }
    
    public default  Func2 thenGet(Func0 supplier) {
        requireNonNull(supplier);
        return (input1, input2) -> {
            acceptUnsafe(input1, input2);
            val value = supplier.applyUnsafe();
            return value;
        };
    }
    
    public default FuncUnit2 ignoreNullInput() {
        return (input1, input2) -> {
            if ((input1 != null)
             && (input2 != null))
                acceptUnsafe(input1, input2);
        };
    }
    
    public default FuncUnit1> wholly() {
        return tuple -> {
            val _1 = tuple._1();
            val _2 = tuple._2();
            acceptUnsafe(_1, _2);
        };
    }
    
    public default FuncUnit2 carelessly() {
        return this::acceptCarelessly;
    }
    
    public default Func2> async() {
        return this.thenReturnNull().async();
    }
    public default Func2, HasPromise, Promise> defer() {
        return (promise1, promise2) -> {
            val func0 = this.thenReturnNull();
            return Promise.from(
                    input1 -> promise1,
                    input2 -> promise2,
                    func0);
        };
    }
    public default FuncUnit1 elevateWith(INPUT2 input2) {
        return (input1) -> acceptUnsafe(input1, input2);
    }
    
    //== Partially apply functions ==
    
    @SuppressWarnings("javadoc")
    public default FuncUnit0 bind(INPUT1 i1, INPUT2 i2) {
        return () -> this.acceptUnsafe(i1, i2);
    }
    @SuppressWarnings("javadoc")
    public default FuncUnit1 bind1(INPUT1 i1) {
        return i2 -> this.acceptUnsafe(i1, i2);
    }
    
    @SuppressWarnings("javadoc")
    public default FuncUnit1 bind2(INPUT2 i2) {
        return i1 -> this.acceptUnsafe(i1, i2);
    }
    
    @SuppressWarnings("javadoc")
    public default FuncUnit1 bind(Absent a1, INPUT2 i2) {
        return i1 -> this.acceptUnsafe(i1, i2);
    }
    @SuppressWarnings("javadoc")
    public default FuncUnit1 bind(INPUT1 i1, Absent a2) {
        return i2 -> this.acceptUnsafe(i1, i2);
    }
}