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

net.tascalate.concurrent.var.ContextVar Maven / Gradle / Ivy

Go to download

Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s

There is a newer version: 0.9.8
Show newest version
/**
 * Copyright 2015-2019 Valery Silaev (http://vsilaev.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.tascalate.concurrent.var;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public interface ContextVar {
    
    T get();
    
    void set(T value);
    
    default void remove() {
        set(null);
    }
    
    public static  ContextVar define(Supplier reader, Consumer writer) {
        return define(reader, writer, null);
    }
    
    public static  ContextVar define(String name, Supplier reader, Consumer writer) {
        return define(name, reader, writer, null);
    }
    
    public static  ContextVar define(Supplier reader, Consumer writer, Runnable eraser) {
        return define(ContextualPromiseFactory.generateVarName(), reader, writer, eraser);
    }
    
    public static  ContextVar define(String name, Supplier reader, Consumer writer, Runnable eraser) {
        return new ContextVar() {
            @Override
            public T get() { 
                return reader.get();
            }

            @Override
            public void set(T value) {
                writer.accept(value);
            }

            @Override
            public void remove() {
                if (null != eraser) {
                    eraser.run();
                } else {
                    set(null);
                }
            }
            
            @Override
            public String toString() {
                return String.format("[%s]", name);
            }
        };
    }

    public static  ContextVar from(ThreadLocal tl) {
        return new ContextVar() {
            @Override
            public T get() { 
                return tl.get();
            }

            @Override
            public void set(T value) {
                tl.set(value);
            }

            @Override
            public void remove() {
                tl.remove();
            }
            
            @Override
            public String toString() {
                return String.format("[%s]", tl);
            }
        };
    }    
    
    
    public static ContextualPromiseFactory relay(ContextVar contextVar) {
        return new ContextualPromiseFactory(Collections.singletonList(contextVar));
    }
    
    public static ContextualPromiseFactory relay(ThreadLocal threadLocal) {
        return relay(ContextVar.from(threadLocal));
    }

    public static ContextualPromiseFactory relay(ContextVar... contextVars) {
        return new ContextualPromiseFactory(Arrays.asList(contextVars));
    }
    
    public static ContextualPromiseFactory relay(ThreadLocal... threadLocals) {
        return new ContextualPromiseFactory(Arrays.stream(threadLocals).map(ContextVar::from).collect(Collectors.toList()));
    }

    public static ContextualPromiseFactory relay(List> contextVars) {
        return new ContextualPromiseFactory(
            contextVars == null ? Collections.emptyList() : new ArrayList<>(contextVars)
        );
    }
    
    public static ContextualPromiseFactory relayThreadLocals(List> threadLocals) {
        return new ContextualPromiseFactory(
            threadLocals == null ? Collections.emptyList() : threadLocals
                .stream()
                .map(tl -> ContextVar.from((ThreadLocal)tl))
                .collect(Collectors.toList())
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy