org.norther.tammi.core.converter
Class Formatter

java.lang.Object
  extended by org.norther.tammi.core.converter.Formatter
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
FormatTool

public class Formatter
extends Object
implements Serializable

A localized formatter.

Note that the formatter is not thread-safe.

Author:
Ilkka Priha
See Also:
Serialized Form

Constructor Summary
Formatter()
          Constructs a new formatter.
Formatter(MBeanServer server)
          Constructs a new formatter for a specific server.
 
Method Summary
protected  void clearFormats()
          Clears cached formats.
 String format(String key, List arguments)
          Formats a string resource from the default bundle.
 String format(String name, String key, List arguments)
          Formats a string resource from the named bundle.
 String formatCurrency(Number number)
          Formats currency.
 String formatDate()
          Formats the current date.
 String formatDate(Object date)
          Formats the specified date.
 String formatDate(Object date, String style)
          Formats the specified date with style.
 String formatDate(Object date, String style, String timezone)
          Formats the specified with style and time zone.
 String formatDate(String style)
          Formats the current date with style.
 String formatDate(String style, String timezone)
          Formats the current date with style and time zone.
 String formatDateTime()
          Formats the current date and time.
 String formatDateTime(Object date)
          Formats the specified date and time.
 String formatDateTime(Object date, String dstyle, String tstyle)
          Formats the specified date and time with style.
 String formatDateTime(Object date, String dstyle, String tstyle, String timezone)
          Formats the specified date and time with style and time zone.
 String formatDateTime(String dstyle, String tstyle)
          Formats the current date and time with style.
 String formatDateTime(String dstyle, String tstyle, String timezone)
          Formats the current date and time with style and time zone.
 String formatError(Throwable error)
          Formats an error.
 String formatNumber(Number number)
          Formats a number.
 String formatNumber(Number number, String pattern)
          Formats a number with a pattern.
 String formatPercent(Number number)
          Formats percent.
 String formatString(String string, List arguments)
          Formats a string with arguments.
protected  String formatString(String string, Object[] args)
          Formats a string with arguments.
 String formatTime()
          Formats the current time.
 String formatTime(Object time)
          Formats the specified time.
 String formatTime(Object time, String style)
          Formats the specified time with style.
 String formatTime(Object time, String style, String timezone)
          Formats the specified time with style and time zone.
 String formatTime(String style)
          Formats the current time with style.
 String formatTime(String style, String timezone)
          Formats the current time with style and time zone.
 DateFormat getDateFormat(String pattern)
          Gets a date format.
 Locale getDefaultLocale()
          Gets the default locale.
 Locale getLocale()
          Gets the locale to apply.
protected  MBeanServer getMBeanServer()
          Gets a reference to the MBean server.
 NumberFormat getNumberFormat(String pattern)
          Gets a number format.
 Object getObject(String key)
          Gets a resource from the default bundle.
 Object getObject(String name, String key)
          Gets a resource from the named bundle.
protected  ResourceMap getResourceMap()
          Gets the referent of the resource map MBean.
 String getString(String key)
          Gets a string resource from the default bundle.
 String getString(String name, String key)
          Gets a string resource from the named bundle.
 Date parseDate(String str)
          Parses date.
 Date parseDate(String str, String timezone)
          Parses date with time zone.
 Date parseDateTime(String str)
          Parses date and time.
 Date parseDateTime(String str, String timezone)
          Parses date and time with time zone.
 double parseDouble(String number)
          Parses a double number.
 double parseDouble(String number, double error)
          Parses a double number.
 int parseInt(String number)
          Parses an int number.
 int parseInt(String number, int error)
          Parses an int number.
 long parseLong(String number)
          Parses a long number.
 long parseLong(String number, long error)
          Parses a long number.
 Date parseTime(String str)
          Parses time.
 Date parseTime(String str, String timezone)
          Parses time with time zone.
 String resolve(String key)
          Resolves a string resource from the default bundle.
 String resolve(String key, int casep)
          Resolves a string resource from the default bundle with the specified case.
 String resolve(String key, List arguments)
          Formats a resolved string resource from the default bundle.
 String resolve(String key, List arguments, int casep)
          Formats a resolved string resource from the default bundle with the specified case.
 String resolve(String qualifier, String key)
          Resolves a string resource from the qualified bundle.
 String resolve(String qualifier, String key, int casep)
          Resolves a string resource from the qualified bundle with the specified case.
 String resolve(String qualifier, String key, List arguments)
          Formats a resolved string resource from the qualified bundle.
 void setLocale(Locale locale)
          Sets the locale to apply.
 String toLowerCase(String str)
          Returns a string in localized lowercase.
 String toUpperCase(String str)
          Returns a string in localized uppercase.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Formatter

