Top Description Fields Constructors Methods
javax.faces.convert

public Class NumberConverter

extends Object
implements Converter, PartialStateHolder
Class Inheritance
All Implemented Interfaces
javax.faces.component.PartialStateHolder, javax.faces.component.StateHolder, javax.faces.convert.Converter
Imports
javax.el.ValueExpression, javax.faces.component.UIComponent, .PartialStateHolder, javax.faces.context.FacesContext, java.lang.reflect.Method, java.math.BigDecimal, java.text.DecimalFormat, .DecimalFormatSymbols, .NumberFormat, .ParseException, java.util.Locale

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:

The getAsString() method expects a value of type java.lang.Number (or a subclass), and creates a formatted String according to the following algorithm:

Field Summary

Modifier and TypeField and Description
public static final String
CONVERTER_ID

The standard converter id for this converter.

public static final String
CURRENCY_ID

The message identifier of the javax.faces.application.FacesMessage to be created if the conversion to Number fails.

private static Class<T>
private String
private String
private static final Class<T>[]
private Boolean
private boolean
private Boolean
private Locale
private Integer
private Integer
private Integer
private Integer
private static final String
public static final String
NUMBER_ID

The message identifier of the javax.faces.application.FacesMessage to be created if the conversion to Number fails.

private String
public static final String
PATTERN_ID

The message identifier of the javax.faces.application.FacesMessage to be created if the conversion to Number fails.

public static final String
PERCENT_ID

The message identifier of the javax.faces.application.FacesMessage to be created if the conversion to Number fails.

public static final String
STRING_ID

The message identifier of the javax.faces.application.FacesMessage to be created if the conversion of the Number value to String fails.

private boolean
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
clearInitialState()

Implements javax.faces.component.PartialStateHolder.clearInitialState.

Reset the PartialStateHolder to a non-delta tracking state.

private void
configureCurrency(NumberFormat
The NumberFormatter to be configured
formatter
)

Override the formatting locale's default currency symbol with the specified currency code (specified via the "currencyCode" attribute) or currency symbol (specified via the "currencySymbol" attribute).

private void
configureFormatter(NumberFormat
The NumberFormat instance to configure
formatter
)

Configure the specified NumberFormat based on the formatting properties that have been set.

public Object
getAsObject(FacesContext
FacesContext for the request being processed
context
,
UIComponent
UIComponent with which this model object value is associated
component
,
String
String value to be converted (may be null)
value
)

Implements javax.faces.convert.Converter.getAsObject.

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.

public String
getAsString(FacesContext
FacesContext for the request being processed
context
,
UIComponent
UIComponent with which this model object value is associated
component
,
Object
Model object value to be converted (may be null)
value
)

Implements javax.faces.convert.Converter.getAsString.

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.

public String

Returns:

the currency code
getCurrencyCode
()

Return the ISO 4217 currency code used by getAsString() with a type of currency.

public String

Returns:

the currency symbol
getCurrencySymbol
()

Return the currency symbol used by getAsString() with a type of currency.

public Locale

Returns:

the Locale for this converter
getLocale
()

Return the Locale to be used when parsing numbers.

private Locale
getLocale(FacesContext
The FacesContext for the current request
context
)

Return the Locale we will use for localizing our formatting and parsing processing.

public int

Returns:

the maximum fraction digits
getMaxFractionDigits
()

Return the maximum number of digits getAsString() should render in the fraction portion of the result.

public int

Returns:

the max integer digits
getMaxIntegerDigits
()

Return the maximum number of digits getAsString() should render in the integer portion of the result.

public int

Returns:

the min fraction digits
getMinFractionDigits
()

Return the minimum number of digits getAsString() should render in the fraction portion of the result.

public int

Returns:

the minimum integer digits
getMinIntegerDigits
()

Return the minimum number of digits getAsString() should render in the integer portion of the result.

private NumberFormat
getNumberFormat(Locale
The Locale used to select formatting and parsing conventions
locale
)

Return a NumberFormat instance to use for formatting and parsing in this Converter.

public String

Returns:

the pattern
getPattern
()

Return the format pattern to be used when formatting and parsing numbers.

public String

Returns:

the type
getType
()

Return the number type to be used when formatting and parsing numbers.

public boolean
initialStateMarked()

Implements javax.faces.component.PartialStateHolder.initialStateMarked.

Return true if delta state changes are being tracked, otherwise false

