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

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

import static java.util.Objects.requireNonNull;

import java.util.List;

import functionalj.function.Func0;
import functionalj.list.FuncList;
import lombok.val;

public abstract class Substitution {
    
    public static  Substitution of(Ref ref, D value) {
        return new Substitution.Value(ref, false, value);
    }
    public static  Substitution from(Ref ref, Func0 supplier) {
        return new Substitution.Supplier(ref, false, supplier);
    }
    
    public static final FuncList> getCurrentSubstitutions() {
        return Ref.getSubstitutions();
    }
    
    public static final FuncList> getCurrentSubstitutionOf(Ref ... refs) {
        val refList = FuncList.of((Ref[])refs);
        return getCurrentSubstitutionOf((List>)refList);
    }
    
    public static FuncList> getCurrentSubstitutionOf(List> refs) {
        return Ref
                .getSubstitutions()
                .filter(Substitution::ref, refs::contains);
    }
    
    
    private final Ref ref;
    private final boolean   isThreadLocal;
    
    protected Substitution(Ref ref, boolean isThreadLocal) {
        this.ref = requireNonNull(ref);
        this.isThreadLocal = isThreadLocal;
    }
    
    public final Ref ref() {
        return ref;
    }
    
    public final boolean isThreadLocal() {
        return isThreadLocal;
    }
    
    abstract Substitution newSubstitution(Ref ref, boolean isThreadLocal);
    
    public Substitution withinThisThread() {
        return withinThisThread(true);
    }
    public Substitution withinThisThread(boolean threadLocal) {
        if (threadLocal == isThreadLocal)
            return this;
        return newSubstitution(ref, threadLocal);
    }
    
    public abstract Func0 supplier();
    
    //== Sub classes ==
    
    public static class Value extends Substitution {
        
        private final DATA value;
        
        public Value(Ref ref, boolean isThreadLocal, DATA value) {
            super(ref, isThreadLocal);
            this.value = value;
        }
        
        Substitution newSubstitution(Ref ref, boolean isThreadLocal) {
            return new Substitution.Value(ref, isThreadLocal, value);
        }
        
        public final Func0 supplier() {
            return () -> value;
        }
        
        @Override
        public String toString() {
            return "Value [value=" + value + ", ref()=" + ref() + "]";
        }
        
    }
    
    public static class Supplier extends Substitution {
        
        private final Func0 supplier;
        
        public Supplier(Ref ref, boolean isThreadLocal, Func0 supplier) {
            super(ref, isThreadLocal);
            this.supplier = (supplier != null) ? supplier : ()->null;
        }
        
        Substitution newSubstitution(Ref ref, boolean isThreadLocal) {
            return new Substitution.Supplier(ref, isThreadLocal, supplier);
        }
        
        public final Func0 supplier() {
            return supplier;
        }
        
        @Override
        public String toString() {
            return "Supplier [ref()=" + ref() + "]";
        }
        
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy