Package org.apache.wicket.model
Class LoadableDetachableModel<T>
java.lang.Object
org.apache.wicket.model.LoadableDetachableModel<T>
- Type Parameters:
T
- The Model Object type
- All Implemented Interfaces:
Serializable
,IDetachable
,IModel<T>
,IClusterable
- Direct Known Subclasses:
StringResourceModel
Model that makes working with detachable models a breeze. LoadableDetachableModel holds a
temporary, transient model object, that is set when
getObject()
is called by calling
abstract method 'load', and that will be reset/ set to null on detach()
.
A usage example:
LoadableDetachableModel venueListModel = new LoadableDetachableModel() { protected Object load() { return getVenueDao().findVenues(); } };
Though you can override methods onAttach()
and onDetach()
for additional
attach/ detach behavior, the point of this class is to hide as much of the attaching/ detaching
as possible. So you should rarely need to override those methods, if ever.
- Author:
- Eelco Hillenius, Igor Vaynberg
- See Also:
-
Constructor Summary
ConstructorDescriptionDefault constructor, constructs the model in detached state with no data associated with the model.LoadableDetachableModel
(T object) This constructor is used if you already have the object retrieved and want to wrap it with a detachable model. -
Method Summary
Modifier and TypeMethodDescriptionvoid
detach()
Detaches model after use.final T
Gets the model object.final boolean
Gets the attached status of this model instanceprotected abstract T
load()
Loads and returns the (temporary) model object.static <T> LoadableDetachableModel<T>
of
(org.danekja.java.util.function.serializable.SerializableSupplier<T> getter) Create aLoadableDetachableModel
for the given supplier.protected void
onAttach()
Attaches to the current request.protected void
onDetach()
Detaches from the current request.void
Manually loads the model with the specified object.toString()
-
Constructor Details
-
LoadableDetachableModel
public LoadableDetachableModel()Default constructor, constructs the model in detached state with no data associated with the model. -
LoadableDetachableModel
This constructor is used if you already have the object retrieved and want to wrap it with a detachable model. Constructs the model in attached state. Calls togetObject()
will returnobject
untildetach()
is called.- Parameters:
object
- retrieved instance of the detachable object
-
-
Method Details
-
detach
Description copied from interface:IDetachable
Detaches model after use. This is generally used to null out transient references that can be re-attached later.- Specified by:
detach
in interfaceIDetachable
- Specified by:
detach
in interfaceIModel<T>
-
getObject
Description copied from interface:IModel
Gets the model object. -
isAttached
Gets the attached status of this model instance- Returns:
- true if the model is attached, false otherwise
-
toString
-
load
Loads and returns the (temporary) model object.- Returns:
- the (temporary) model object
-
onAttach
Attaches to the current request. Implement this method with custom behavior, such as loading the model object. -
onDetach
Detaches from the current request. Implement this method with custom behavior, such as setting the model object to null. -
setObject
Manually loads the model with the specified object. Subsequent calls togetObject()
will returnobject
untildetach()
is called. -
of
public static <T> LoadableDetachableModel<T> of(org.danekja.java.util.function.serializable.SerializableSupplier<T> getter) Create aLoadableDetachableModel
for the given supplier.- Type Parameters:
T
-- Parameters:
getter
- Used for the getObject() method.- Returns:
- the model
-