org.ujac.util.text
Class FormatHelper

java.lang.Object
  extended by org.ujac.util.text.FormatHelper
All Implemented Interfaces:
Serializable

public class FormatHelper
extends Object
implements Serializable

Name: FormatHelper
Description: A helper class handling formatting objects.

Author:
lauerc
See Also:
Serialized Form

Field Summary
static String ATTRIBUTE_NAME
          Constant for the format helper attribute name, needed to store the format helper in the session or request.
protected  DateFormat dateFormat
          The date format.
protected  NumberFormat doubleFormat
          The floating point format.
protected  NumberFormat integerFormat
          The integer format.
static String ISO_DATE_PATTERN
          Constant for the ISO 8601 date format pattern.
static String ISO_TIME_PATTERN
          Constant for the ISO 8601 time format pattern.
static String ISO_TIMESTAMP_PATTERN
          Constant for the ISO 8601 timestamp format pattern.
protected  Locale locale
          The locale.
protected  DateFormat timeFormat
          The time format.
protected  DateFormat timestampFormat
          The timestamp format.
 
Constructor Summary
FormatHelper()
          Constructs a FormatHelper instance with no specific attributes.
FormatHelper(Locale locale)
          Constructs a FormatHelper instance with specific attributes.
 
Method Summary
 Object clone()
           
 DateFormat createDateFormat(String formatPattern)
          Creates a date format for the current locale.
static FormatHelper createInstance()
          Creates an FormatHelper instance according to the registered prototype.
static FormatHelper createInstance(Locale locale)
          Creates an FormatHelper instance according to the registered prototype.
static FormatHelper createIsoInstance()
          Creates an FormatHelper instance according to the registered prototype.
 NumberFormat createNumberFormat(String formatPattern)
          Creates a number format for the current locale.
 String formatDate(Date date)
          Formats the given date as String using the current date format.
 String formatDouble(double value)
          Formats the given double as String using the current double format.
 String formatFloat(float value)
          Formats the given float as String using the current double format.
 String formatInt(int value)
          Formats the given long as String using the current integer format.
 String formatLong(long value)
          Formats the given long as String using the current integer format.
 String formatShort(short value)
          Formats the given short as String using the current integer format.
 String formatTime(Date date)
          Formats the given time as String using the current time format.
 String formatTimestamp(Date date)
          Formats the given date as String using the current time format.
 DateFormat getDateFormat()
          Gets the current date format.
 DateFormat getDefaultDateFormat(Locale locale)
          Gets the default date format for the given locale.
 NumberFormat getDefaultDoubleFormat(Locale locale)
          Gets the default double format for the given locale.
 NumberFormat getDefaultIntegerFormat(Locale locale)
          Gets the default integer format for the given locale.
 DateFormat getDefaultTimeFormat(Locale locale)
          Gets the default time format for the given locale.
 DateFormat getDefaultTimestampFormat(Locale locale)
          Gets the default timestamp format for the given locale.
 NumberFormat getDoubleFormat()
          Gets the current double format.
 FormatHelper getInstance(Locale locale)
          Gets an instance of the FormatHelper for the given locale.
 NumberFormat getIntegerFormat()
          Gets the current integer format.
 DateFormat getIsoDateFormat()
          Gets the date format according to the ISO 8601.
 DateFormat getIsoTimeFormat()
          Gets the time format according to the ISO 8601.
 DateFormat getIsoTimestampFormat()
          Gets the timestamp format according to the ISO 8601.
 Locale getLocale()
          Gets the used locale.
 DateFormat getTimeFormat()
          Gets the current time format.
 DateFormat getTimestampFormat()
          Gets the current timestamp format.
 Format lookupFormat(String formatString)
          Looks up the macro definition for the given macro name.
 Date parseDate(String text)
          Parses a date from the given text using the current date format.
 long parseDouble(String text)
          Parses a double from the given text using the current double format.
 long parseFloat(String text)
          Parses a float from the given text using the current double format.
 int parseInt(String text)
          Parses an integer from the given text using the current integer format.
 long parseLong(String text)
          Parses a long from the given text using the current integer format.
 short parseShort(String text)
          Parses a short from the given text using the current integer format.
 Time parseTime(String text)
          Parses a time from the given text using the current time format.
 Timestamp parseTimestamp(String text)
          Parses a time stamp from the given text using the current timestamp format.
protected  void readData(ObjectInputStream s)
          Reads the object's data from the given stream.
 void registerFormat(String formatString, Format formatInstance)
          Registers the given format instance for the also given format string.
 void setDateFormat(DateFormat dateFormat)
          Sets the date format.
 DateFormat setDateFormat(String formatPattern)
          Sets the date format.
 void setDoubleFormat(NumberFormat doubleFormat)
          Sets the double format.
 NumberFormat setDoubleFormat(String formatPattern)
          Sets the double format.
 void setIntegerFormat(NumberFormat integerFormat)
          Sets the integer format.
 NumberFormat setIntegerFormat(String formatPattern)
          Sets the integer format.
static void setPrototype(FormatHelper prototype)
          Defines the format helper prototype.
 void setTimeFormat(DateFormat timeFormat)
          Sets the timeFormat.
 DateFormat setTimeFormat(String formatPattern)
          Sets the time format.
 void setTimestampFormat(DateFormat timestampFormat)
          Sets the timestampFormat.
 DateFormat setTimestampFormat(String formatPattern)
          Sets the timestamp format.
protected  void writeData(ObjectOutputStream s)
          Writes the object's data to the given stream.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTRIBUTE_NAME

public static final String ATTRIBUTE_NAME
Constant for the format helper attribute name, needed to store the format helper in the session or request.

See Also:
Constant Field Values

ISO_DATE_PATTERN

public static final String ISO_DATE_PATTERN
Constant for the ISO 8601 date format pattern.

See Also:
Constant Field Values

ISO_TIME_PATTERN

public static final String ISO_TIME_PATTERN
Constant for the ISO 8601 time format pattern.

See Also:
Constant Field Values

ISO_TIMESTAMP_PATTERN

public static final String ISO_TIMESTAMP_PATTERN
Constant for the ISO 8601 timestamp format pattern.

See Also:
Constant Field Values

locale

protected Locale locale
The locale.


dateFormat

protected DateFormat dateFormat
The date format.


timeFormat

protected DateFormat timeFormat
The time format.


timestampFormat

protected DateFormat timestampFormat
The timestamp format.


integerFormat

protected NumberFormat integerFormat
The integer format.


doubleFormat

protected NumberFormat doubleFormat
The floating point format.

Constructor Detail

FormatHelper

public FormatHelper()
Constructs a FormatHelper instance with no specific attributes.


FormatHelper

public FormatHelper(Locale locale)
Constructs a FormatHelper instance with specific attributes.

Parameters:
locale - The initial locale.
Method Detail

createInstance

public static FormatHelper createInstance()
Creates an FormatHelper instance according to the registered prototype.

Returns:
The created FormatHelper instance.

createIsoInstance

public static FormatHelper createIsoInstance()
Creates an FormatHelper instance according to the registered prototype.

Returns:
The created FormatHelper instance.

createInstance

public static FormatHelper createInstance(Locale locale)
Creates an FormatHelper instance according to the registered prototype.

Parameters:
locale - The locale to use.
Returns:
The created FormatHelper instance.

setPrototype

public static void setPrototype(FormatHelper prototype)
Defines the format helper prototype.

Parameters:
prototype - The prototype to define.

getInstance

public FormatHelper getInstance(Locale locale)
Gets an instance of the FormatHelper for the given locale.

Parameters:
locale - The locale to use.
Returns:
The created format helper instance.

getLocale

public Locale getLocale()
Gets the used locale.

Returns:
The locale, used for output formats.

getIntegerFormat

public NumberFormat getIntegerFormat()
Gets the current integer format.

Returns:
The current integer format.

setIntegerFormat

public void setIntegerFormat(NumberFormat integerFormat)
Sets the integer format.

