Top Description Fields Constructors Methods
org.springframework.beans.propertyeditors

public Class CustomNumberEditor

extends PropertyEditorSupport
Class Inheritance
Imports
java.beans.PropertyEditorSupport, java.text.NumberFormat, org.springframework.lang.Nullable, org.springframework.util.NumberUtils, .StringUtils

Property editor for any Number subclass such as Short, Integer, Long, BigInteger, Float, Double, BigDecimal. Can use a given NumberFormat for (locale-specific) parsing and rendering, or alternatively the default decode / valueOf / toString methods.

This is not meant to be used as system PropertyEditor but rather as locale-specific number editor within custom controller code, parsing user-entered number strings into Number properties of beans and rendering them in the UI form.

In web MVC code, this editor will typically be registered with binder.registerCustomEditor calls.

Author
Juergen Hoeller
Since
06.06.2003
See Also
Number, java.text.NumberFormat, org.springframework.validation.DataBinder#registerCustomEditor

Field Summary

Modifier and TypeField and Description
private final boolean
private final Class<? extends Number>
private final NumberFormat

Constructor Summary

AccessConstructor and Description
public
CustomNumberEditor(Class<? extends Number>
the Number subclass to generate
numberClass
,
boolean
if empty strings should be allowed
allowEmpty
)

Create a new CustomNumberEditor instance, using the default valueOf methods for parsing and toString methods for rendering.

public
CustomNumberEditor(Class<? extends Number>
the Number subclass to generate
numberClass
,
NumberFormat
the NumberFormat to use for parsing and rendering
numberFormat
,
boolean
if empty strings should be allowed
allowEmpty
)

Create a new CustomNumberEditor instance, using the given NumberFormat for parsing and rendering.

Method Summary

Modifier and TypeMethod and Description
public String
getAsText()

Overrides java.beans.PropertyEditorSupport.getAsText.

Implements java.beans.PropertyEditor.getAsText.

Format the Number as String, using the specified NumberFormat.

public void
setAsText(String
The string to be parsed.
text
)

Overrides java.beans.PropertyEditorSupport.setAsText.

Implements java.beans.PropertyEditor.setAsText.

Parse the Number from the given text, using the specified NumberFormat.

public void
setValue(Object
The new target object to be edited. Note that this object should not be modified by the PropertyEditor, rather the PropertyEditor should create a new object to hold any modified value.
value
)

Overrides java.beans.PropertyEditorSupport.setValue.

Implements java.beans.PropertyEditor.setValue.

Coerce a Number value into the required target class, if necessary.

Inherited from java.beans.PropertyEditorSupport:
addPropertyChangeListenerfirePropertyChangegetCustomEditorgetJavaInitializationStringgetSourcegetTagsgetValueisPaintablepaintValueremovePropertyChangeListenersetSourcesupportsCustomEditor

Field Detail

allowEmptyback to summary
private final boolean allowEmpty
numberClassback to summary
private final Class<? extends Number> numberClass
numberFormatback to summary
private final NumberFormat numberFormat
Annotations
@Nullable

Constructor Detail

CustomNumberEditorback to summary
public CustomNumberEditor(Class<? extends Number> numberClass, boolean allowEmpty) throws IllegalArgumentException

Create a new CustomNumberEditor instance, using the default valueOf methods for parsing and toString methods for rendering.

The "allowEmpty" parameter states if an empty String should be allowed for parsing, i.e. get interpreted as null value. Else, an IllegalArgumentException gets thrown in that case.

Parameters
numberClass:Class<? extends Number>

the Number subclass to generate

allowEmpty:boolean

if empty strings should be allowed

Exceptions
IllegalArgumentException:
if an invalid numberClass has been specified
See Also
org.springframework.util.NumberUtils#parseNumber(String, Class), Integer#valueOf, Integer#toString
CustomNumberEditorback to summary
public CustomNumberEditor(Class<? extends Number> numberClass, NumberFormat numberFormat, boolean allowEmpty) throws IllegalArgumentException

Create a new CustomNumberEditor instance, using the given NumberFormat for parsing and rendering.

The allowEmpty parameter states if an empty String should be allowed for parsing, i.e. get interpreted as null value. Else, an IllegalArgumentException gets thrown in that case.

Parameters
numberClass:Class<? extends Number>

the Number subclass to generate

numberFormat:NumberFormat

the NumberFormat to use for parsing and rendering

allowEmpty:boolean

if empty strings should be allowed

Exceptions
IllegalArgumentException:
if an invalid numberClass has been specified
See Also
org.springframework.util.NumberUtils#parseNumber(String, Class, java.text.NumberFormat), java.text.NumberFormat#parse, java.text.NumberFormat#format

Method Detail

getAsTextback to summary
public String getAsText()

Overrides java.beans.PropertyEditorSupport.getAsText.

Implements java.beans.PropertyEditor.getAsText.

Format the Number as String, using the specified NumberFormat.

Returns:String

Doc from java.beans.PropertyEditorSupport.getAsText.

The property value as a string suitable for presentation to a human to edit.

Returns null if the value can't be expressed as a string.

If a non-null value is returned, then the PropertyEditor should be prepared to parse that string back in setAsText().

Annotations
@Override
setAsTextback to summary
public void setAsText(String text) throws IllegalArgumentException

Overrides java.beans.PropertyEditorSupport.setAsText.

Implements java.beans.PropertyEditor.setAsText.

Parse the Number from the given text, using the specified NumberFormat.

Parameters
text:String

Doc from java.beans.PropertyEditorSupport.setAsText.

The string to be parsed.

Annotations
@Override
setValueback to summary
public void setValue(Object value)

Overrides java.beans.PropertyEditorSupport.setValue.

Implements java.beans.PropertyEditor.setValue.

Coerce a Number value into the required target class, if necessary.

Parameters
value:Object

Doc from java.beans.PropertyEditorSupport.setValue.

The new target object to be edited. Note that this object should not be modified by the PropertyEditor, rather the PropertyEditor should create a new object to hold any modified value.

Annotations
@Override