public class CurrentInstance extends Object implements Serializable
Please note that the instances are stored using WeakReference
. This
means that the a current instance value may suddenly disappear if there a no
other references to the object.
Currently the framework uses the following instances:
UI
, VaadinService
, VaadinSession
,
VaadinRequest
, VaadinResponse
.
Modifier and Type | Method and Description |
---|---|
static void |
clearAll()
Clears all current instances.
|
static <T> void |
defineFallbackResolver(Class<T> type,
CurrentInstanceFallbackResolver<T> fallbackResolver)
Adds a CurrentInstanceFallbackResolver, that is triggered when
get(Class) can't find a suitable instance for the given type
parameter. |
static <T> T |
get(Class<T> type)
Gets the current instance of a specific type if available.
|
static Map<Class<?>,CurrentInstance> |
getInstances()
Gets the currently set instances so that they can later be restored using
restoreInstances(Map) . |
static void |
restoreInstances(Map<Class<?>,CurrentInstance> old)
Restores the given instances to the given values.
|
static <T> CurrentInstance |
set(Class<T> type,
T instance)
Sets the current instance of the given type.
|
static Map<Class<?>,CurrentInstance> |
setCurrent(UI ui)
Sets current instances for the UI and all related classes.
|
static Map<Class<?>,CurrentInstance> |
setCurrent(VaadinSession session)
Sets current instances for the
VaadinSession and all related
classes. |
public static <T> T get(Class<T> type)
When a current instance of the specific type is not found, the
CurrentInstanceFallbackResolver
registered via
defineFallbackResolver(Class, CurrentInstanceFallbackResolver)
(if any) is invoked.
type
- the class to get an instance ofnull
if there is no current instance.public static <T> void defineFallbackResolver(Class<T> type, CurrentInstanceFallbackResolver<T> fallbackResolver)
get(Class)
can't find a suitable instance for the given type
parameter.type
- the class used on get(Class)
invocations to retrieve
the current instancefallbackResolver
- the resolver, not null
IllegalArgumentException
- if there's already a defined fallback resolver for the given
typepublic static <T> CurrentInstance set(Class<T> type, T instance)
type
- the class that should be used when getting the current
instance backinstance
- the actual instanceThreadLocal
public static void clearAll()
public static void restoreInstances(Map<Class<?>,CurrentInstance> old)
old
- A Class -> CurrentInstance map to set as current instancespublic static Map<Class<?>,CurrentInstance> getInstances()
restoreInstances(Map)
.public static Map<Class<?>,CurrentInstance> setCurrent(UI ui)
restoreInstances(Map)
.ui
- The UIpublic static Map<Class<?>,CurrentInstance> setCurrent(VaadinSession session)
VaadinSession
and all related
classes. The previously defined values can be restored by passing the
returned map to restoreInstances(Map)
.session
- The VaadinSessionCopyright © 2018 Vaadin Ltd. All rights reserved.