Parameters:
integerFormat - The integerFormat to set

setIntegerFormat

public NumberFormat setIntegerFormat(String formatPattern)
Sets the integer format.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

getDoubleFormat

public NumberFormat getDoubleFormat()
Gets the current double format.

Returns:
The current double format.

setDoubleFormat

public void setDoubleFormat(NumberFormat doubleFormat)
Sets the double format.

Parameters:
doubleFormat - The doubleFormat to set

setDoubleFormat

public NumberFormat setDoubleFormat(String formatPattern)
Sets the double format.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

getDateFormat

public DateFormat getDateFormat()
Gets the current date format.

Returns:
The current date format.

setDateFormat

public void setDateFormat(DateFormat dateFormat)
Sets the date format.

Parameters:
dateFormat - The date format to set

setDateFormat

public DateFormat setDateFormat(String formatPattern)
Sets the date format.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

getTimeFormat

public DateFormat getTimeFormat()
Gets the current time format.

Returns:
The current time format.

setTimeFormat

public void setTimeFormat(DateFormat timeFormat)
Sets the timeFormat.

Parameters:
timeFormat - The timeFormat to set

setTimeFormat

public DateFormat setTimeFormat(String formatPattern)
Sets the time format.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

getTimestampFormat

public DateFormat getTimestampFormat()
Gets the current timestamp format.

Returns:
The current timestamp format.

setTimestampFormat

public void setTimestampFormat(DateFormat timestampFormat)
Sets the timestampFormat.

Parameters:
timestampFormat - The timestampFormat to set

setTimestampFormat

public DateFormat setTimestampFormat(String formatPattern)
Sets the timestamp format.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

createNumberFormat

public NumberFormat createNumberFormat(String formatPattern)
Creates a number format for the current locale.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

createDateFormat

public DateFormat createDateFormat(String formatPattern)
Creates a date format for the current locale.

Parameters:
formatPattern - The format pattern to use.
Returns:
The created format.

getDefaultIntegerFormat

public NumberFormat getDefaultIntegerFormat(Locale locale)
Gets the default integer format for the given locale.

Parameters:
locale - The locale to use.
Returns:
The default integer format for the given locale.

getDefaultDoubleFormat

public NumberFormat getDefaultDoubleFormat(Locale locale)
Gets the default double format for the given locale.

Parameters:
locale - The locale to use.
Returns:
The default double format for the given locale.

getDefaultDateFormat

public DateFormat getDefaultDateFormat(Locale locale)
Gets the default date format for the given locale.

Parameters:
locale - The locale to use.
Returns:
The default date format for the given locale.

getDefaultTimeFormat

public DateFormat getDefaultTimeFormat(Locale locale)
Gets the default time format for the given locale.

Parameters:
locale - The locale to use.
Returns:
The default time format for the given locale.

getDefaultTimestampFormat

public DateFormat getDefaultTimestampFormat(Locale locale)
Gets the default timestamp format for the given locale.

Parameters:
locale - The locale to use.
Returns:
The default timestamp format for the given locale.

getIsoDateFormat

public DateFormat getIsoDateFormat()
Gets the date format according to the ISO 8601.

Returns:
The ISO 8601 date format.

getIsoTimeFormat

public DateFormat getIsoTimeFormat()
Gets the time format according to the ISO 8601.

Returns:
The ISO 8601 time format.

getIsoTimestampFormat

public DateFormat getIsoTimestampFormat()
Gets the timestamp format according to the ISO 8601.

Returns:
The ISO 8601 time stamp format.

registerFormat

public void registerFormat(String formatString,
                           Format formatInstance)
Registers the given format instance for the also given format string.

Parameters:
formatString - The format string.
formatInstance - The format instance.

lookupFormat

public Format lookupFormat(String formatString)
Looks up the macro definition for the given macro name.

Parameters:
formatString - The format string.
Returns:
The according format instance or null.

parseInt

public int parseInt(String text)
             throws ParseException
Parses an integer from the given text using the current integer format.

