Skip to content

Instantly share code, notes, and snippets.

@athlan
Created July 11, 2019 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athlan/20b0560afa0641f73e7ebc2ab4920106 to your computer and use it in GitHub Desktop.
Save athlan/20b0560afa0641f73e7ebc2ab4920106 to your computer and use it in GitHub Desktop.
ConcurrentMementoSupplier Java
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