Wicket gem: AdapterModel implements IModel

Recently, on the Wicket-User mailing list:

What’s the best practice when using generics with models that take some
object but return another type of object?

examples:
When you have a collection and need to convert it to a list for listview
purposes.

to which mighty Igor replied:

 Java |  copy |? 
/**
 * Simplifies implementing wrapper models that adapt from model object of one type to another
 *
 * @author igor.vaynberg
 *
 * @param < N >
 *            new type
 * @param < O >
 *            old type
 */
public abstract class AdapterModel< N, O > implements IModel< N >
{
    private static final long serialVersionUID = 1L;
 
    /** delegate model */
    private final IModel< O > delegate;
 
    /**
     * Constructor
     *
     * @param delegate
     *            model to be wrapped
     */
    public AdapterModel(IModel< O > delegate)
    {
        this.delegate = delegate;
    }
 
    /** {@inheritDoc} */
    public N getObject()
    {
        return getObject(delegate);
    }
 
    /**
     * Translates from IModel of type T to object of type A
     *
     * @param delegate
     * @return adapter value of delegate model
     */
    protected abstract N getObject(IModel< O > delegate);
 
    /** {@inheritDoc} */
    public void setObject(N object)
    {
        setObject(object, delegate);
    }
 
    /**
     * Translates from object of type A to IModel of type T
     *
     * @param object
     *            adapted object that needs to be unadopted
     * @param delegate
     *            delegate model whose value should be set to unadopted version of
     *            object
     */
    protected abstract void setObject(N object, IModel< O > delegate);
 
    /** {@inheritDoc} */
    public void detach()
    {
        delegate.detach();
    }
}
-igor 

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>