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:
java.io.Serializable,IDetachable,IModel<T>,org.apache.wicket.util.io.IClusterable
- Direct Known Subclasses:
StringResourceModel
public abstract class LoadableDetachableModel<T> extends java.lang.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 voiddetach()Detaches model after use.TgetObject()Gets the model object.booleanisAttached()Gets the attached status of this model instanceprotected abstract Tload()Loads and returns the (temporary) model object.static <T> LoadableDetachableModel<T>of(org.danekja.java.util.function.serializable.SerializableSupplier<T> getter)Create aLoadableDetachableModelfor the given supplier.protected voidonAttach()Attaches to the current request.protected voidonDetach()Detaches from the current request.voidsetObject(T object)Manually loads the model with the specified object.java.lang.StringtoString()
-
-
-
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 returnobjectuntildetach()is called.- Parameters:
object- retrieved instance of the detachable object
-
-
Method Detail
-
detach
public void detach()
Description copied from interface:IDetachableDetaches model after use. This is generally used to null out transient references that can be re-attached later.- Specified by:
detachin interfaceIDetachable- Specified by:
detachin interfaceIModel<T>
-
getObject
public final T getObject()
Description copied from interface:IModelGets 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
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
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 returnobjectuntildetach()is called.
-
of
public static <T> LoadableDetachableModel<T> of(org.danekja.java.util.function.serializable.SerializableSupplier<T> getter)
Create aLoadableDetachableModelfor the given supplier.- Type Parameters:
T-- Parameters:
getter- Used for the getObject() method.- Returns:
- the model
-
-