Class AbstractDetachableCollection<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    Serializable, Iterable<T>, Collection<T>, IDetachable, IClusterable
    Direct Known Subclasses:
    DetachableSet

    public abstract class AbstractDetachableCollection<T>
    extends Object
    implements Collection<T>, IDetachable, Serializable
    A collection that can be converted between attached and detached state via calls to attach() and detach(). Elements are converted between the two states using the specified IDetachCodec. This collection allows the use of real objects, and yet has the convenience of a small session footprint. If the collection is detached, invocation of any method from Collection will cause this collection to be attached. NOTICE: During the conversion to either state N method calls are invoked on the IDetachCodec, one for each element in the collection. This can cause a performance problem in certain situations. Example
     
     class SelectUsersPanel extends Panel {
     
       // codec to transcode user object to and from its detached state
       private static final IDetachCodec<User> userCodec=new IDetachCodec<User> {
         public Serializable detach(User object) { return object.getId(); }
         public User attach(Serializable object) { return UserDao.get().userForId(object); }
       }
     
       // collection used to store selected user objects
       private final DetachableHashSet<User> selected=new DetachableHashSet<User>(userCodec);
        
        protected void onDetach() {
          // this will shrink the size of selected collection before the page is stored in session.
          // the collection will be attached automatically when some method is invoked on it.
          selected.detach(); 
          super.onDetach();
        }
     
        public SelectUsersPanel (String id) {
                    super(id);
                Form form=new Form("form");
          add(form);
                CheckGroup checked=new CheckGroup("checked", new PropertyModel(this, "selected"));
          form.add(checked);
          checked.add(new DataView("users", new UsersDataProvider()) {
            protected void populateItem(Item item) {
              item.add(new Check("user", item.getModel()));
              item.add(new Label("username", new PropertyModel(item.getModel(), "username")));
            }
          }
                ...
        }
     
     
    Author:
    Igor Vaynberg (ivaynberg)
    See Also:
    Serialized Form
    • Constructor Detail

      • AbstractDetachableCollection

        public AbstractDetachableCollection​(IDetachCodec<T> codec)
        Constructor
        Parameters:
        codec - codec that will be used to transcode elements between attached and detached states
    • Method Detail

      • attach

        public final void attach()
        Converts the collection into its attached state. If the collection is already attached this is a noop.
      • detach

        public final void detach()
        Converts the collection into a detached state. If the collection is already detached this is a noop.
        Specified by:
        detach in interface IDetachable
      • getAttachedStore

        protected Collection<T> getAttachedStore()
        Returns collection used to store elements in attached state. If this collection is currently detached it will be attached.
        Returns:
        collection used to store elements in attached state
      • newAttachedStore

        protected abstract Collection<T> newAttachedStore()
        Creates a collection used to store elements in attached state
        Returns:
        collection used to store elements in attached state