public Formatter()
Constructs a new formatter.


Formatter

public Formatter(MBeanServer server)
Constructs a new formatter for a specific server. Note that MBean server cannot be serialized.

Parameters:
server - the MBean server.
Method Detail

getObject

public Object getObject(String key)
Gets a resource from the default bundle.

Parameters:
key - the key of the resource.
Returns:
the found resource or the key if not found.

getObject

public Object getObject(String name,
                        String key)
Gets a resource from the named bundle.

Parameters:
name - the name of the resource bundle.
key - the key of the resource.
Returns:
the found resource or the key if not found.

getString

public String getString(String key)
Gets a string resource from the default bundle.

Parameters:
key - the key of the resource.
Returns:
the found string resource or the key if not found.

getString

public String getString(String name,
                        String key)
Gets a string resource from the named bundle.

Parameters:
name - the name of the resource bundle.
key - the key of the resource.
Returns:
the found string resource or the key if not found.

format

public String format(String key,
                     List arguments)
Formats a string resource from the default bundle. String arguments are XML encoded before formatting to prevent interference with markup language.

Parameters:
key - the key of the resource.
arguments - the source objects of the format.
Returns:
the formatted string resource or an empty string.

format

public String format(String name,
                     String key,
                     List arguments)
Formats a string resource from the named bundle. String arguments are XML encoded before formatting to prevent interference with markup language.

Parameters:
name - the name of the resource bundle.
key - the key of the resource.
arguments - the source objects of the format.
Returns:
the formatted string resource or an empty string.

resolve

public String resolve(String key)
Resolves a string resource from the default bundle.

Parameters:
key - the key of the resource.
Returns:
the resolved string resource or the key if not found.

resolve

public String resolve(String qualifier,
                      String key)
Resolves a string resource from the qualified bundle.

Parameters:
qualifier - the qualifier of the resource bundle.
key - the key of the resource.
Returns:
the resolved string resource or the key if not found.

resolve

public String resolve(String key,
                      List arguments)
Formats a resolved string resource from the default bundle. String arguments are XML encoded before formatting to prevent interference with markup language.

Parameters:
key - the key of the resource.
arguments - the source objects of the format.
Returns:
the formatted string resource or an empty string.

resolve

public String resolve(String key,
                      int casep)
Resolves a string resource from the default bundle with the specified case.

Parameters:
key - the key of the resource.
casep - 0 = as is, -1 = lowercase, 1 = uppercase.
Returns:
the resolved string resource or the key if not found.

resolve

public String resolve(String qualifier,
                      String key,
                      int casep)
Resolves a string resource from the qualified bundle with the specified case.

Parameters:
qualifier - the qualifier of the resource bundle.
key - the key of the resource.
casep - 0 = as is, -1 = lowercase, 1 = uppercase.
Returns:
the resolved string resource or the key if not found.

resolve

public String resolve(String key,
                      List arguments,
                      int casep)
Formats a resolved string resource from the default bundle with the specified case. String arguments are XML encoded before formatting to prevent interference with markup language.

Parameters:
key - the key of the resource.
arguments - the source objects of the format.
casep - 0 = as is, -1 = lowercase, 1 = uppercase.
Returns:
the formatted string resource or an empty string.

resolve

public String resolve(String qualifier,
                      String key,
                      List arguments)
Formats a resolved string resource from the qualified bundle. String arguments are XML encoded before formatting to prevent interference with markup language.

Parameters:
qualifier - the qualifier of the resource bundle.
key - the key of the resource.
arguments - the source objects of the format.
Returns:
the formatted string resource or an empty string.

formatString

public String formatString(String string,
                           List arguments)
Formats a string with arguments.

Parameters:
string - the string to format.
arguments - the source objects of the format.
Returns:
the formatted string.

formatError

public String formatError(Throwable error)
Formats an error. The result is encoded as an XML string.

Parameters:
error - the error to format.
Returns:
the formatted error.

formatDate

public String formatDate()
Formats the current date.

Returns:
the formatted date.

formatDate

public String formatDate(String style)
Formats the current date with style.

Parameters:
style - default, short, medium or long.
Returns:
the formatted date.

formatDate

public String formatDate(String style,
                         String timezone)
Formats the current date with style and time zone.

Parameters:
style - default, short, medium, long or full.
timezone - the timezone.
Returns:
the formatted date.

formatDate

public String formatDate(Object date)
Formats the specified date.

Parameters:
date - the date or milliseconds.
Returns:
the formatted date.

formatDate

public String formatDate(Object date,
                         String style)
Formats the specified date with style.

Parameters:
date - the date or milliseconds.
style - default, short, medium, long or full.
Returns:
the formatted date.

formatDate

public String formatDate(Object date,
                         String style,
                         String timezone)
Formats the specified with style and time zone.

Parameters:
date - the date or milliseconds.
style - default, short, medium, long or full.
timezone - the timezone.
Returns:
the formatted date.

formatTime

public String formatTime()
Formats the current time.

Returns:
the formatted time.

formatTime

public String formatTime(String style)
Formats the current time with style.

Parameters:
style - default, short, medium, long or full.
Returns:
the formatted time.

formatTime

public String formatTime(String style,
                         String timezone)
Formats the current time with style and time zone.

Parameters:
style - default, short, medium, long or full.
timezone - the timezone.
Returns:
the formatted time.

formatTime

public String formatTime(Object time)
Formats the specified time.

Parameters:
time - the time or milliseconds.
Returns:
the formatted time.

formatTime

public String formatTime(Object time,
                         String style)
Formats the specified time with style.

Parameters:
time - the time or milliseconds.
style - default, short, medium, long or full.
Returns:
the formatted time.

formatTime

public String formatTime(Object time,
                         String style,
                         String timezone)
Formats the specified time with style and time zone.

Parameters:
time - the time or milliseconds.
style - default, short, medium, long or full.
timezone - the timezone.
Returns:
the formatted time.

formatDateTime

public String formatDateTime()
Formats the current date and time.

Returns:
the formatted date and time.

formatDateTime

public String formatDateTime(String dstyle,
                             String tstyle)
Formats the current date and time with style.

Parameters:
dstyle - default, short, medium, long or full for date.
tstyle - default, short, medium, long or full for time.
Returns:
the formatted date and time.

formatDateTime

public String formatDateTime(String dstyle,
                             String tstyle,
                             String timezone)
Formats the current date and time with style and time zone.

Parameters:
dstyle - default, short, medium, long or full for date.
tstyle - default, short, medium, long or full for time.
timezone - the timezone.
Returns:
the formatted date and time.

formatDateTime

public String formatDateTime(Object date)
Formats the specified date and time.

Parameters:
date - the date and time or milliseconds.
Returns:
the formatted date and time.

formatDateTime

public String formatDateTime(Object date,
                             String dstyle,
                             String tstyle)
Formats the specified date and time with style.

Parameters:
date - the date and time or milliseconds.
dstyle - default, short, medium, long or full for date.
tstyle - default, short, medium, long or full for time.
Returns:
the formatted date and time.

formatDateTime

public String formatDateTime(Object date,
                             String dstyle,
                             String tstyle,
                             String timezone)
Formats the specified date and time with style and time zone.

Parameters:
date - the date and time or milliseconds.
dstyle - default, short, medium, long or full for date.
tstyle - default, short, medium, long or full for time.
timezone - the timezone.
Returns:
the formatted date and time.

formatNumber

public String formatNumber(Number number)
Formats a number.

Parameters:
number - the number to format.
Returns:
the formatted number.

formatNumber

public String formatNumber(Number number,
                           String pattern)
Formats a number with a pattern.

Parameters:
number - the number to format.
pattern - the format pattern.
Returns:
the formatted number.

formatCurrency

public String formatCurrency(Number number)
Formats currency.

Parameters:
number - the amount to format.
Returns:
the formatted currency.

formatPercent

public String formatPercent(Number number)
Formats percent.

Parameters:
number - the percent to format.
Returns:
the formatted currency.

parseDate

public Date parseDate(String str)
Parses date.

Parameters:
str - the date string.
Returns:
the parsed date or a zero date if parsing fails.

parseDate

public Date parseDate(String str,
                      String timezone)
Parses date with time zone.

Parameters:
str - the date string.
timezone - the timezone.
Returns:
the parsed date or a zero date if parsing fails.

parseTime

public Date parseTime(String str)
Parses time.

Parameters:
str - the time string.
Returns:
the parsed time or a zero date if parsing fails.

parseTime

public Date parseTime(String str,
                      String timezone)
Parses time with time zone.

Parameters:
str - the time string.
timezone - the timezone.
Returns:
the parsed time or a zero date if parsing fails.

parseDateTime

public Date parseDateTime(String str)
Parses date and time.

Parameters:
str - the date string.
Returns:
the parsed date or a zero date if parsing fails.

parseDateTime

public Date parseDateTime(String str,
                          String timezone)
Parses date and time with time zone.

Parameters:
str - the date string.
timezone - the timezone.
Returns:
the parsed date or a zero date if parsing fails.

parseInt

public int parseInt(String number)
Parses an int number.

Parameters:
number - the number string to parse.
Returns:
the parsed number as a long or -999999 if parsing fails.

parseInt

public int parseInt(String number,
                    int error)
Parses an int number.

Parameters:
number - the number string to parse.
error - the value returned for parsing errors.
Returns:
the parsed number as a long or error if parsing fails.

parseLong

public long parseLong(String number)
Parses a long number.

Parameters:
number - the number string to parse.
Returns:
the parsed number as a long or -999999999 if parsing fails.

parseLong

public long parseLong(String number,
                      long error)
Parses a long number.

Parameters:
number - the number string to parse.
error - the value returned for parsing errors.
Returns:
the parsed number as a long or error if parsing fails.

parseDouble

public double parseDouble(String number)
Parses a double number.

Parameters:
number - the number string to parse.
Returns:
the parsed number as a double or -999999999.999999999 if parsing fails.

parseDouble

public double parseDouble(String number,
                          double error)
Parses a double number.

Parameters:
number - the number string to parse.
error - the value returned for parsing errors.
Returns:
the parsed number as a double or error if parsing fails.

getLocale

public Locale getLocale()
Gets the locale to apply.

Returns:
the locale.

setLocale

public void setLocale(Locale locale)
Sets the locale to apply.

Parameters:
locale - the locale.

getDefaultLocale

public Locale getDefaultLocale()
Gets the default locale.

Returns:
the default locale.

getDateFormat

public DateFormat getDateFormat(String pattern)
Gets a date format.

Parameters:
pattern - the pattern.
Returns:
the requested number format.

getNumberFormat

public NumberFormat getNumberFormat(String pattern)
Gets a number format.

Parameters:
pattern - the pattern.
Returns:
the requested number format.

toLowerCase

public String toLowerCase(String str)
Returns a string in localized lowercase.

Parameters:
str - the string.
Returns:
the lowercase string.

toUpperCase

public String toUpperCase(String str)
Returns a string in localized uppercase.

Parameters:
str - the string.
Returns:
the uppercase string.

formatString

protected String formatString(String string,
                              Object[] args)
Formats a string with arguments.

Parameters:
string - the string to format.
args - the source objects of the format.
Returns:
the formatted string.

clearFormats

protected void clearFormats()
Clears cached formats.


getResourceMap

protected ResourceMap getResourceMap()
Gets the referent of the resource map MBean.

Returns:
the referent of the resource map MBean or null.

getMBeanServer

protected MBeanServer getMBeanServer()
Gets a reference to the MBean server.

Returns:
the server or null.


Copyright © 2004 The Norther Organization. All rights reserved.