Parameters:
text - The text to parse.
Returns:
The parsed int.
Throws:
ParseException - In case the number parsing failed.
See Also:
getIntegerFormat()

formatInt

public String formatInt(int value)
Formats the given long as String using the current integer format.

Parameters:
value - The value to format.
Returns:
The formatted number.
See Also:
getIntegerFormat()

parseLong

public long parseLong(String text)
               throws ParseException
Parses a long from the given text using the current integer format.

Parameters:
text - The text to parse.
Returns:
The parsed long.
Throws:
ParseException - In case the number parsing failed.
See Also:
getIntegerFormat()

formatLong

public String formatLong(long value)
Formats the given long as String using the current integer format.

Parameters:
value - The value to format.
Returns:
The formatted number.
See Also:
getIntegerFormat()

parseShort

public short parseShort(String text)
                 throws ParseException
Parses a short from the given text using the current integer format.

Parameters:
text - The text to parse.
Returns:
The parsed short.
Throws:
ParseException - In case the number parsing failed.
See Also:
getIntegerFormat()

formatShort

public String formatShort(short value)
Formats the given short as String using the current integer format.

Parameters:
value - The value to format.
Returns:
The formatted number.
See Also:
getIntegerFormat()

parseDouble

public long parseDouble(String text)
                 throws ParseException
Parses a double from the given text using the current double format.

Parameters:
text - The text to parse.
Returns:
The parsed double.
Throws:
ParseException - In case the number parsing failed.
See Also:
getDoubleFormat()

formatDouble

public String formatDouble(double value)
Formats the given double as String using the current double format.

Parameters:
value - The value to format.
Returns:
The formatted number.
See Also:
getDoubleFormat()

parseFloat

public long parseFloat(String text)
                throws ParseException
Parses a float from the given text using the current double format.

Parameters:
text - The text to parse.
Returns:
The parsed float.
Throws:
ParseException - In case the number parsing failed.
See Also:
getDoubleFormat()

formatFloat

public String formatFloat(float value)
Formats the given float as String using the current double format.

Parameters:
value - The value to format.
Returns:
The formatted number.
See Also:
getDoubleFormat()

parseDate

public Date parseDate(String text)
               throws ParseException
Parses a date from the given text using the current date format.

Parameters:
text - The text to parse.
Returns:
The parsed text.
Throws:
ParseException - In case the date parsing failed.
See Also:
getDateFormat()

formatDate

public String formatDate(Date date)
Formats the given date as String using the current date format.

Parameters:
date - The date to format.
Returns:
The formatted date.

parseTime

public Time parseTime(String text)
               throws ParseException
Parses a time from the given text using the current time format.

Parameters:
text - The text to parse.
Returns:
The parsed time.
Throws:
ParseException - In case the time parsing failed.
See Also:
getTimeFormat()

formatTime

public String formatTime(Date date)
Formats the given time as String using the current time format.

Parameters:
date - The date to format.
Returns:
The formatted time.

parseTimestamp

public Timestamp parseTimestamp(String text)
                         throws ParseException
Parses a time stamp from the given text using the current timestamp format.

Parameters:
text - The text to parse.
Returns:
The parsed time stamp.
Throws:
ParseException - In case the time parsing failed.
See Also:
getTimestampFormat()

formatTimestamp

public String formatTimestamp(Date date)
Formats the given date as String using the current time format.

Parameters:
date - The date to format.
Returns:
The formatted time.

clone

public Object clone()
Overrides:
clone in class Object
See Also:
Object.clone()

writeData

protected void writeData(ObjectOutputStream s)
                  throws IOException
Writes the object's data to the given stream.

Parameters:
s - The stream to write to
Throws:
IOException - In case the data output failed.

readData

protected void readData(ObjectInputStream s)
                 throws IOException,
                        ClassNotFoundException
Reads the object's data from the given stream.

Parameters:
s - The stream to read from.
Throws:
IOException - In case the data reading failed.
ClassNotFoundException - In case the class to deserialize could not be found


Copyright © 2003-2004 UJAC.org. All Rights Reserved.