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

functionalj.ref.Substitute 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.ref;

import java.util.List;

import functionalj.function.Func;
import functionalj.function.Func0;
import functionalj.function.Func1;
import functionalj.function.Func2;
import functionalj.function.Func3;
import functionalj.function.Func4;
import functionalj.function.Func5;
import functionalj.function.Func6;
import functionalj.function.FuncUnit0;
import functionalj.function.FuncUnit1;
import functionalj.function.FuncUnit2;
import functionalj.function.FuncUnit3;
import functionalj.list.FuncList;
import functionalj.list.ImmutableFuncList;
import lombok.val;

public class Substitute {
    
    
    public static Substitute Using(Substitution ... substitutions) {
        return Using(ImmutableFuncList.of(substitutions));
    }
    public static Substitute Using(List> substitutions) {
        return new Substitute(substitutions);
    }
    
    public static Substitute using(Substitution ... substitutions) {
        return Using(ImmutableFuncList.of(substitutions));
    }
    
    public static Substitute using(List> substitutions) {
        return new Substitute(substitutions);
    }
    
    
    private final FuncList> substitutions;
    
    Substitute() {
        this.substitutions = FuncList.empty();
    }
    Substitute(List> substitutions) {
        this.substitutions = FuncList.from(substitutions);
    }
    
    public FuncList> substitutions() {
        return this.substitutions;
    }
    
    public Substitute and(Substitution ... newSubstitutions) {
        return new Substitute(substitutions.appendAll(newSubstitutions));
    }
    public Substitute and(List> newSubstitutions) {
        return new Substitute(substitutions.appendAll(newSubstitutions));
    }
    
    //== Around ==
    
    public Runnable arround(Runnable runnable) {
        return () -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> {
                runnable.run();
            });
        };
    }
    
    public  Func0 arround(Func0 supplier) {
        return Func.f(() -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> supplier.applyUnsafe());
        });
    }
    
    public  Func1 arround(Func1 function) {
        return Func.f(input -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input));
        });
    }
    
    public  Func2 arround(Func2 function) {
        return Func.f((input1, input2) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2));
        });
    }
    
    public  Func3 arround(Func3 function) {
        return Func.f((input1, input2, input3) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3));
        });
    }
    
    public  Func4 arround(Func4 function) {
        return Func.f((input1, input2, input3, input4) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3, input4));
        });
    }
    
    public  Func5 arround(Func5 function) {
        return Func.f((input1, input2, input3, input4, input5) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3, input4, input5));
        });
    }
    
    public  Func6 arround(Func6 function) {
        return Func.f((input1, input2, input3, input4, input5, input6) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3, input4, input5, input6));
        });
    }
    
    public FuncUnit0 arround(FuncUnit0 runnable) {
        return Func.f(() -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> runnable.runUnsafe());
        });
    }
    
    public  FuncUnit1 arround(FuncUnit1 function) {
        return Func.f(input -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> function.acceptUnsafe(input));
        });
    }
    
    public  FuncUnit2 arround(FuncUnit2 function) {
        return Func.f((input1, input2) -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> function.acceptUnsafe(input1, input2));
        });
    }
    
    public  FuncUnit3 arround(FuncUnit3 function) {
        return Func.f((input1, input2, input3) -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> function.acceptUnsafe(input1, input2, input3));
        });
    }
    
    //== Within ==
    
    public  Func0 withIn(Func0 supplier) {
        return Func.f(() -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> supplier.applyUnsafe());
        });
    }
    
    public  Func1 withIn(Func1 function) {
        return Func.f(input -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input));
        });
    }
    
    public  Func2 withIn(Func2 function) {
        return Func.f((input1, input2) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2));
        });
    }
    
    public  Func3 withIn(Func3 function) {
        return Func.f((input1, input2, input3) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3));
        });
    }
    
    public  Func4 withIn(Func4 function) {
        return Func.f((input1, input2, input3, input4) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3, input4));
        });
    }
    
    public  Func5 withIn(Func5 function) {
        return Func.f((input1, input2, input3, input4, input5) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3, input4, input5));
        });
    }
    
    public  Func6 withIn(Func6 function) {
        return Func.f((input1, input2, input3, input4, input5, input6) -> {
            val substitutions = substitutions();
            return Ref.runWith(substitutions, () -> function.applyUnsafe(input1, input2, input3, input4, input5, input6));
        });
    }
    
    public FuncUnit0 withIn(FuncUnit0 runnable) {
        return Func.f(() -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> runnable.runUnsafe());
        });
    }
    
    public  FuncUnit1 withIn(FuncUnit1 function) {
        return Func.f(input -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> function.acceptUnsafe(input));
        });
    }
    
    public  FuncUnit2 withIn(FuncUnit2 function) {
        return Func.f((input1, input2) -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> function.acceptUnsafe(input1, input2));
        });
    }
    
    public  FuncUnit3 withIn(FuncUnit3 function) {
        return Func.f((input1, input2, input3) -> {
            val substitutions = substitutions();
            Ref.runWith(substitutions, () -> function.acceptUnsafe(input1, input2, input3));
        });
    }
    
}