public boolean

Returns:

whether or not grouping is used
isGroupingUsed
()

Return true if getAsString should include grouping separators if necessary.

public boolean

Returns:

whether or not this is integer only
isIntegerOnly
()

Return true if only the integer portion of the given value should be returned from getAsObject().

private boolean
private boolean
private boolean
private boolean
public boolean
isTransient()

Implements javax.faces.component.StateHolder.isTransient.

If true, the Object implementing this interface must not participate in state saving or restoring.

public void
markInitialState()

Implements javax.faces.component.PartialStateHolder.markInitialState.

The runtime must ensure that the 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.

public void
restoreState(FacesContext
the Faces context.
context
,
Object
the state.
state
)

Implements javax.faces.component.StateHolder.restoreState.

Perform any processing required to restore the state from the entries in the state Object.

public Object
saveState(FacesContext
the Faces context.
context
)

Implements javax.faces.component.StateHolder.saveState.

Gets the state of the instance as a Serializable Object.

public void
setCurrencyCode(String
The new currency code
currencyCode
)

Set the ISO 4217 currency code used by getAsString() with a type of currency.

public void
setCurrencySymbol(String
The new currency symbol
currencySymbol
)

Set the currency symbol used by getAsString() with a type of currency.

public void
setGroupingUsed(boolean
The new grouping used flag
groupingUsed
)

Set the flag indicating whether getAsString() should include grouping separators if necessary.

public void
setIntegerOnly(boolean
The new integer-only flag
integerOnly
)

Set to true if only the integer portion of the given value should be returned from getAsObject().

public void
setLocale(Locale
The new Locale (or null)
locale
)

Set the Locale to be used when parsing numbers.

public void
setMaxFractionDigits(int
The new limit
maxFractionDigits
)

Set the maximum number of digits getAsString() should render in the fraction portion of the result.

public void
setMaxIntegerDigits(int
The new limit
maxIntegerDigits
)

Set the maximum number of digits getAsString() should render in the integer portion of the result.

public void
setMinFractionDigits(int
The new limit
minFractionDigits
)

Set the minimum number of digits getAsString() should render in the fraction portion of the result.

public void
setMinIntegerDigits(int
The new limit
minIntegerDigits
)

Set the minimum number of digits getAsString() should render in the integer portion of the result.

public void
setPattern(String
The new format pattern
pattern
)

Set the format pattern to be used when formatting and parsing numbers.

public void
setTransient(boolean
boolean pass true if this Object will not participate in state saving or restoring, otherwise pass false.
transientFlag
)

Implements javax.faces.component.StateHolder.setTransient.

Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.

public void
setType(String
The new number style
type
)

Set the number type to be used when formatting and parsing numbers.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONVERTER_IDback to summary
public static final String CONVERTER_ID

The standard converter id for this converter.

CURRENCY_IDback to summary
public static final String CURRENCY_ID

The message identifier of the javax.faces.application.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.
currencyClassback to summary
private static Class<T> currencyClass
currencyCodeback to summary
private String currencyCode
currencySymbolback to summary
private String currencySymbol
GET_INSTANCE_PARAM_TYPESback to summary
private static final Class<T>[] GET_INSTANCE_PARAM_TYPES
groupingUsedback to summary
private Boolean groupingUsed
initialStateback to summary
private boolean initialState
integerOnlyback to summary
private Boolean integerOnly
localeback to summary
private Locale locale
maxFractionDigitsback to summary
private Integer maxFractionDigits
maxIntegerDigitsback to summary
private Integer maxIntegerDigits
minFractionDigitsback to summary
private Integer minFractionDigits
minIntegerDigitsback to summary
private Integer minIntegerDigits
NBSPback to summary
private static final String NBSP
NUMBER_IDback to summary
public static final String NUMBER_ID

The message identifier of the javax.faces.application.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.
patternback to summary
private String pattern
PATTERN_IDback to summary
public static final String PATTERN_ID

The message identifier of the javax.faces.application.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.
PERCENT_IDback to summary
public static final String PERCENT_ID

The message identifier of the javax.faces.application.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.
STRING_IDback to summary
public static final String STRING_ID

The message identifier of the javax.faces.application.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.
transientFlagback to summary
private boolean transientFlag
typeback to summary
private String type

Constructor Detail

NumberConverterback to summary
public NumberConverter()

Method Detail

clearInitialStateback to summary
public void clearInitialState()

