public class NumberConverter extends Object implements Converter, PartialStateHolder
Converter
implementation for java.lang.Number
values.
The getAsObject()
method parses a String into an
java.lang.Double
or java.lang.Long
, according
to the following algorithm:
null
. Otherwise, trim leading and trailing
whitespace before proceeding.null
.locale
property is not null,
use that Locale
for managing parsing. Otherwise, use the
Locale
from the UIViewRoot
.pattern
has been specified, its syntax must conform
the rules specified by java.text.DecimalFormat
. Such
a pattern will be used to parse, and the type
property
will be ignored.pattern
has not been specified, parsing will be based
on the type
property, which expects a currency, a number,
or a percent. The parse pattern for currencies, numbers, and
percentages is determined by calling the
getCurrencyInstance()
, getNumberInstance()
,
or getPercentInstance()
method of the
java.text.NumberFormat
class, passing in the selected
Locale
.integerOnly
property has been set to true, only
the integer portion of the String will be parsed. See the JavaDocs
for the setParseIntegerOnly()
method of the
java.text.NumberFormat
class for more information.The getAsString()
method expects a value of type
java.lang.Number
(or a subclass), and creates a formatted
String according to the following algorithm:
locale
property is not null,
use that Locale
for managing formatting. Otherwise, use the
Locale
from the FacesContext
.pattern
has been specified, its syntax must conform
the rules specified by java.text.DecimalFormat
. Such
a pattern will be used to format, and the type
property
(along with related formatting options described in the next paragraph)
will be ignored.pattern
has not been specified, formatting will be
based on the type
property, which formats the value as a
currency, a number, or a percent. The format pattern for currencies,
numbers, and percentages is determined by calling the
percentages is determined by calling the
getCurrencyInstance()
, getNumberInstance()
,
or getPercentInstance()
method of the
java.text.NumberFormat
class, passing in the selected
Locale
. In addition, the following properties will be
applied to the format pattern, if specified:
groupingUsed
property is true
, the
setGroupingUsed(true)
method on the corresponding
NumberFormat
instance will be called.maxFractionDigits
,
maxIntegerDigits
, minFractionDigits
,
and minIntegerDigits
properties.currency
, it is also possible
to configure the currency symbol to be used, using either the
currencyCode
or currencySymbol
properties.
If both are set, the value for currencyCode
takes
precedence on a JDK 1.4 (or later) JVM; otherwise, the value
for currencySymbol
takes precedence.Modifier and Type | Field and Description |
---|---|
static String |
CONVERTER_ID
The standard converter id for this converter.
|
static String |
CURRENCY_ID
The message identifier of the
FacesMessage to be created if
the conversion to Number fails. |
static String |
NUMBER_ID
The message identifier of the
FacesMessage to be created if
the conversion to Number fails. |
static String |
PATTERN_ID
The message identifier of the
FacesMessage to be created if
the conversion to Number fails. |
static String |
PERCENT_ID
The message identifier of the
FacesMessage to be created if
the conversion to Number fails. |
static String |
STRING_ID
The message identifier of the
FacesMessage to be created if
the conversion of the Number value to
String fails. |
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
Constructor and Description |
---|
NumberConverter() |
Modifier and Type | Method and Description |
---|---|
void |
clearInitialState()
Reset the PartialStateHolder to a non-delta tracking state. |
Object |
getAsObject(FacesContext context,
UIComponent component,
String value)
Convert the specified string value, which is associated with
the specified
UIComponent , into a model data object that
is appropriate for being stored during the Process Validations
phase of the request processing lifecycle. |
String |
getAsString(FacesContext context,
UIComponent component,
Object value)
Convert the specified model object value, which is associated with
the specified
UIComponent , into a String that is suitable
for being included in the response generated during the
Render Response phase of the request processing
lifeycle. |
String |
getCurrencyCode()
Return the ISO 4217 currency code used by
getAsString()
with a type of currency . |
String |
getCurrencySymbol()
Return the currency symbol used by
getAsString()
with a type of currency . |
Locale |
getLocale()
Return the
Locale to be used when parsing numbers. |
int |
getMaxFractionDigits()
Return the maximum number of digits
getAsString() should
render in the fraction portion of the result. |
int |
getMaxIntegerDigits()
Return the maximum number of digits
getAsString() should
render in the integer portion of the result. |
int |
getMinFractionDigits()
Return the minimum number of digits
getAsString() should
render in the fraction portion of the result. |
int |
getMinIntegerDigits()
Return the minimum number of digits
getAsString() should
render in the integer portion of the result. |
String |
getPattern()
Return the format pattern to be used when formatting and
parsing numbers.
|
String |
getType()
Return the number type to be used when formatting and parsing numbers.
|
boolean |
initialStateMarked()
Return |
boolean |
isGroupingUsed()
Return
true if getAsString should include
grouping separators if necessary. |
boolean |
isIntegerOnly()
Return
true if only the integer portion of the given
value should be returned from getAsObject() . |
boolean |
isTransient()
If true, the Object implementing this interface must not
participate in state saving or restoring.
|
void |
markInitialState()
The runtime must ensure that the |
void |
restoreState(FacesContext context,
Object state)
Perform any
processing required to restore the state from the entries in the
state Object.
|
Object |
saveState(FacesContext context)
Gets the state of the instance as a
Serializable Object. |
void |
setCurrencyCode(String currencyCode)
Set the ISO 4217 currency code used by
getAsString()
with a type of currency . |
void |
setCurrencySymbol(String currencySymbol)
Set the currency symbol used by
getAsString()
with a type of currency . |
void |
setGroupingUsed(boolean groupingUsed)
Set the flag indicating whether
getAsString() should
include grouping separators if necessary. |
void |
setIntegerOnly(boolean integerOnly)
Set to
true if only the integer portion of the given
value should be returned from getAsObject() . |
void |
setLocale(Locale locale)
Set the
Locale to be used when parsing numbers. |
void |
setMaxFractionDigits(int maxFractionDigits)
Set the maximum number of digits
getAsString() should
render in the fraction portion of the result. |
void |
setMaxIntegerDigits(int maxIntegerDigits)
Set the maximum number of digits
getAsString() should
render in the integer portion of the result. |
void |
setMinFractionDigits(int minFractionDigits)
Set the minimum number of digits
getAsString() should
render in the fraction portion of the result. |
void |
setMinIntegerDigits(int minIntegerDigits)
Set the minimum number of digits
getAsString() should
render in the integer portion of the result. |
void |
setPattern(String pattern)
Set the format pattern to be used when formatting and parsing
numbers.
|
void |
setTransient(boolean transientFlag)
Denotes
whether or not the Object implementing this interface must or
must not participate in state saving or restoring.
|
void |
setType(String type)
Set the number type to be used when formatting and parsing numbers.
|
public static final String CONVERTER_ID
The standard converter id for this converter.
public static final String CURRENCY_ID
The message identifier of the FacesMessage
to be created if
the conversion to Number
fails. The message format
string for this message may optionally include the following
placeholders:
{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by a String
whose value
is the label of the input component that produced this message.public static final String NUMBER_ID
The message identifier of the FacesMessage
to be created if
the conversion to Number
fails. The message format
string for this message may optionally include the following
placeholders:
{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by a String
whose value
is the label of the input component that produced this message.public static final String PATTERN_ID
The message identifier of the FacesMessage
to be created if
the conversion to Number
fails. The message format
string for this message may optionally include the following
placeholders:
{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by a String
whose value
is the label of the input component that produced this message.public static final String PERCENT_ID
The message identifier of the FacesMessage
to be created if
the conversion to Number
fails. The message format
string for this message may optionally include the following
placeholders:
{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by a String
whose value
is the label of the input component that produced this message.public static final String STRING_ID
The message identifier of the FacesMessage
to be created if
the conversion of the Number
value to
String
fails. The message format string for this message
may optionally include the following placeholders:
{0}
relaced by the unconverted value.{1}
replaced by a String
whose value
is the label of the input component that produced this message.public String getCurrencyCode()
Return the ISO 4217 currency code used by getAsString()
with a type
of currency
. If not set,
the value used will be based on the formatting Locale
.
public void setCurrencyCode(String currencyCode)
Set the ISO 4217 currency code used by getAsString()
with a type
of currency
.
currencyCode
- The new currency codepublic String getCurrencySymbol()
Return the currency symbol used by getAsString()
with a type
of currency
. If not set,
the value used will be based on the formatting Locale
.
public void setCurrencySymbol(String currencySymbol)
Set the currency symbol used by getAsString()
with a type
of currency
.
currencySymbol
- The new currency symbolpublic boolean isGroupingUsed()
Return true
if getAsString
should include
grouping separators if necessary. If not modified, the default value
is true
.
public void setGroupingUsed(boolean groupingUsed)
Set the flag indicating whether getAsString()
should
include grouping separators if necessary.
groupingUsed
- The new grouping used flagpublic boolean isIntegerOnly()
Return true
if only the integer portion of the given
value should be returned from getAsObject()
. If not
modified, the default value is false
.
public void setIntegerOnly(boolean integerOnly)
Set to true
if only the integer portion of the given
value should be returned from getAsObject()
.
integerOnly
- The new integer-only flagpublic int getMaxFractionDigits()
Return the maximum number of digits getAsString()
should
render in the fraction portion of the result.
public void setMaxFractionDigits(int maxFractionDigits)
Set the maximum number of digits getAsString()
should
render in the fraction portion of the result. If not set, the number of
digits depends on the value being converted.
maxFractionDigits
- The new limitpublic int getMaxIntegerDigits()
Return the maximum number of digits getAsString()
should
render in the integer portion of the result.
public void setMaxIntegerDigits(int maxIntegerDigits)
Set the maximum number of digits getAsString()
should
render in the integer portion of the result. If not set, the number of
digits depends on the value being converted.
maxIntegerDigits
- The new limitpublic int getMinFractionDigits()
Return the minimum number of digits getAsString()
should
render in the fraction portion of the result.
public void setMinFractionDigits(int minFractionDigits)
Set the minimum number of digits getAsString()
should
render in the fraction portion of the result. If not set, the number of
digits depends on the value being converted.
minFractionDigits
- The new limitpublic int getMinIntegerDigits()
Return the minimum number of digits getAsString()
should
render in the integer portion of the result.
public void setMinIntegerDigits(int minIntegerDigits)
Set the minimum number of digits getAsString()
should
render in the integer portion of the result. If not set, the number of
digits depends on the value being converted.
minIntegerDigits
- The new limitpublic Locale getLocale()
Return the Locale
to be used when parsing numbers.
If this value is null
, the Locale
stored
in the UIViewRoot
for the current request
will be utilized.
Locale
for this converterpublic void setLocale(Locale locale)
Set the Locale
to be used when parsing numbers.
If set to null
, the Locale
stored in the
UIViewRoot
for the current request
will be utilized.
locale
- The new Locale
(or null
)public String getPattern()
Return the format pattern to be used when formatting and parsing numbers.
public void setPattern(String pattern)
Set the format pattern to be used when formatting and parsing
numbers. Valid values are those supported by
java.text.DecimalFormat
.
An invalid value will cause a ConverterException
when
getAsObject()
or getAsString()
is called.
pattern
- The new format patternpublic String getType()
Return the number type to be used when formatting and parsing numbers.
If not modified, the default type is number
.
public void setType(String type)
Set the number type to be used when formatting and parsing numbers.
Valid values are currency
, number
, or
percent
.
An invalid value will cause a ConverterException
when
getAsObject()
or getAsString()
is called.
type
- The new number stylepublic Object getAsObject(FacesContext context, UIComponent component, String value)
Converter
Convert the specified string value, which is associated with
the specified UIComponent
, into a model data object that
is appropriate for being stored during the Process Validations
phase of the request processing lifecycle.
getAsObject
in interface Converter
context
- FacesContext
for the request being processedcomponent
- UIComponent
with which this model object
value is associatedvalue
- String value to be converted (may be null
)null
if the value to convert is null
,
otherwise the result of the conversionConverterException
- if conversion cannot be successfully
performedNullPointerException
- if context
or
component
is null
public String getAsString(FacesContext context, UIComponent component, Object value)
Converter
Convert the specified model object value, which is associated with
the specified UIComponent
, into a String that is suitable
for being included in the response generated during the
Render Response phase of the request processing
lifeycle.
getAsString
in interface Converter
context
- FacesContext
for the request being processedcomponent
- UIComponent
with which this model object
value is associatedvalue
- Model object value to be converted
(may be null
)null
,
otherwise the result of the conversionConverterException
- if conversion cannot be successfully
performedNullPointerException
- if context
or
component
is null
public Object saveState(FacesContext context)
StateHolder
Gets the state of the instance as a
Serializable
Object.
If the class that implements this interface has references to
instances that implement StateHolder (such as a
UIComponent
with event handlers, validators, etc.)
this method must call the StateHolder.saveState(javax.faces.context.FacesContext)
method on all those
instances as well. This method must not save the state
of children and facets. That is done via the StateManager
This method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);
component
should be the same as before executing
it.
The return from this method must be Serializable
saveState
in interface StateHolder
context
- the Faces context.public void restoreState(FacesContext context, Object state)
StateHolder
Perform any processing required to restore the state from the entries in the state Object.
If the class that implements this interface has references to
instances that also implement StateHolder (such as a
UIComponent
with event handlers, validators, etc.)
this method must call the StateHolder.restoreState(javax.faces.context.FacesContext, java.lang.Object)
method on all those
instances as well.
If the state
argument is null
, take no action and return.
restoreState
in interface StateHolder
context
- the Faces context.state
- the state.public boolean isTransient()
StateHolder
If true, the Object implementing this interface must not participate in state saving or restoring.
isTransient
in interface StateHolder
true
if transient, false
otherwise.public void setTransient(boolean transientFlag)
StateHolder
Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
setTransient
in interface StateHolder
transientFlag
- boolean pass true
if this
Object will not
participate in state saving or restoring, otherwise pass
false
.public void markInitialState()
PartialStateHolder
The runtime must ensure that the PartialStateHolder.markInitialState()
method is called on each instance
of this interface in the view at the appropriate time to indicate the component is in its
initial state. The implementor of the interface must ensure that PartialStateHolder.initialStateMarked()
returns true
from the time markInitialState()
is called until
PartialStateHolder.clearInitialState()
is called, after which time initialStateMarked()
must
return false
. Also, during the time that the instance returns true
from initialStateMarked()
, the implementation must return only the state that
has changed in its implementation of StateHolder.saveState(javax.faces.context.FacesContext)
.
markInitialState
in interface PartialStateHolder
public boolean initialStateMarked()
PartialStateHolder
Return true
if delta state changes are being tracked, otherwise
false
initialStateMarked
in interface PartialStateHolder
true
if the initial state is marked, false
otherwise.public void clearInitialState()
PartialStateHolder
Reset the PartialStateHolder to a non-delta tracking state.
clearInitialState
in interface PartialStateHolder
Copyright © 2019 Eclipse Foundation.
Use is subject to license terms.