Hi Alen
thanks for your kind reply! :) Going though the steps:
1) OK, even if sounds reasonable, I'd prefer being able to change the
default behavior - just because I've a different background and I'm
used to work with the converters package included in apache
commons-beanutils
2) I defined a custom boolean converter (better if I show you the
source code[1]) where { "true", "yes", "y", "on", "1" } values are to
convert true values, and { "false", "no", "n", "off", "0" } for false.
As I reported in my previous email, they work only for
java.lang.Boolean[] but not for boolean[], boolean and
java.lang.Boolean - and you can see the behavior running the tests.
Thanks for your help, have a nice weekend,
Simo
[1] http://code.google.com/p/rocoto/source/browse/#svn/trunk/converters
(you can take a look at the project homepage on
http://rocoto.googlecode.com/svn/site/1.0/index.html for more details)
On Fri, Jan 15, 2010 at 8:52 PM, Alen Vrečko <[email protected]> wrote:
> Hi, Simo.
>
> 1) Afaik No. The primitive type converters are designed to convert
> from java primitive type literals to values. I think this is the most
> obvious default. I'd go the same way.
>
> 2) 'true, no' should have 'true, false' or something. I'd check if
> Boolean converter can convert 'no'.
>
> Why not just stick to java type literals? The simplest thing I can
> think of is to write a converter that changes the properties to java
> type literals. E.g. you read '1' but before passing to Guice you
> change it to 'true'. Something along those lines.
>
> Cheers
> Alen
>
> On Jan 14, 9:20 pm, Simone Tripodi <[email protected]> wrote:
>> Hi all guys,
>> I've been developing a set of TypeConverters to convert values read
>> from properties files. The questions I'd ask are:
>>
>> 1) Is i possible to override the default TypeConverter? I yes, how? I
>> need, for example, override the converter for boolean values, since in
>> my con files the 'true' value can be expressed as "true", "yes", "y",
>> "on", "1"; even if I can register my custom converter, when trying to
>> convert to boolean, guice rises the following error:
>>
>> Multiple converters can convert '0'
>>
>> 2) Since I need to manage also arrays of types, where values are CSV,
>> I wrote this abstract converter able to converts arrays; it works for
>> java.lang.Boolean[] but not for boolean[], rising the following error:
>>
>> Type mismatch converting 'true, no' to boolean[] using
>> booleanconver...@481958 which matches only(boolean[])[...] Converter
>> returned [...@659078. while locating boolean[]
>>
>> follows class implementation; what's wrong?
>>
>> Thanks in advance, best regards,
>> Simo
>>
>> abstract class AbstractConverter implements TypeConverter {
>>
>> private static final String DEFAULT_DELIMITER = ",";
>>
>> public final Object convert(String value, TypeLiteral<?> toType) {
>> if (GenericArrayType.class.isInstance(toType.getType())) {
>> StringTokenizer tokenizer = new StringTokenizer(value,
>> DEFAULT_DELIMITER);
>> Class<?> arrayType =
>> MoreTypes.getRawType(((GenericArrayType)
>> toType.getType()).getGenericComponentType());
>> Object array = Array.newInstance(arrayType,
>> tokenizer.countTokens());
>>
>> int i = 0;
>> while (tokenizer.hasMoreTokens()) {
>> String token = tokenizer.nextToken().trim();
>> Array.set(array, i++, this.simpleConvert(token, arrayType));
>> }
>>
>> return array;
>> }
>>
>> return this.simpleConvert(value,
>> MoreTypes.getRawType(toType.getType()));
>> }
>>
>> protected abstract Object simpleConvert(String value, Class<?> toType);
>>
>> }
>>
>> --http://people.apache.org/~simonetripodi/
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
>
>
--
http://people.apache.org/~simonetripodi/
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.