Author: bleny Date: 2014-01-24 18:14:05 +0100 (Fri, 24 Jan 2014) New Revision: 303 Url: http://nuiton.org/projects/nuiton-web/repository/revisions/303 Log: refs #3010 add some variation points in LocalNumberConverter and some javadocs Modified: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/converters/LocalNumberConverter.java Modified: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/converters/LocalNumberConverter.java =================================================================== --- trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/converters/LocalNumberConverter.java 2014-01-24 13:53:42 UTC (rev 302) +++ trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/converters/LocalNumberConverter.java 2014-01-24 17:14:05 UTC (rev 303) @@ -25,29 +25,38 @@ * <bean type="com.opensymphony.xwork2.conversion.impl.NumberConverter" class="org.nuiton.web.struts2.converters.LocalNumberConverter" scope="singleton"/> * </pre> * + * You may tune this converter to your needs by overriding some methods like {@link #formatNullValue()}, + * {@link #getMinDecimals()} or {@link #getMaxDecimals} + * * @author Sylvain Bavencoff <bavencoff@codelutin.com> * @since 1.15 */ public class LocalNumberConverter extends NumberConverter { - protected int getMinDecimals() { - return 0; - } - - protected int getMaxDecimals() { - return 3; - } - + /** {@link java.text.DecimalFormat} to use for parsing/formatting {@link int}s and {@link java.lang.Integer} instances */ protected DecimalFormat integerFormat; - protected DecimalFormat decimalFormat; + /** {@link java.text.DecimalFormat} to use for parsing/formatting {@link double}s and {@link java.lang.Double} instances */ + protected DecimalFormat doubleFormat; - public LocalNumberConverter() { - integerFormat = new DecimalFormat("###,##0"); - decimalFormat = new DecimalFormat("###,##0." + StringUtils.repeat("0", getMinDecimals()) + StringUtils.repeat("#", getMaxDecimals() - getMinDecimals())); + protected DecimalFormat getIntegerFormat() { + if (integerFormat == null) { + integerFormat = new DecimalFormat("###,##0"); + } + return integerFormat; } + protected DecimalFormat getDoubleFormat() { + if (doubleFormat == null) { + String pattern = "###,##0." + StringUtils.repeat("0", getMinDecimals()) + StringUtils.repeat("#", getMaxDecimals() - getMinDecimals()); + doubleFormat = new DecimalFormat(pattern); + } + return doubleFormat; + } + + @Override public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) { + Object result; if (toType == String.class && (value instanceof Double || value instanceof Integer)) { @@ -75,79 +84,94 @@ if (value == null) { - string = StringUtils.EMPTY; + string = formatNullValue(); } else if (value instanceof Integer) { - string = integerFormat.format(value); + string = getIntegerFormat().format(value); } else { - string = decimalFormat.format(value); + string = getDoubleFormat().format(value); } return string; + } protected Number convertToNumber(String string, Class toType) { Number parsedValue; - if (string == null || string.isEmpty()) { + if (isNullValue(string)) { if (toType.isPrimitive()) { - - if (toType == Double.class || toType == double.class) { - - parsedValue = new Double(0); - + if (toType == double.class) { + parsedValue = 0.; } else { - - parsedValue = new Integer(0); - + parsedValue = 0; } } else { - parsedValue = null; - } } else { - char groupingSeparator = integerFormat.getDecimalFormatSymbols().getGroupingSeparator(); + char groupingSeparator = getIntegerFormat().getDecimalFormatSymbols().getGroupingSeparator(); if (Character.isSpaceChar(groupingSeparator)) { - string = string.replaceAll(" ", groupingSeparator + ""); - } try { if (toType == Double.class || toType == double.class) { - string = string.replace('.', decimalFormat.getDecimalFormatSymbols().getDecimalSeparator()); + string = string.replace('.', getDoubleFormat().getDecimalFormatSymbols().getDecimalSeparator()); - parsedValue = decimalFormat.parse(string).doubleValue(); + parsedValue = getDoubleFormat().parse(string).doubleValue(); } else { - parsedValue = integerFormat.parse(string).intValue(); + parsedValue = getIntegerFormat().parse(string).intValue(); } - } catch (ParseException e ){ + } catch (ParseException e) { + throw new TypeConversionException("unable to parse " + string, e); + } catch (NumberFormatException e) { + throw new TypeConversionException("unable to parse " + string, e); + } - throw new TypeConversionException("strings=" + string, e); + } - } catch (NumberFormatException e ){ + return parsedValue; + } - throw new TypeConversionException("strings=" + string, e); + /** + * Result if formatted object is null. + * + * If you override this method, you probably must override + * {@link #isNullValue(String)} too + */ + protected String formatNullValue() { + return StringUtils.EMPTY; + } - } + /** + * Method to know if a given string should be interpreted as "no value". + */ + protected boolean isNullValue(String string) { + boolean isNullValue = StringUtils.isEmpty(string); + return isNullValue; + } - } + protected int getMinDecimals() { + return 0; + } - return parsedValue; + protected int getMaxDecimals() { + return 3; } + }