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

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

public abstract class Annotated implements Named, Traced {
    
    private final String toString;
    
    protected Annotated(String type, String name) {
        this(type, name, null);
    }
    protected Annotated(String type, String name, String location) {
        this.toString = type + prefixWhenNotBlank("::", name) + prefixWhenNotBlank("@", location);
    }
    private String prefixWhenNotBlank(String prefix, String location) {
        return ((location != null) && !location.isEmpty()) ? prefix + location : "";
    }
    
    public final String getName() {
        return this.toString.replaceFirst("^.*::", "").replaceFirst("^(.*)(@.*)$", "$1");
    }
    public final String getLocation() {
        return this.toString.replaceFirst("^.*::", "").replaceFirst("^.*@", "");
    }
    
    public final String toString() {
        return toString;
    }
    
    
    /** Named predicate. **/
    static class Predicate extends Annotated implements java.util.function.Predicate {
        private final java.util.function.Predicate check;
        
        /** Constructors. */
        public Predicate(String name, String location, java.util.function.Predicate check) {
            super("Predicate", name, location);
            this.check = check;
        }
        /** Constructors. */
        public Predicate(String name, java.util.function.Predicate check) {
            super("Predicate", name);
            this.check = check;
        }
        
        @Override
        public boolean test(T t) {
            return check.test(t);
        }
    }
    /** Named predicate. **/
    static class BiPredicate extends Annotated implements java.util.function.BiPredicate {
        private final java.util.function.BiPredicate check;
        
        /** Constructors. */
        public BiPredicate(String name, String location, java.util.function.BiPredicate check) {
            super("BiPredicate", name, location);
            this.check = check;
        }
        /** Constructors. */
        public BiPredicate(String name, java.util.function.BiPredicate check) {
            super("BiPredicate", name);
            this.check = check;
        }
        
        @Override
        public boolean test(T t, U u) {
            return check.test(t, u);
        }
    }
    
    /** Named runnable. **/
    static class Runnable extends Annotated implements java.lang.Runnable {
        private final java.lang.Runnable runnable;
        
        /** Constructors. */
        public Runnable(String name, String location, java.lang.Runnable runnable) {
            super("Runnable", name, location);
            this.runnable = runnable;
        }
        /** Constructors. */
        public Runnable(String name, java.lang.Runnable runnable) {
            super("Runnable", name);
            this.runnable = runnable;
        }
        
        @Override
        public void run() {
            runnable.run();
        }
    }
    
    /** Named supplier */
    static class Supplier extends Annotated implements java.util.function.Supplier {
        private final java.util.function.Supplier supplier;
        
        /** Constructors. */
        public Supplier(String name, String location, java.util.function.Supplier supplier) {
            super("Supplier", name, location);
            this.supplier = supplier;
        }
        /** Constructors. */
        public Supplier(String name, java.util.function.Supplier supplier) {
            super("Supplier", name);
            this.supplier = supplier;
        }
        
        @Override
        public T get() {
            return supplier.get();
        }
        
    }
    
    /** Named consumer. **/
    static class Consumer extends Annotated implements java.util.function.Consumer {
        private final java.util.function.Consumer consumer;
        
        /** Constructors. */
        public Consumer(String name, String location, java.util.function.Consumer consumer) {
            super("Consumer", name, location);
            this.consumer = consumer;
        }
        /** Constructors. */
        public Consumer(String name, java.util.function.Consumer consumer) {
            super("Consumer", name);
            this.consumer = consumer;
        }
        
        @Override
        public void accept(T value) {
            consumer.accept(value);
        }
        
    }
    
    static class Func0 extends Annotated implements functionalj.function.Func0 {
        private final functionalj.function.Func0 func;
        
