Created
July 11, 2019 14:59
-
-
Save athlan/20b0560afa0641f73e7ebc2ab4920106 to your computer and use it in GitHub Desktop.
ConcurrentMementoSupplier Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.athlan.common.concurrent; | |
import java.util.function.Supplier; | |
import javax.annotation.concurrent.ThreadSafe; | |
/** | |
* Thread-safe implementation of {@link MementoSupplier}. | |
* | |
* @param <T> | |
*/ | |
@ThreadSafe | |
class ConcurrentMementoSupplier<T> implements MementoSupplier<T> { | |
private final Supplier<T> delegate; | |
private final ThreadLocal<T> lastSuppliedValue = new ThreadLocal<>(); | |
ConcurrentMementoSupplier(final Supplier<T> delegate) { | |
this.delegate = delegate; | |
} | |
@Override | |
public T get() { | |
T value = delegate.get(); | |
lastSuppliedValue.set(value); | |
return value; | |
} | |
@Override | |
public T getLastSuppliedValue() { | |
return lastSuppliedValue.get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment