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

fi.evolver.ai.vaadin.entity.ConcurrentValueGetterSetter Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
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);
			}
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy