package com.perforce.workshop.tjuricek.p4oauth.util; import java.util.function.Supplier; /** * Wraps a Supplier with thread local caching. */ public class ThreadMemoizingSupplier<T> implements Supplier<T> { private Supplier<T> wrapped; private T cached = null; private ThreadLocal<T> threadLocal = new ThreadLocal<T>() { @Override protected T initialValue() { cached = wrapped.get(); return cached; } }; public ThreadMemoizingSupplier(Supplier<T> wrapped) { this.wrapped = wrapped; } /** * Returns the cached type. If it's null, then the type was not used in this * thread. */ public T getCached() { return cached; } /** * Will always fire the wrapped supplier and return the value, which is * then cached on the thread. */ @Override public T get() { return threadLocal.get(); } /** * Returns the underlying supplier used to fetch the value. */ public Supplier<T> getWrapped() { return wrapped; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9157 | tjuricek | Added basic "authorization code" workflow test. | ||
#1 | 9095 | tjuricek | Added some basic test data and renamed "workspace" to "workshop" in package name | ||
//guest/tjuricek/p4oauth/src/main/java/com/perforce/workspace/tjuricek/p4oauth/util/ThreadMemoizingSupplier.java | |||||
#1 | 9089 | tjuricek |
Moving some code that worked via some manual validation to the workshop. This just implements a basic code authorization grant scheme. Automated tests are forthcoming, awating some gradle plugin work that should sit outside of this project. |