org.codehaus.waffle.bind.converters
Class ListValueConverter

java.lang.Object
  extended by org.codehaus.waffle.bind.converters.AbstractValueConverter
      extended by org.codehaus.waffle.bind.converters.ListValueConverter
All Implemented Interfaces:
ValueConverter

public class ListValueConverter
extends AbstractValueConverter

ValueConverter that converts a CSV value to a List. A null or empty value (once trimmed) will be returned as an empty list (behaviour which can be overridden via the convertMissingValue() method). The message keys and default values used are:

The patterns are also optionally injectable via Properties in the constructor and take precedence over the ones configured in the messages resources.

NOTE: the converter will first check if the values match the configured number regex pattern and only if it does will it attempt to parse them (using the NumberFormat instance provided, which defaults to NumberFormat.getInstance()) and if not successful returns the string values. The reason for the presence of the preliminary number pattern matching is to disable the attempt of number parsing altogether for some string values that may start with number and may be erronously parsed as numbers.

Author:
Mauro Talevi

Field Summary
static java.lang.String BIND_ERROR_LIST_KEY
           
static java.lang.String DEFAULT_LIST_MESSAGE
           
static java.lang.String DEFAULT_NUMBER_PATTERN
           
static java.lang.String NUMBER_PATTERN_KEY
           
 
Fields inherited from class org.codehaus.waffle.bind.converters.AbstractValueConverter
messageResources
 
Constructor Summary
ListValueConverter(MessageResources messageResources)
           
ListValueConverter(MessageResources messageResources, java.text.NumberFormat numberFormat, java.util.Properties patterns)
           
 
Method Summary
 boolean accept(java.lang.Class<?> type)
          Determines if converter is compatible with the given type
protected  boolean areNumbers(java.util.List<java.lang.String> values)
           
 void changePatterns(java.util.Properties patterns)
           
protected  java.lang.Object convertMissingValue(java.lang.String key, java.lang.String defaultMessage, java.lang.Object... parameters)
          Handles the case of a missing value.
<T> T
convertValue(java.lang.String propertyName, java.lang.String value, java.lang.Class<T> toType)
          Converts a String value to an Object of a given type
 java.util.Properties getPatterns()
           
protected  java.util.List<java.lang.Number> toNumbers(java.util.List<java.lang.String> values)
           
 
Methods inherited from class org.codehaus.waffle.bind.converters.AbstractValueConverter
matches, messageFor, missingValue, newBindException, patternFor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIND_ERROR_LIST_KEY

public static final java.lang.String BIND_ERROR_LIST_KEY
See Also:
Constant Field Values

DEFAULT_LIST_MESSAGE

public static final java.lang.String DEFAULT_LIST_MESSAGE
See Also:
Constant Field Values

NUMBER_PATTERN_KEY

public static final java.lang.String NUMBER_PATTERN_KEY
See Also:
Constant Field Values

DEFAULT_NUMBER_PATTERN

public static final java.lang.String DEFAULT_NUMBER_PATTERN
See Also:
Constant Field Values
Constructor Detail

ListValueConverter

public ListValueConverter(MessageResources messageResources)

ListValueConverter

public ListValueConverter(MessageResources messageResources,
                          java.text.NumberFormat numberFormat,
                          java.util.Properties patterns)
Method Detail

accept

public boolean accept(java.lang.Class<?> type)
Description copied from interface: ValueConverter
Determines if converter is compatible with the given type

Parameters:
type - the type of the field a value is to be bound to
Returns:
A boolean true is type is compatible

convertValue

public <T> T convertValue(java.lang.String propertyName,
                          java.lang.String value,
                          java.lang.Class<T> toType)
Description copied from interface: ValueConverter
Converts a String value to an Object of a given type

Parameters:
propertyName - the associated property name, which can be null, also needed to present customized error messages.
value - the String value
toType - the Object type
Returns:
The converted Object

getPatterns

public java.util.Properties getPatterns()

changePatterns

public void changePatterns(java.util.Properties patterns)

convertMissingValue

protected java.lang.Object convertMissingValue(java.lang.String key,
                                               java.lang.String defaultMessage,
                                               java.lang.Object... parameters)
Description copied from class: AbstractValueConverter
Handles the case of a missing value. By default it return a null converted value, but can be overridden to throw a BindException

Overrides:
convertMissingValue in class AbstractValueConverter
Parameters:
key - the error message key
defaultMessage - the default message if key is not found
parameters - the message formatting parameters
Returns:
A converted object when value is missing, null by default.

areNumbers

protected boolean areNumbers(java.util.List<java.lang.String> values)

toNumbers

protected java.util.List<java.lang.Number> toNumbers(java.util.List<java.lang.String> values)
                                              throws java.text.ParseException
Throws:
java.text.ParseException


Copyright © 2008. All Rights Reserved.