
net.tascalate.concurrent.var.ContextVar Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of net.tascalate.concurrent Show documentation
Show all versions of net.tascalate.concurrent Show documentation
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage
and related extensions to java.util.concurrent.ExecutorService-s
/**
* 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 extends T> reader, Consumer super T> writer) {
return define(reader, writer, null);
}
public static ContextVar define(String name, Supplier extends T> reader, Consumer super T> writer) {
return define(name, reader, writer, null);
}
public static ContextVar define(Supplier extends T> reader, Consumer super T> writer, Runnable eraser) {
return define(ContextualPromiseFactory.generateVarName(), reader, writer, eraser);
}
public static ContextVar define(String name, Supplier extends T> reader, Consumer super T> 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 extends ContextVar>> contextVars) {
return new ContextualPromiseFactory(
contextVars == null ? Collections.emptyList() : new ArrayList<>(contextVars)
);
}
public static ContextualPromiseFactory relayThreadLocals(List extends ThreadLocal>> 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