Class 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 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:
    Serialized Form
    • 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 to getObject() will return object until detach() 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 interface IDetachable
        Specified by:
        detach in interface IModel<T>
      • getObject

        public final T getObject()
        Description copied from interface: IModel
        Gets the model object.
        Specified by:
        getObject in interface IModel<T>
        Returns:
        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 to getObject() will return object until detach() is called.
        Specified by:
        setObject in interface IModel<T>
        Parameters:
        object - The object to set into the model
      • of

        public static <T> LoadableDetachableModel<T> of​(org.danekja.java.util.function.serializable.SerializableSupplier<T> getter)
        Create a LoadableDetachableModel for the given supplier.
        Type Parameters:
        T -
        Parameters:
        getter - Used for the getObject() method.
        Returns:
        the model