/**
 * 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 <code>IModel</code> of type <code>T</code> to
object of type <code>A</code>
     *
     * @param delegate
     * @return adapter value of <code>delegate</code> model
     */
    protected abstract N getObject(IModel<O> delegate);

    /** {...@inheritdoc} */
    public void setObject(N object)
    {
        setObject(object, delegate);
    }

    /**
     * Translates from object of type <code>A</code> to
<code>IModel</code> of type <code>T</code>
     *
     * @param object
     *            adapted object that needs to be unadopted
     * @param delegate
     *            delegate model whose value should be set to
unadopted version of
     *            <code>object</code>
     */
    protected abstract void setObject(N object, IModel<O> delegate);

    /** {...@inheritdoc} */
    public void detach()
    {
        delegate.detach();
    }


}

-igor

On Wed, Dec 30, 2009 at 5:23 AM, Sam Zilverberg <samzilverb...@gmail.com> wrote:
> 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.
> When you have some object and you need a wrapping model that creates some
> string representation of the object - usually involving more objects  (so
> you can't just use the "toString" or other getter of the object).
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to