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:
GroupsModel
,SessionIdentifiersModel
,StringResourceModel
public abstract class LoadableDetachableModel<T> extends Object implements IModel<T>
Model that makes working with detachable models a breeze. LoadableDetachableModel holds a temporary, transient model object, that is set whengetObject()
is called by calling abstract method 'load', and that will be reset/ set to null ondetach()
. A usage example:LoadableDetachableModel venueListModel = new LoadableDetachableModel() { protected Object load() { return getVenueDao().findVenues(); } };
Though you can override methods
onAttach()
andonDetach()
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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description LoadableDetachableModel()
Default 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
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
detach()
Detaches model after use.T
getObject()
Gets the model object.boolean
isAttached()
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
setObject(T object)
Manually loads the model with the specified object.String
toString()
-
-
-
Constructor Detail
-
LoadableDetachableModel
public LoadableDetachableModel()
Default constructor, constructs the model in detached state with no data associated with the model.
-
LoadableDetachableModel
public LoadableDetachableModel(T object)
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 Detail
-
detach
public void 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
public final T getObject()
Description copied from interface:IModel
Gets the model object.
-
isAttached
public final boolean isAttached()
Gets the attached status of this model instance- Returns:
- true if the model is attached, false otherwise
-
load
protected abstract T load()
Loads and returns the (temporary) model object.- Returns:
- the (temporary) model object
-
onAttach
protected void onAttach()
Attaches to the current request. Implement this method with custom behavior, such as loading the model object.
-
onDetach
protected void onDetach()
Detaches from the current request. Implement this method with custom behavior, such as setting the model object to null.
-
setObject
public void setObject(T object)
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
-
-