fi.evolver.ai.vaadin.entity.ConcurrentValueGetterSetter Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.entity;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class ConcurrentValueGetterSetter implements HasValueGetterSetter {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final HasValueGetterSetter valueGetterSetter;
public ConcurrentValueGetterSetter(HasValueGetterSetter valueGetterSetter) {
this.valueGetterSetter = valueGetterSetter;
}
@Override
public final V getValue() {
lock.readLock().lock();
try {
return valueGetterSetter.getValue();
}
finally {
lock.readLock().unlock();
}
}
@Override
public final void setValue(V value) {
lock.writeLock().lock();
try {
valueGetterSetter.setValue(value);
}
finally {
lock.writeLock().unlock();
}
}
public static ConcurrentValueGetterSetter< TVal> of(Supplier getValue, Consumer setValue) {
return new ConcurrentValueGetterSetter(new HasValueGetterSetter() {
@Override
public TVal getValue() {
return getValue.get();
}
@Override
public void setValue(TVal value) {
setValue.accept(value);
}
});
}
}