com.taco.text
Class MappedStringToObjectConverter

java.lang.Object
  extended by com.taco.text.AbstractStringToObjectConverter
      extended by com.taco.text.MappedStringToObjectConverter
All Implemented Interfaces:
IStringToObjectConverter, IObjectMapper, java.io.Serializable
Direct Known Subclasses:
StringToBorderLayoutConstraintConverter, StringToCloseOperationConverter, StringToLayerConverter, StringToSelectionModeConverter, StringToSwingConstantConverter

public class MappedStringToObjectConverter
extends AbstractStringToObjectConverter
implements java.io.Serializable

A simple implementation of IStringToObjectConverter that looks up strings to be converted in a map, and returns the mapped value. This is useful when the set of strings to be converted is finite.

See Also:
Serialized Form

Field Summary
protected  java.lang.Object _defaultValue
          The default value returned when the the string is not found in the map.
protected  java.util.Map _map
          The string to object map used for conversion.
protected static java.lang.Object _NO_DEFAULT_VALUE
          A value for _defaultValue indicating that a ParseException should be thrown instead of returning a default value.
 
Constructor Summary
MappedStringToObjectConverter(java.util.Map map)
          Construct a new instance using map as a conversion map between strings and objects.
MappedStringToObjectConverter(java.util.Map map, java.lang.Object defaultValue)
          Construct a new instance using map as a conversion map between strings and objects.
MappedStringToObjectConverter(java.lang.Object[] mappings)
          A convenience constructor that converts mappings into the conversion map using MapUtilities.arrayToHashMap(), wrapping null values.
MappedStringToObjectConverter(java.lang.Object[] mappings, java.lang.Object defaultValue)
          A convenience constructor that converts mappings into the conversion map using MapUtilities.arrayToHashMap(), wrapping null values.
 
Method Summary
 java.lang.Object toObject(java.lang.String s)
          See if the string is mapped to a value.
 
Methods inherited from class com.taco.text.AbstractStringToObjectConverter
map
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_map

protected final java.util.Map _map
The string to object map used for conversion.


_defaultValue

protected final java.lang.Object _defaultValue
The default value returned when the the string is not found in the map. If equal (using ==) to _NO_DEFAULT_VALUE, a ParseException is thrown instead.


_NO_DEFAULT_VALUE

protected static final java.lang.Object _NO_DEFAULT_VALUE
A value for _defaultValue indicating that a ParseException should be thrown instead of returning a default value.

Constructor Detail

MappedStringToObjectConverter

public MappedStringToObjectConverter(java.util.Map map)
Construct a new instance using map as a conversion map between strings and objects. If a string to be converted cannot be found in the conversion map when toObject() is called, throw a ParseException.


MappedStringToObjectConverter

public MappedStringToObjectConverter(java.util.Map map,
                                     java.lang.Object defaultValue)
Construct a new instance using map as a conversion map between strings and objects. If a string to be converted cannot be found in the conversion map, defaultValue will be returned instead by toObject().

Throws:
java.lang.NullPointerException - if map is null

MappedStringToObjectConverter

public MappedStringToObjectConverter(java.lang.Object[] mappings,
                                     java.lang.Object defaultValue)
A convenience constructor that converts mappings into the conversion map using MapUtilities.arrayToHashMap(), wrapping null values. If a string to be converted cannot be found in the conversion map, defaultValue will be returned instead by toObject().

See Also:
MapUtilities.arrayToHashMap(java.lang.Object[])

MappedStringToObjectConverter

public MappedStringToObjectConverter(java.lang.Object[] mappings)
A convenience constructor that converts mappings into the conversion map using MapUtilities.arrayToHashMap(), wrapping null values. If a string to be converted cannot be found in the conversion map when toObject() is called, throw a ParseException.

See Also:
MapUtilities.arrayToHashMap(java.lang.Object[])
Method Detail

toObject

public java.lang.Object toObject(java.lang.String s)
                          throws java.text.ParseException
See if the string is mapped to a value. If so, unwrap the value (it may be MapUtilities.NULL_MAP_VALUE, indicating that the string is mapped to null). If the string is not mapped, return the default value if one was specified during construction. Otherwise, throw a ParseException.

Specified by:
toObject in interface IStringToObjectConverter
Throws:
java.text.ParseException