/** * Register array converters. * * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. * @param defaultArraySize The size of the default array value for array converters * (N.B. This values is ignored if <code>throwException</code> is <code>true</code>). * Specifying a value less than zero causes a <code>null<code> value to be used for * the default. */ private void registerArrays(final boolean throwException, final int defaultArraySize) { // Primitives registerArrayConverter(Boolean.TYPE, new BooleanConverter(), throwException, defaultArraySize); registerArrayConverter(Byte.TYPE, new ByteConverter(), throwException, defaultArraySize); registerArrayConverter(Character.TYPE, new CharacterConverter(), throwException, defaultArraySize); registerArrayConverter(Double.TYPE, new DoubleConverter(), throwException, defaultArraySize); registerArrayConverter(Float.TYPE, new FloatConverter(), throwException, defaultArraySize); registerArrayConverter(Integer.TYPE, new IntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Long.TYPE, new LongConverter(), throwException, defaultArraySize); registerArrayConverter(Short.TYPE, new ShortConverter(), throwException, defaultArraySize); // Standard registerArrayConverter(BigDecimal.class, new BigDecimalConverter(), throwException, defaultArraySize); registerArrayConverter(BigInteger.class, new BigIntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Boolean.class, new BooleanConverter(), throwException, defaultArraySize); registerArrayConverter(Byte.class, new ByteConverter(), throwException, defaultArraySize); registerArrayConverter(Character.class, new CharacterConverter(), throwException, defaultArraySize); registerArrayConverter(Double.class, new DoubleConverter(), throwException, defaultArraySize); registerArrayConverter(Float.class, new FloatConverter(), throwException, defaultArraySize); registerArrayConverter(Integer.class, new IntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Long.class, new LongConverter(), throwException, defaultArraySize); registerArrayConverter(Short.class, new ShortConverter(), throwException, defaultArraySize); registerArrayConverter(String.class, new StringConverter(), throwException, defaultArraySize); // Other registerArrayConverter(Class.class, new ClassConverter(), throwException, defaultArraySize); registerArrayConverter(java.util.Date.class, new DateConverter(), throwException, defaultArraySize); registerArrayConverter(Calendar.class, new DateConverter(), throwException, defaultArraySize); registerArrayConverter(File.class, new FileConverter(), throwException, defaultArraySize); registerArrayConverter(java.sql.Date.class, new SqlDateConverter(), throwException, defaultArraySize); registerArrayConverter(java.sql.Time.class, new SqlTimeConverter(), throwException, defaultArraySize); registerArrayConverter(Timestamp.class, new SqlTimestampConverter(), throwException, defaultArraySize); registerArrayConverter(URL.class, new URLConverter(), throwException, defaultArraySize); }
/** * Test read properties to alias bean1. */ @Test public void testToAliasBean1(){ ArrayConverter arrayConverter = new ArrayConverter(String[].class, new StringConverter(), 2); char[] allowedChars = { ':' }; arrayConverter.setAllowedChars(allowedChars); ConvertUtils.register(arrayConverter, String[].class); DangaMemCachedConfig dangaMemCachedConfig = toAliasBean(getResourceBundle("messages.memcached"), DangaMemCachedConfig.class); assertThat( dangaMemCachedConfig, allOf(// hasProperty("serverList", arrayContaining("172.20.3-1.23:11211", "172.20.31.22:11211")), hasProperty("poolName", is("sidsock2")), hasProperty("expireTime", is(180)), hasProperty("weight", arrayContaining(2)), hasProperty("initConnection", is(10)), hasProperty("minConnection", is(5)), hasProperty("maxConnection", is(250)), hasProperty("maintSleep", is(30)), hasProperty("nagle", is(false)), hasProperty("socketTo", is(3000)), hasProperty("aliveCheck", is(false)) // )); }
/** * Register the converters for standard types. * </p> * This method registers the following converters: * <ul> * <li><code>BigDecimal.class</code> - {@link BigDecimalConverter}</li> * <li><code>BigInteger.class</code> - {@link BigIntegerConverter}</li> * <li><code>Boolean.class</code> - {@link BooleanConverter}</li> * <li><code>Byte.class</code> - {@link ByteConverter}</li> * <li><code>Character.class</code> - {@link CharacterConverter}</li> * <li><code>Double.class</code> - {@link DoubleConverter}</li> * <li><code>Float.class</code> - {@link FloatConverter}</li> * <li><code>Integer.class</code> - {@link IntegerConverter}</li> * <li><code>Long.class</code> - {@link LongConverter}</li> * <li><code>Short.class</code> - {@link ShortConverter}</li> * <li><code>String.class</code> - {@link StringConverter}</li> * </ul> * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. * @param defaultNull <code>true</code>if the <i>standard</i> converters * (see {@link ConvertUtilsBean#registerStandard(boolean, boolean)}) * should use a default value of <code>null</code>, otherwise <code>false</code>. * N.B. This values is ignored if <code>throwException</code> is <code>true</code> */ private void registerStandard(final boolean throwException, final boolean defaultNull) { final Number defaultNumber = defaultNull ? null : ZERO; final BigDecimal bigDecDeflt = defaultNull ? null : new BigDecimal("0.0"); final BigInteger bigIntDeflt = defaultNull ? null : new BigInteger("0"); final Boolean booleanDefault = defaultNull ? null : Boolean.FALSE; final Character charDefault = defaultNull ? null : SPACE; final String stringDefault = defaultNull ? null : ""; register(BigDecimal.class, throwException ? new BigDecimalConverter() : new BigDecimalConverter(bigDecDeflt)); register(BigInteger.class, throwException ? new BigIntegerConverter() : new BigIntegerConverter(bigIntDeflt)); register(Boolean.class, throwException ? new BooleanConverter() : new BooleanConverter(booleanDefault)); register(Byte.class, throwException ? new ByteConverter() : new ByteConverter(defaultNumber)); register(Character.class, throwException ? new CharacterConverter() : new CharacterConverter(charDefault)); register(Double.class, throwException ? new DoubleConverter() : new DoubleConverter(defaultNumber)); register(Float.class, throwException ? new FloatConverter() : new FloatConverter(defaultNumber)); register(Integer.class, throwException ? new IntegerConverter() : new IntegerConverter(defaultNumber)); register(Long.class, throwException ? new LongConverter() : new LongConverter(defaultNumber)); register(Short.class, throwException ? new ShortConverter() : new ShortConverter(defaultNumber)); register(String.class, throwException ? new StringConverter() : new StringConverter(stringDefault)); }
/** * Returns a String representation of the field or a default value in case if the field cannot * be converted to a String. * @param def the default value to be returned if an error occurs converting the field * @return a String representation of the field */ public String getString(String def) { return new StringConverter(def).convert(String.class, value); }