My application stores money amounts as an integer number of cents. I've
written a converter to handle this as follows:

public class MoneyConverter extends SimpleConverterAdapter {
    public Object toObject(String value) {
        try {
            return Math.round(Float.parseFloat(value) * 100);
        } catch (NumberFormatException e) {
            throw new ConversionException("").setSourceValue(value);
        }
    }
    public String toString(Object value) {
        float amount = ((Number) value).floatValue() / 100;
        return String.format("%.2f", amount);
    }
}

When attached to a TextField, this converter is called twice, once
before calling the field validators, and again when saving to the model.
This second call is failing in this part of SimpleConverterAdapter:

  else if (value != null && (!value.getClass().isAssignableFrom(c)))
  {
    throw new IllegalArgumentException("unable to convert " +
                  value + " to type " + c);
  }

...because value.getClass() is "Integer" while c is "int", and the two
are not assignable from one another (apparently, Java 5 autoboxing does
not apply here). The error message is an unhelpful "unable to convert 1
to type int".

So it seems that SimpleAdapterConverter cannot be used with
primitive-typed properties. Does this sound right? Will this limitation
exist in 1.3 (I'm using 1.2.6)? Can anyone think of a workaround other
that implementing IConverter directly and checking explicitly for
Integer -> int?

jk


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to