Package io.github.astrapi69.model
Class LoadableDetachableModel<T>
- java.lang.Object
-
- io.github.astrapi69.model.LoadableDetachableModel<T>
-
- Type Parameters:
T
- The IModel Object type
- All Implemented Interfaces:
Attachable
,IDetachable
,IModel<T>
,java.io.Serializable
public abstract class LoadableDetachableModel<T> extends java.lang.Object implements IModel<T>
IModel 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.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description LoadableDetachableModel()
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 Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
attach()
Attach an object.void
detach()
Detach an object.T
getObject()
Gets the model object.boolean
isAttached()
Checks if the object is attached.protected abstract T
load()
Loads and returns the (temporary) model object.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.java.lang.String
toString()
-
-
-
Constructor Detail
-
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.- Parameters:
object
- retrieved instance of the detachable object
-
LoadableDetachableModel
public LoadableDetachableModel()
-
-
Method Detail
-
attach
public void attach()
Attach an object.- Specified by:
attach
in interfaceAttachable
-
detach
public void detach()
Detach an object.- Specified by:
detach
in interfaceIDetachable
-
getObject
public final T getObject()
Gets the model object.
-
setObject
public void setObject(T object)
Manually loads the model with the specified object. Subsequent calls togetObject()
will returnobject
untildetach()
is called.
-
isAttached
public final boolean isAttached()
Checks if the object is attached.- Returns:
- true, if the object is attached otherwise false
-
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.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-