Implements javax.faces.component.PartialStateHolder.clearInitialState.

Doc from javax.faces.component.PartialStateHolder.clearInitialState.

Reset the PartialStateHolder to a non-delta tracking state.

Annotations
@Override
configureCurrencyback to summary
private void configureCurrency(NumberFormat formatter) throws Exception

Override the formatting locale's default currency symbol with the specified currency code (specified via the "currencyCode" attribute) or currency symbol (specified via the "currencySymbol" attribute).

If both "currencyCode" and "currencySymbol" are present, "currencyCode" takes precedence over "currencySymbol" if the java.util.Currency class is defined in the container's runtime (that is, if the container's runtime is J2SE 1.4 or greater), and "currencySymbol" takes precendence over "currencyCode" otherwise.

If only "currencyCode" is given, it is used as a currency symbol if java.util.Currency is not defined.

Example:

JDK "currencyCode" "currencySymbol" Currency symbol being displayed ----------------------------------------------------------------------- all --- --- Locale's default currency symbol

<1.4 EUR --- EUR >=1.4 EUR --- Locale's currency symbol for Euro

all --- € €

<1.4 EUR € € >=1.4 EUR € Locale's currency symbol for Euro

Parameters
formatter:NumberFormat

The NumberFormatter to be configured

configureFormatterback to summary
private void configureFormatter(NumberFormat formatter)

Configure the specified NumberFormat based on the formatting properties that have been set.

Parameters
formatter:NumberFormat

The NumberFormat instance to configure

getAsObjectback to summary
public Object getAsObject(FacesContext context, UIComponent component, String value)

Implements javax.faces.convert.Converter.getAsObject.

Doc from javax.faces.convert.Converter.getAsObject.

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.

Parameters
context:FacesContext

FacesContext for the request being processed

component:UIComponent

UIComponent with which this model object value is associated

value:String

String value to be converted (may be null)

Returns:Object

null if the value to convert is null, otherwise the result of the conversion

Annotations
@Override
Exceptions
ConverterException:
if conversion cannot be successfully performed
NullPointerException:
if context or component is null
getAsStringback to summary
public String getAsString(FacesContext context, UIComponent component, Object value)

Implements javax.faces.convert.Converter.getAsString.

Doc from javax.faces.convert.Converter.getAsString.

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.

Parameters
context:FacesContext

FacesContext for the request being processed

component:UIComponent

UIComponent with which this model object value is associated

value:Object

Model object value to be converted (may be null)

Returns:String

a zero-length String if value is null, otherwise the result of the conversion

Annotations
@Override
Exceptions
ConverterException:
if conversion cannot be successfully performed
NullPointerException:
if context or component is null
getCurrencyCodeback to summary
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.

Returns:String

the currency code

getCurrencySymbolback to summary
public 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.

Returns:String

the currency symbol

getLocaleback to summary
public Locale getLocale()

Return the Locale to be used when parsing numbers. If this value is null, the Locale stored in the javax.faces.component.UIViewRoot for the current request will be utilized.

Returns:Locale

the Locale for this converter

getLocaleback to summary
private Locale getLocale(FacesContext context)

Return the Locale we will use for localizing our formatting and parsing processing.

Parameters
context:FacesContext

The FacesContext for the current request

getMaxFractionDigitsback to summary
public int getMaxFractionDigits()

Return the maximum number of digits getAsString() should render in the fraction portion of the result.

Returns:int

the maximum fraction digits

getMaxIntegerDigitsback to summary
public int getMaxIntegerDigits()

Return the maximum number of digits getAsString() should render in the integer portion of the result.

Returns:int

the max integer digits

getMinFractionDigitsback to summary
public int getMinFractionDigits()

Return the minimum number of digits getAsString() should render in the fraction portion of the result.

Returns:int

the min fraction digits

getMinIntegerDigitsback to summary
public int getMinIntegerDigits()

Return the minimum number of digits getAsString() should render in the integer portion of the result.

Returns:int

the minimum integer digits

getNumberFormatback to summary
private NumberFormat getNumberFormat(Locale locale)

Return a NumberFormat instance to use for formatting and parsing in this Converter.

Parameters
locale:Locale

The Locale used to select formatting and parsing conventions

Exceptions
ConverterException:
if no instance can be created
getPatternback to summary
public String getPattern()

Return the format pattern to be used when formatting and parsing numbers.

