com.vaadin.flow.internal.
Class ReflectionCache<C,T>
- java.lang.Object
-
- com.vaadin.flow.internal.ReflectionCache<C,T>
-
Type Parameters:
C
- the class types that are used as the cache keysT
- the cached value type
public class ReflectionCache<C,T> extends Object
A thread-safe cache for the result of doing some reflection lookup based on a class. Cached values never expire since it's assumed that the there is a finite number of classes for which reflection results are used.
Since:
1.0
Author:
Vaadin Ltd
-
-
Constructor Summary
Constructors Constructor and Description ReflectionCache(SerializableFunction<Class<C>,T> valueProvider)
Creates a new reflection cache with the given value provider.
-
Method Summary
All Methods Modifier and Type Method and Description static Registration
addClearAllAction(Runnable action)
Adds an action that will be run when all reflection caches are cleared.
void
clear()
Removes all mappings from this cache.
static void
clearAll()
Clears all mappings from all reflection caches and related resources.
boolean
contains(Class<? extends C> type)
Checks whether this cache contains an entry for the given type.
T
get(Class<? extends C> type)
Gets a cached value.
-
-
-
Constructor Detail
-
ReflectionCache
public ReflectionCache(SerializableFunction<Class<C>,T> valueProvider)
Creates a new reflection cache with the given value provider. The value provider will be used to produce a new cached value whenever there is a cache miss. It will be run in a context where no
CurrentInstance
is available to prevent accidentally caching values that are computed differently depending on external circumstances.Parameters:
valueProvider
- a function that computes the cached value for a class, notnull
-
-
Method Detail
-
get
public T get(Class<? extends C> type)
Gets a cached value. If this cache does not contain a value for the key, the value is computed using the configured value provider and the cache is populated with the new value.
Parameters:
type
- the type for which to get reflection resultsReturns:
the reflection results
-
contains
public boolean contains(Class<? extends C> type)
Checks whether this cache contains an entry for the given type.
Parameters:
type
- the type to check forReturns:
true
if there is a mapping for the type,false
if there is no mapping
-
clear
public void clear()
Removes all mappings from this cache.
-
addClearAllAction
public static Registration addClearAllAction(Runnable action)
Adds an action that will be run when all reflection caches are cleared.
The actions are held with a weak reference, which typically means that the action will be ignored if the returned registration is garbage collected.
Parameters:
action
- the action to runReturns:
a registration for removing the action
See Also:
-
clearAll
public static void clearAll()
Clears all mappings from all reflection caches and related resources.
-
-