generics and models that take X as model object but return Y

2009-12-30 Thread Sam Zilverberg
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).


Re: generics and models that take X as model object but return Y

2009-12-30 Thread Igor Vaynberg
/**
 * 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 AdapterModelN, O implements IModelN
{
private static final long serialVersionUID = 1L;

/** delegate model */
private final IModelO delegate;

/**
 * Constructor
 *
 * @param delegate
 *model to be wrapped
 */
public AdapterModel(IModelO delegate)
{
this.delegate = delegate;
}

/** {...@inheritdoc} */
public N getObject()
{
return getObject(delegate);
}

/**
 * Translates from codeIModel/code of type codeT/code to
object of type codeA/code
 *
 * @param delegate
 * @return adapter value of codedelegate/code model
 */
protected abstract N getObject(IModelO delegate);

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

/**
 * Translates from object of type codeA/code to
codeIModel/code of type codeT/code
 *
 * @param object
 *adapted object that needs to be unadopted
 * @param delegate
 *delegate model whose value should be set to
unadopted version of
 *codeobject/code
 */
protected abstract void setObject(N object, IModelO 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