net.sf.japaki.text
Class EnumFormat

java.lang.Object
  extended by java.text.Format
      extended by net.sf.japaki.text.EnumFormat
All Implemented Interfaces:
Serializable, Cloneable

public class EnumFormat
extends Format

A format for a finite set of objects. Every object is stored with its string.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Field Summary
static Object DUMMY
          A dummy object used by getInstanceFor(String)
 
Constructor Summary
EnumFormat(Map<Object,String> map)
          Constructs a new EnumFormat.
EnumFormat(T[] keys, String[] values)
          Constructs a new EnumFormat.
 
Method Summary
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
           
static
<T extends Enum<T>>
EnumFormat
fromBundle(Class<T> type, ResourceBundle bundle)
          Constructs an enum format from an enum type and a resource bundle.
 Format.Field getField()
          Returns the field used by this format.
static Format getInstanceFor(String value)
          Creates a format that recognizes only a single string and parses it as DUMMY.
 Object parseObject(String source, ParsePosition pos)
           
 void setField(Format.Field field)
          Sets the field used by this format.
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DUMMY

public static final Object DUMMY
A dummy object used by getInstanceFor(String)

Constructor Detail

EnumFormat

public EnumFormat(Map<Object,String> map)
Constructs a new EnumFormat.

Parameters:
map - a map that contains the string representation of every allowed value
Throws:
NullPointerException - if map is null.

EnumFormat

public EnumFormat(T[] keys,
                  String[] values)
Constructs a new EnumFormat.

Parameters:
keys - a list of objects known by this format
values - texts to be associated to the key values
Throws:
NullPointerException - if keys or values is null.
ArrayIndexOutOfBoundsException - if keys and values do not have the same length
Method Detail

getInstanceFor

public static Format getInstanceFor(String value)
Creates a format that recognizes only a single string and parses it as DUMMY.

Parameters:
value - the text linked to the dummy value
Returns:
new format for the string

fromBundle

public static <T extends Enum<T>> EnumFormat fromBundle(Class<T> type,
                                                        ResourceBundle bundle)
Constructs an enum format from an enum type and a resource bundle. If the key of a resource bundle is "null", null will be used as a key.

Parameters:
type - enum type
bundle - a resource bundle
Throws:
NullPointerException - if any parameter is null

getField

public Format.Field getField()
Returns the field used by this format.

Returns:
the field used by this format.

setField

public void setField(Format.Field field)
Sets the field used by this format.

Parameters:
field - the field to be compared in format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) used by this format.

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Specified by:
parseObject in class Format
Throws:
NullPointerException - if source or pos is null.

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Specified by:
format in class Format
Parameters:
obj - the object to format
toAppendTo - where the text is to be appended
pos - the position of the result in the buffer after formatting
Returns:
the string buffer passed in as toAppendTo, with formatted text appended
Throws:
IllegalArgumentException - if the given object is not a member of the enumeration