Class NumberConverter
- java.lang.Object
-
- jakarta.faces.convert.NumberConverter
-
- All Implemented Interfaces:
PartialStateHolder,StateHolder,Converter
public class NumberConverter extends Object implements Converter, PartialStateHolder
Converterimplementation forjava.lang.Numbervalues.The
getAsObject()method parses a String into anjava.lang.Doubleorjava.lang.Long, according to the following algorithm:- If the specified String is null, return a
null. Otherwise, trim leading and trailing whitespace before proceeding. - If the specified String - after trimming - has a zero length, return
null. - If the
localeproperty is not null, use thatLocalefor managing parsing. Otherwise, use theLocalefrom theUIViewRoot. - If a
patternhas been specified, its syntax must conform the rules specified byjava.text.DecimalFormat. Such a pattern will be used to parse, and thetypeproperty will be ignored. - If a
patternhas not been specified, parsing will be based on thetypeproperty, which expects a currency, a number, or a percent. The parse pattern for currencies, numbers, and percentages is determined by calling thegetCurrencyInstance(),getNumberInstance(), orgetPercentInstance()method of thejava.text.NumberFormatclass, passing in the selectedLocale. - If the
integerOnlyproperty has been set to true, only the integer portion of the String will be parsed. See the JavaDocs for thesetParseIntegerOnly()method of thejava.text.NumberFormatclass for more information.
The
getAsString()method expects a value of typejava.lang.Number(or a subclass), and creates a formatted String according to the following algorithm:- If the specified value is null, return a zero-length String.
- If the specified value is a String, return it unmodified.
- If the
localeproperty is not null, use thatLocalefor managing formatting. Otherwise, use theLocalefrom theFacesContext. - If a
patternhas been specified, its syntax must conform the rules specified byjava.text.DecimalFormat. Such a pattern will be used to format, and thetypeproperty (along with related formatting options described in the next paragraph) will be ignored. - If a
patternhas not been specified, formatting will be based on thetypeproperty, 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 thegetCurrencyInstance(),getNumberInstance(), orgetPercentInstance()method of thejava.text.NumberFormatclass, passing in the selectedLocale. In addition, the following properties will be applied to the format pattern, if specified:- If the
groupingUsedproperty istrue, thesetGroupingUsed(true)method on the correspondingNumberFormatinstance will be called. - The minimum and maximum number of digits in the integer and fractional portions of the result will be configured
based on any values set for the
maxFractionDigits,maxIntegerDigits,minFractionDigits, andminIntegerDigitsproperties. - If the type is set to
currency, it is also possible to configure the currency symbol to be used, using either thecurrencyCodeorcurrencySymbolproperties. If both are set, the value forcurrencyCodetakes precedence on a JDK 1.4 (or later) JVM; otherwise, the value forcurrencySymboltakes precedence.
- If the
-
-
Field Summary
Fields Modifier and Type Field Description static StringCONVERTER_IDThe standard converter id for this converter.static StringCURRENCY_IDThe message identifier of theFacesMessageto be created if the conversion toNumberfails.static StringNUMBER_IDThe message identifier of theFacesMessageto be created if the conversion toNumberfails.static StringPATTERN_IDThe message identifier of theFacesMessageto be created if the conversion toNumberfails.static StringPERCENT_IDThe message identifier of theFacesMessageto be created if the conversion toNumberfails.static StringSTRING_IDThe message identifier of theFacesMessageto be created if the conversion of theNumbervalue toStringfails.-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description NumberConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearInitialState()Reset the PartialStateHolder to a non-delta tracking state.ObjectgetAsObject(FacesContext context, UIComponent component, String value)Convert the specified string value, which is associated with the specifiedUIComponent, into a model data object that is appropriate for being stored during the Process Validations phase of the request processing lifecycle.StringgetAsString(FacesContext context, UIComponent component, Object value)Convert the specified model object value, which is associated with the specifiedUIComponent, into a String that is suitable for being included in the response generated during the Render Response phase of the request processing lifeycle.StringgetCurrencyCode()Return the ISO 4217 currency code used bygetAsString()with atypeofcurrency.StringgetCurrencySymbol()Return the currency symbol used bygetAsString()with atypeofcurrency.LocalegetLocale()Return theLocaleto be used when parsing numbers.intgetMaxFractionDigits()Return the maximum number of digitsgetAsString()should render in the fraction portion of the result.intgetMaxIntegerDigits()Return the maximum number of digitsgetAsString()should render in the integer portion of the result.intgetMinFractionDigits()Return the minimum number of digitsgetAsString()should render in the fraction portion of the result.intgetMinIntegerDigits()Return the minimum number of digitsgetAsString()should render in the integer portion of the result.StringgetPattern()Return the format pattern to be used when formatting and parsing numbers.StringgetType()Return the number type to be used when formatting and parsing numbers.booleaninitialStateMarked()Returntrueif delta state changes are being tracked, otherwisefalsebooleanisGroupingUsed()ReturntrueifgetAsStringshould include grouping separators if necessary.booleanisIntegerOnly()Returntrueif only the integer portion of the given value should be returned fromgetAsObject().booleanisTransient()If true, the Object implementing this interface must not participate in state saving or restoring.voidmarkInitialState()The runtime must ensure that thePartialStateHolder.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.voidrestoreState(FacesContext context, Object state)Perform any processing required to restore the state from the entries in the state Object.ObjectsaveState(FacesContext context)Gets the state of the instance as aSerializableObject.voidsetCurrencyCode(String currencyCode)Set the ISO 4217 currency code used bygetAsString()with atypeofcurrency.voidsetCurrencySymbol(String currencySymbol)Set the currency symbol used bygetAsString()with atypeofcurrency.voidsetGroupingUsed(boolean groupingUsed)Set the flag indicating whethergetAsString()should include grouping separators if necessary.voidsetIntegerOnly(boolean integerOnly)Set totrueif only the integer portion of the given value should be returned fromgetAsObject().voidsetLocale(Locale locale)Set theLocaleto be used when parsing numbers.voidsetMaxFractionDigits(int maxFractionDigits)Set the maximum number of digitsgetAsString()should render in the fraction portion of the result.voidsetMaxIntegerDigits(int maxIntegerDigits)Set the maximum number of digitsgetAsString()should render in the integer portion of the result.voidsetMinFractionDigits(int minFractionDigits)Set the minimum number of digitsgetAsString()should render in the fraction portion of the result.voidsetMinIntegerDigits(int minIntegerDigits)Set the minimum number of digitsgetAsString()should render in the integer portion of the result.voidsetPattern(String pattern)Set the format pattern to be used when formatting and parsing numbers.voidsetTransient(boolean transientFlag)Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.voidsetType(String type)Set the number type to be used when formatting and parsing numbers.
-
-
-
Field Detail
-
CONVERTER_ID
public static final String CONVERTER_ID
The standard converter id for this converter.
- See Also:
- Constant Field Values
-
CURRENCY_ID
public static final String CURRENCY_ID
The message identifier of the
FacesMessageto be created if the conversion toNumberfails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
NUMBER_ID
public static final String NUMBER_ID
The message identifier of the
FacesMessageto be created if the conversion toNumberfails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
PATTERN_ID
public static final String PATTERN_ID
The message identifier of the
FacesMessageto be created if the conversion toNumberfails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
PERCENT_ID
public static final String PERCENT_ID
The message identifier of the
FacesMessageto be created if the conversion toNumberfails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
STRING_ID
public static final String STRING_ID
The message identifier of the
FacesMessageto be created if the conversion of theNumbervalue toStringfails. The message format string for this message may optionally include the following placeholders:{0}relaced by the unconverted value.{1}replaced by aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
-
Method Detail
-
getCurrencyCode
public String getCurrencyCode()
Return the ISO 4217 currency code used by
getAsString()with atypeofcurrency. If not set, the value used will be based on the formattingLocale.- Returns:
- the currency code
-
setCurrencyCode
public void setCurrencyCode(String currencyCode)
Set the ISO 4217 currency code used by
getAsString()with atypeofcurrency.- Parameters:
currencyCode- The new currency code
-
getCurrencySymbol
public String getCurrencySymbol()
Return the currency symbol used by
getAsString()with atypeofcurrency. If not set, the value used will be based on the formattingLocale.- Returns:
- the currency symbol
-
setCurrencySymbol
public void setCurrencySymbol(String currencySymbol)
Set the currency symbol used by
getAsString()with atypeofcurrency.- Parameters:
currencySymbol- The new currency symbol
-
isGroupingUsed
public boolean isGroupingUsed()
Return
trueifgetAsStringshould include grouping separators if necessary. If not modified, the default value istrue.- Returns:
- whether or not grouping is used
-
setGroupingUsed
public void setGroupingUsed(boolean groupingUsed)
Set the flag indicating whether
getAsString()should include grouping separators if necessary.- Parameters:
groupingUsed- The new grouping used flag
-
isIntegerOnly
public boolean isIntegerOnly()
Return
trueif only the integer portion of the given value should be returned fromgetAsObject(). If not modified, the default value isfalse.- Returns:
- whether or not this is integer only
-
setIntegerOnly
public void setIntegerOnly(boolean integerOnly)
Set to
trueif only the integer portion of the given value should be returned fromgetAsObject().- Parameters:
integerOnly- The new integer-only flag
-
getMaxFractionDigits
public int getMaxFractionDigits()
Return the maximum number of digits
getAsString()should render in the fraction portion of the result.- Returns:
- the maximum fraction digits
-
setMaxFractionDigits
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.- Parameters:
maxFractionDigits- The new limit
-
getMaxIntegerDigits
public int getMaxIntegerDigits()
Return the maximum number of digits
getAsString()should render in the integer portion of the result.- Returns:
- the max integer digits
-
setMaxIntegerDigits
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.- Parameters:
maxIntegerDigits- The new limit
-
getMinFractionDigits
public int getMinFractionDigits()
Return the minimum number of digits
getAsString()should render in the fraction portion of the result.- Returns:
- the min fraction digits
-
setMinFractionDigits
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.- Parameters:
minFractionDigits- The new limit
-
getMinIntegerDigits
public int getMinIntegerDigits()
Return the minimum number of digits
getAsString()should render in the integer portion of the result.- Returns:
- the minimum integer digits
-
setMinIntegerDigits
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.- Parameters:
minIntegerDigits- The new limit
-
getLocale
public Locale getLocale()
Return the
Localeto be used when parsing numbers. If this value isnull, theLocalestored in theUIViewRootfor the current request will be utilized.- Returns:
- the
Localefor this converter
-
setLocale
public void setLocale(Locale locale)
Set the
Localeto be used when parsing numbers. If set tonull, theLocalestored in theUIViewRootfor the current request will be utilized.- Parameters:
locale- The newLocale(ornull)
-
getPattern
public String getPattern()
Return the format pattern to be used when formatting and parsing numbers.
- Returns:
- the pattern
-
setPattern
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 aConverterExceptionwhengetAsObject()orgetAsString()is called.- Parameters:
pattern- The new format pattern
-
getType
public String getType()
Return the number type to be used when formatting and parsing numbers. If not modified, the default type is
number.- Returns:
- the type
-
setType
public void setType(String type)
Set the number type to be used when formatting and parsing numbers. Valid values are
currency,number, orpercent. An invalid value will cause aConverterExceptionwhengetAsObject()orgetAsString()is called.- Parameters:
type- The new number style
-
getAsObject
public Object getAsObject(FacesContext context, UIComponent component, String value)
Description copied from interface:ConverterConvert 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.- Specified by:
getAsObjectin interfaceConverter- Parameters:
context-FacesContextfor the request being processedcomponent-UIComponentwith which this model object value is associatedvalue- String value to be converted (may benull)- Returns:
nullif the value to convert isnull, otherwise the result of the conversion- Throws:
ConverterException- if conversion cannot be successfully performedNullPointerException- ifcontextorcomponentisnull
-
getAsString
public String getAsString(FacesContext context, UIComponent component, Object value)
Description copied from interface:ConverterConvert 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.- Specified by:
getAsStringin interfaceConverter- Parameters:
context-FacesContextfor the request being processedcomponent-UIComponentwith which this model object value is associatedvalue- Model object value to be converted (may benull)- Returns:
- a zero-length String if value is
null, otherwise the result of the conversion - Throws:
ConverterException- if conversion cannot be successfully performedNullPointerException- ifcontextorcomponentisnull
-
saveState
public Object saveState(FacesContext context)
Description copied from interface:StateHolderGets the state of the instance as a
SerializableObject.If the class that implements this interface has references to instances that implement StateHolder (such as a
UIComponentwith event handlers, validators, etc.) this method must call theStateHolder.saveState(jakarta.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 theStateManagerThis method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);componentshould be the same as before executing it.The return from this method must be
Serializable- Specified by:
saveStatein interfaceStateHolder- Parameters:
context- the Faces context.- Returns:
- the saved state.
-
restoreState
public void restoreState(FacesContext context, Object state)
Description copied from interface:StateHolderPerform 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
UIComponentwith event handlers, validators, etc.) this method must call theStateHolder.restoreState(jakarta.faces.context.FacesContext, java.lang.Object)method on all those instances as well.If the
stateargument isnull, take no action and return.- Specified by:
restoreStatein interfaceStateHolder- Parameters:
context- the Faces context.state- the state.
-
isTransient
public boolean isTransient()
Description copied from interface:StateHolderIf true, the Object implementing this interface must not participate in state saving or restoring.
- Specified by:
isTransientin interfaceStateHolder- Returns:
trueif transient,falseotherwise.
-
setTransient
public void setTransient(boolean transientFlag)
Description copied from interface:StateHolderDenotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
- Specified by:
setTransientin interfaceStateHolder- Parameters:
transientFlag- boolean passtrueif this Object will not participate in state saving or restoring, otherwise passfalse.
-
markInitialState
public void markInitialState()
Description copied from interface:PartialStateHolderThe 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 thatPartialStateHolder.initialStateMarked()returnstruefrom the timemarkInitialState()is called untilPartialStateHolder.clearInitialState()is called, after which timeinitialStateMarked()must returnfalse. Also, during the time that the instance returnstruefrominitialStateMarked(), the implementation must return only the state that has changed in its implementation ofStateHolder.saveState(jakarta.faces.context.FacesContext).- Specified by:
markInitialStatein interfacePartialStateHolder
-
initialStateMarked
public boolean initialStateMarked()
Description copied from interface:PartialStateHolderReturn
trueif delta state changes are being tracked, otherwisefalse- Specified by:
initialStateMarkedin interfacePartialStateHolder- Returns:
trueif the initial state is marked,falseotherwise.
-
clearInitialState
public void clearInitialState()
Description copied from interface:PartialStateHolderReset the PartialStateHolder to a non-delta tracking state.
- Specified by:
clearInitialStatein interfacePartialStateHolder
-
-