org.codehaus.waffle.bind.converters
Class ListValueConverter
java.lang.Object
org.codehaus.waffle.bind.converters.AbstractValueConverter
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
|
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. |
|
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 java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
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
ListValueConverter
public ListValueConverter(MessageResources messageResources)
ListValueConverter
public ListValueConverter(MessageResources messageResources,
java.text.NumberFormat numberFormat,
java.util.Properties patterns)
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 valuetoType - 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 keydefaultMessage - the default message if key is not foundparameters - 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.