org.wicketstuff.minis.util.collection
Class AbstractDetachableCollection<T>

java.lang.Object
  extended by org.wicketstuff.minis.util.collection.AbstractDetachableCollection<T>
Type Parameters:
T -
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>, org.apache.wicket.IClusterable, org.apache.wicket.model.IDetachable
Direct Known Subclasses:
DetachableSet

public abstract class AbstractDetachableCollection<T>
extends java.lang.Object
implements java.util.Collection<T>, org.apache.wicket.model.IDetachable, java.io.Serializable

A collection that can be coverted 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 convinience 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 Summary
AbstractDetachableCollection(IDetachCodec<T> codec)
          Constructor
 
Method Summary
 void attach()
          Converts the collection into its attached state.
 void detach()
          Converts the collection into a detached state.
 boolean equals(java.lang.Object obj)
           
protected  java.util.Collection<T> getAttachedStore()
          Returns collection used to store elements in attached state.
 int hashCode()
           
protected abstract  java.util.Collection<T> newAttachedStore()
          Creates a collection used to store elements in attached state
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

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 org.apache.wicket.model.IDetachable

getAttachedStore

protected java.util.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 java.util.Collection<T> newAttachedStore()
Creates a collection used to store elements in attached state

Returns:
collection used to store elements in attached state

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Collection<T>
Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()

equals

public boolean equals(java.lang.Object obj)
Specified by:
equals in interface java.util.Collection<T>
Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)


Copyright © 2010. All Rights Reserved.