org.codehaus.waffle.bind.converters
Class StringListMapValueConverter

java.lang.Object
  extended by org.codehaus.waffle.bind.converters.AbstractValueConverter
      extended by org.codehaus.waffle.bind.converters.StringListMapValueConverter
All Implemented Interfaces:
ValueConverter
Direct Known Subclasses:
StringNumberListMapValueConverter

public class StringListMapValueConverter
extends AbstractValueConverter

ValueConverter that converts a text value to a Map of String Lists indexed by Strings.

A value of the form

 a=x\n
 b=x,y\n
 c=x,y,z
 
would be converted to a map
 Map<String, List<String>> map = new HashMap<String, List<String>>();
 map.put("a", asList("x"));
 map.put("b", asList("x", "y"));
 map.put("c", asList("x", "y", "z"));
 
A null or empty value (once trimmed) will be returned as an empty HashMap (behaviour which can be overridden via the convertMissingValue(java.lang.String, java.lang.String, java.lang.Object...) method), while an invalid value will cause a BindException to be thrown. The message keys and default values used are: The separators are also optionally injectable via Properties in the constructor and take precedence over the ones configured in the messages resources.

Author:
Mauro Talevi

Field Summary
static java.lang.String BIND_ERROR_MAP_KEY
           
static java.lang.String DEFAULT_KEY_SEPARATOR
           
static java.lang.String DEFAULT_LIST_SEPARATOR
           
static java.lang.String DEFAULT_MAP_MESSAGE
           
static java.lang.String DEFAULT_NEWLINE_SEPARATOR
           
static java.lang.String KEY_SEPARATOR_KEY
           
static java.lang.String LIST_SEPARATOR_KEY
           
static java.lang.String NEWLINE_SEPARATOR_KEY
           
 
Constructor Summary
StringListMapValueConverter(MessageResources messageResources)
           
StringListMapValueConverter(MessageResources messageResources, java.util.Properties patterns)
           
 
Method Summary
 boolean accept(java.lang.reflect.Type type)
          Accepts parameterized types of type Map>
protected  java.lang.Object convertMissingValue(java.lang.String key, java.lang.String defaultMessage, java.lang.Object... parameters)
          Handles the case of a missing value.
 java.lang.Object convertValue(java.lang.String propertyName, java.lang.String value, java.lang.reflect.Type toType)
          Converts a String value to an Object of a given type
 
Methods inherited from class org.codehaus.waffle.bind.converters.AbstractValueConverter
acceptList, acceptMapOfLists, changePatterns, getPatterns, matches, messageFor, missingValue, newBindException, patternFor, split
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIND_ERROR_MAP_KEY

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

DEFAULT_MAP_MESSAGE

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

NEWLINE_SEPARATOR_KEY

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

KEY_SEPARATOR_KEY

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

LIST_SEPARATOR_KEY

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

DEFAULT_NEWLINE_SEPARATOR

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

DEFAULT_KEY_SEPARATOR

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

DEFAULT_LIST_SEPARATOR

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

StringListMapValueConverter

public StringListMapValueConverter(MessageResources messageResources)

StringListMapValueConverter

public StringListMapValueConverter(MessageResources messageResources,
                                   java.util.Properties patterns)
Method Detail

accept

public boolean accept(java.lang.reflect.Type type)
Accepts parameterized types of type Map>

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

convertValue

public java.lang.Object convertValue(java.lang.String propertyName,
                                     java.lang.String value,
                                     java.lang.reflect.Type 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

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.


Copyright © 2008. All Rights Reserved.