Returns:String

the pattern

getTypeback to summary
public String getType()

Return the number type to be used when formatting and parsing numbers. If not modified, the default type is number.

Returns:String

the type

initialStateMarkedback to summary
public boolean initialStateMarked()

Implements javax.faces.component.PartialStateHolder.initialStateMarked.

Doc from javax.faces.component.PartialStateHolder.initialStateMarked.

Return true if delta state changes are being tracked, otherwise false

Returns:boolean

true if the initial state is marked, false otherwise.

Annotations
@Override
isGroupingUsedback to summary
public boolean isGroupingUsed()

Return true if getAsString should include grouping separators if necessary. If not modified, the default value is true.

Returns:boolean

whether or not grouping is used

isIntegerOnlyback to summary
public 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.

Returns:boolean

whether or not this is integer only

isMaxFractionDigitsSetback to summary
private boolean isMaxFractionDigitsSet()
isMaxIntegerDigitsSetback to summary
private boolean isMaxIntegerDigitsSet()
isMinFractionDigitsSetback to summary
private boolean isMinFractionDigitsSet()
isMinIntegerDigitsSetback to summary
private boolean isMinIntegerDigitsSet()
isTransientback to summary
public boolean isTransient()

Implements javax.faces.component.StateHolder.isTransient.

Doc from javax.faces.component.StateHolder.isTransient.

If true, the Object implementing this interface must not participate in state saving or restoring.

Returns:boolean

true if transient, false otherwise.

Annotations
@Override
markInitialStateback to summary
public void markInitialState()

Implements javax.faces.component.PartialStateHolder.markInitialState.

Doc from javax.faces.component.PartialStateHolder.markInitialState.

The runtime must ensure that the 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 initialStateMarked returns true from the time markInitialState() is called until 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.

Annotations
@Override
restoreStateback to summary
public void restoreState(FacesContext context, Object state)

Implements javax.faces.component.StateHolder.restoreState.

Doc from javax.faces.component.StateHolder.restoreState.

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 restoreState method on all those instances as well.

If the state argument is null, take no action and return.

Parameters
context:FacesContext

the Faces context.

state:Object

the state.

Annotations
@Override
saveStateback to summary
public Object saveState(FacesContext context)

Implements javax.faces.component.StateHolder.saveState.

Doc from javax.faces.component.StateHolder.saveState.

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 saveState method on all those instances as well. This method must not save the state of children and facets. That is done via the javax.faces.application.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

Parameters
context:FacesContext

the Faces context.

Returns:Object

the saved state.

Annotations
@Override
setCurrencyCodeback to summary
public void setCurrencyCode(String currencyCode)

Set the ISO 4217 currency code used by getAsString() with a type of currency.

Parameters
currencyCode:String

The new currency code

setCurrencySymbolback to summary
public void setCurrencySymbol(String currencySymbol)

Set the currency symbol used by getAsString() with a type of currency.

Parameters
currencySymbol:String

The new currency symbol

setGroupingUsedback to summary
public void setGroupingUsed(boolean groupingUsed)

Set the flag indicating whether getAsString() should include grouping separators if necessary.

Parameters
groupingUsed:boolean

The new grouping used flag

setIntegerOnlyback to summary
public void setIntegerOnly(boolean integerOnly)

Set to true if only the integer portion of the given value should be returned from getAsObject().

Parameters
integerOnly:boolean

The new integer-only flag

setLocaleback to summary
public void setLocale(Locale locale)

Set the Locale to be used when parsing numbers. If set to null, the Locale stored in the javax.faces.component.UIViewRoot for the current request will be utilized.

Parameters
locale:Locale

The new Locale (or null)

setMaxFractionDigitsback to summary
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:int

The new limit

setMaxIntegerDigitsback to summary
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:int

The new limit

setMinFractionDigitsback to summary
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:int

The new limit

setMinIntegerDigitsback to summary
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:int

The new limit

setPatternback to summary
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.

Parameters
pattern:String

The new format pattern

setTransientback to summary
public void setTransient(boolean transientFlag)

Implements javax.faces.component.StateHolder.setTransient.

Doc from javax.faces.component.StateHolder.setTransient.

Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.

Parameters
transientFlag:boolean

boolean pass true if this Object will not participate in state saving or restoring, otherwise pass false.

Annotations
@Override
setTypeback to summary
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.

Parameters
type:String

The new number style