        /** Constructors. */
        public Func0(String name, String location, functionalj.function.Func0 func) {
            super("F0", name, location);
            this.func = func;
        }
        public Func0(String name, functionalj.function.Func0 func) {
            super("F0", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe() throws Exception {
            return func.applyUnsafe();
        }
    }
    
    static class Func1 extends Annotated implements functionalj.function.Func1 {
        private final functionalj.function.Func1 func;
        
        /** Constructors. */
        public Func1(String name, String location, functionalj.function.Func1 func) {
            super("F1", name, location);
            this.func = func;
        }
        public Func1(String name, functionalj.function.Func1 func) {
            super("F1", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe(INPUT input) throws Exception {
            return func.applyUnsafe(input);
        }
    }
    
    static class Func2 extends Annotated 
            implements functionalj.function.Func2 {
        private final functionalj.function.Func2 func;
        
        /** Constructors. */
        public Func2(String name, String location, functionalj.function.Func2 func) {
            super("F2", name, location);
            this.func = func;
        }
        public Func2(String name, functionalj.function.Func2 func) {
            super("F2", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe(INPUT1 input1, INPUT2 input2) throws Exception {
            return func.applyUnsafe(input1, input2);
        }
    }
    
    static class Func3 extends Annotated 
            implements functionalj.function.Func3 {
        private final functionalj.function.Func3 func;
        
        /** Constructors. */
        public Func3(String name, String location, functionalj.function.Func3 func) {
            super("F3", name, location);
            this.func = func;
        }
        public Func3(String name, functionalj.function.Func3 func) {
            super("F3", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe(INPUT1 input1, INPUT2 input2, INPUT3 input3) throws Exception {
            return func.applyUnsafe(input1, input2, input3);
        }
    }
    
    static class Func4 extends Annotated 
            implements functionalj.function.Func4 {
        private final functionalj.function.Func4 func;
        
        /** Constructors. */
        public Func4(String name, String location, functionalj.function.Func4 func) {
            super("F3", name, location);
            this.func = func;
        }
        public Func4(String name, functionalj.function.Func4 func) {
            super("F3", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4) throws Exception {
            return func.applyUnsafe(input1, input2, input3, input4);
        }
    }
    
    static class Func5 extends Annotated 
            implements functionalj.function.Func5 {
        private final functionalj.function.Func5 func;
        
        /** Constructors. */
        public Func5(String name, String location, functionalj.function.Func5 func) {
            super("F5", name, location);
            this.func = func;
        }
        public Func5(String name, functionalj.function.Func5 func) {
            super("F5", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5) throws Exception {
            return func.applyUnsafe(input1, input2, input3, input4, input5);
        }
    }
    
    static class Func6 extends Annotated 
            implements functionalj.function.Func6 {
        private final functionalj.function.Func6 func;
        
        /** Constructors. */
        public Func6(String name, String location, functionalj.function.Func6 func) {
            super("F6", name, location);
            this.func = func;
        }
        public Func6(String name, functionalj.function.Func6 func) {
            super("F6", name);
            this.func = func;
        }
        
        @Override
        public OUTPUT applyUnsafe(INPUT1 input1, INPUT2 input2, INPUT3 input3, INPUT4 input4, INPUT5 input5, INPUT6 input6) throws Exception {
            return func.applyUnsafe(input1, input2, input3, input4, input5, input6);
        }
    }
    
    static class FuncUnit0 extends Annotated implements functionalj.function.FuncUnit0 {
        private final functionalj.function.FuncUnit0 func;
        
        /** Constructors. */
        public FuncUnit0(String name, String location, functionalj.function.FuncUnit0 func) {
            super("FU0", name, location);
            this.func = func;
        }
        public FuncUnit0(String name, functionalj.function.FuncUnit0 func) {
            super("FU0", name);
            this.func = func;
        }
        
        @Override
        public void runUnsafe() throws Exception {
            func.runUnsafe();
        }
    }
    
    static class FuncUnit1 extends Annotated implements functionalj.function.FuncUnit1 {
        private final functionalj.function.FuncUnit1 func;
        
        /** Constructors. */
        public FuncUnit1(String name, String location, functionalj.function.FuncUnit1 func) {
            super("FU1", name, location);
            this.func = func;
        }
        public FuncUnit1(String name, functionalj.function.FuncUnit1 func) {
            super("FU1", name);
            this.func = func;
        }
        
        @Override
        public void acceptUnsafe(INPUT input) throws Exception {
            func.acceptUnsafe(input);
        }
    }
    
    static class FuncUnit2 extends Annotated 
            implements functionalj.function.FuncUnit2 {
        private final functionalj.function.FuncUnit2 func;
        
        /** Constructors. */
        public FuncUnit2(String name, String location, functionalj.function.FuncUnit2 func) {
            super("FU2", name, location);
            this.func = func;
        }
        public FuncUnit2(String name, functionalj.function.FuncUnit2 func) {
            super("FU2", name);
            this.func = func;
        }
        
        @Override
        public void acceptUnsafe(INPUT1 input1, INPUT2 input2) throws Exception {
            func.acceptUnsafe(input1, input2);
        }
    }
    
    static class FuncUnit3 extends Annotated 
            implements functionalj.function.FuncUnit3 {
        private final functionalj.function.FuncUnit3 func;
        
        /** Constructors. */
        public FuncUnit3(String name, String location, functionalj.function.FuncUnit3 func) {
            super("FU3", name, location);
            this.func = func;
        }
        public FuncUnit3(String name, functionalj.function.FuncUnit3 func) {
            super("FU3", name);
            this.func = func;
        }
        
        @Override
        public void acceptUnsafe(INPUT1 input1, INPUT2 input2, INPUT3 input3) throws Exception {
            func.acceptUnsafe(input1, input2, input3);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy