|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.velocity.tools.generic.NumberTool
public class NumberTool
Tool for working with Number
in Velocity templates.
It is useful for accessing and
formatting arbitrary Number
objects. Also
the tool can be used to retrieve NumberFormat
instances
or make conversions to and from various number types.
Example uses: $myNumber -> 13.55 $number.format($myNumber) -> 13.6 $number.currency($myNumber) -> $13.55 $number.integer($myNumber) -> 13 Example toolbox.xml config (if you want to use this with VelocityView): <tool> <key>number</key> <scope>application</scope> <class>org.apache.velocity.tools.generic.NumberTool</class> <parameter name="format" value="#0.0"/> </tool>
This tool is entirely threadsafe, and has no instance members. It may be used in any scope (request, session, or application). As such, the methods are highly interconnected, and overriding key methods provides an easy way to create subclasses that use a non-default format or locale.
Field Summary | |
---|---|
private boolean |
configLocked
|
static java.lang.String |
DEFAULT_FORMAT
The default format to be used when none is specified. |
static java.lang.String |
DEFAULT_FORMAT_KEY
The key used for specifying a default format via toolbox params. |
static java.lang.String |
DEFAULT_LOCALE_KEY
The key used for specifying a default locale via toolbox params. |
private java.lang.String |
format
|
private java.util.Locale |
locale
|
static java.lang.String |
LOCK_CONFIG_KEY
The key used for specifying whether or not to prevent templates from reconfiguring this tool. |
private static int |
STYLE_CURRENCY
|
private static int |
STYLE_INTEGER
|
private static int |
STYLE_NUMBER
|
private static int |
STYLE_PERCENT
|
Constructor Summary | |
---|---|
NumberTool()
|
Method Summary | |
---|---|
void |
configure(java.util.Map params)
Looks for configuration values in the given params. |
protected void |
configure(ValueParser values)
Does the actual configuration. |
java.lang.String |
currency(java.lang.Object obj)
Convenience method equivalent to $number.format("currency", $foo). |
java.lang.String |
format(java.lang.Object obj)
Converts the specified object to a number and formats it according to the pattern or style returned by getFormat() . |
java.lang.String |
format(java.lang.String format,
java.lang.Object obj)
Converts the specified object to a number and returns a formatted string representing that number in the locale returned by getLocale() . |
java.lang.String |
format(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Converts the specified object to a number and returns a formatted string representing that number in the specified Locale . |
java.lang.String |
getFormat()
Return the pattern or style to be used for formatting numbers when none is specified. |
private java.text.NumberFormat |
getIntegerInstance(java.util.Locale locale)
Since we wish to continue supporting Java 1.3, for the present we cannot use Java 1.4's NumberFormat.getIntegerInstance(Locale) method. |
java.util.Locale |
getLocale()
This implementation returns the configured default locale. |
protected java.text.NumberFormat |
getNumberFormat(int numberStyle,
java.util.Locale locale)
Returns a NumberFormat instance for the specified
number style and Locale . |
java.text.NumberFormat |
getNumberFormat(java.lang.String format,
java.util.Locale locale)
Returns a NumberFormat instance for the specified
format and Locale . |
protected int |
getStyleAsInt(java.lang.String style)
Checks a string to see if it matches one of the standard NumberFormat style patterns: NUMBER, CURRENCY, PERCENT, INTEGER, or DEFAULT. |
java.lang.String |
integer(java.lang.Object obj)
Convenience method equivalent to $number.format("integer", $foo). |
java.lang.String |
number(java.lang.Object obj)
Convenience method equivalent to $number.format("number", $foo). |
java.lang.String |
percent(java.lang.Object obj)
Convenience method equivalent to $number.format("percent", $foo). |
protected void |
setFormat(java.lang.String format)
Sets the default format for this instance. |
protected void |
setLocale(java.util.Locale locale)
Sets the default locale for this instance. |
java.lang.Number |
toNumber(java.lang.Object obj)
Converts an object to an instance of Number using the
format returned by getFormat() and the Locale
returned by getLocale() if the object is not already
an instance of Number. |
java.lang.Number |
toNumber(java.lang.String format,
java.lang.Object obj)
Converts an object to an instance of Number using the
specified format and the Locale returned by
getLocale() if the object is not already an instance
of Number. |
java.lang.Number |
toNumber(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Converts an object to an instance of Number using the
specified format and Locale if the object is not already
an instance of Number. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_FORMAT
public static final java.lang.String DEFAULT_FORMAT_KEY
public static final java.lang.String DEFAULT_LOCALE_KEY
public static final java.lang.String LOCK_CONFIG_KEY
private static final int STYLE_NUMBER
private static final int STYLE_CURRENCY
private static final int STYLE_PERCENT
private static final int STYLE_INTEGER
private java.lang.String format
private java.util.Locale locale
private boolean configLocked
Constructor Detail |
---|
public NumberTool()
Method Detail |
---|
public void configure(java.util.Map params)
protected void configure(ValueParser values)
public java.util.Locale getLocale()
Locale
protected void setLocale(java.util.Locale locale)
public java.lang.String getFormat()
protected void setFormat(java.lang.String format)
public java.lang.String format(java.lang.Object obj)
getFormat()
.
obj
- the number object to be formatted
format(String format, Object obj, Locale locale)
public java.lang.String currency(java.lang.Object obj)
public java.lang.String integer(java.lang.Object obj)
public java.lang.String number(java.lang.Object obj)
public java.lang.String percent(java.lang.Object obj)
public java.lang.String format(java.lang.String format, java.lang.Object obj)
getLocale()
.
format
- the formatting instructionsobj
- the number object to be formatted
null
if the parameters are invalidformat(String format, Object obj, Locale locale)
public java.lang.String format(java.lang.String format, java.lang.Object obj, java.util.Locale locale)
Locale
.
format
- the custom or standard pattern to be usedobj
- the number object to be formattedlocale
- the Locale
to be used when formatting
null
if the parameters are invalidpublic java.text.NumberFormat getNumberFormat(java.lang.String format, java.util.Locale locale)
NumberFormat
instance for the specified
format and Locale
. If the format specified is a standard
style pattern, then a number instance
will be returned with the number style set to the
specified style. If it is a custom format, then a customized
NumberFormat
will be returned.
format
- the custom or standard formatting pattern to be usedlocale
- the Locale
to be used
NumberFormat
NumberFormat
protected java.text.NumberFormat getNumberFormat(int numberStyle, java.util.Locale locale)
NumberFormat
instance for the specified
number style and Locale
.
numberStyle
- the number style (number will be ignored if this is
less than zero or the number style is not recognized)locale
- the Locale
to be used
NumberFormat
or null
if an instance cannot be constructed with the given
parametersprivate java.text.NumberFormat getIntegerInstance(java.util.Locale locale)
protected int getStyleAsInt(java.lang.String style)
style
- the string to be checked
NumberFormat
public java.lang.Number toNumber(java.lang.Object obj)
Number
using the
format returned by getFormat()
and the Locale
returned by getLocale()
if the object is not already
an instance of Number.
obj
- the number to convert
Number
or null
if no
conversion is possiblepublic java.lang.Number toNumber(java.lang.String format, java.lang.Object obj)
Number
using the
specified format and the Locale
returned by
getLocale()
if the object is not already an instance
of Number.
format
- - the format the number is inobj
- - the number to convert
Number
or null
if no
conversion is possibletoNumber(String format, Object obj, Locale locale)
public java.lang.Number toNumber(java.lang.String format, java.lang.Object obj, java.util.Locale locale)
Number
using the
specified format and Locale
if the object is not already
an instance of Number.
format
- - the format the number is inobj
- - the number to convertlocale
- - the Locale
Number
or null
if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |