org.springframework.beans.propertyeditors
public class: CustomNumberEditor [javadoc |
source]
java.lang.Object
java.beans.PropertyEditorSupport
org.springframework.beans.propertyeditors.CustomNumberEditor
All Implemented Interfaces:
PropertyEditor
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 in a custom
initBinder method.
Also see:
- java.lang.Number
- java.text.NumberFormat
- org.springframework.validation.DataBinder#registerCustomEditor
- org.springframework.web.servlet.mvc.BaseCommandController#initBinder
- author:
Juergen - Hoeller
- since:
06.06.2003 -
| Constructor: |
public CustomNumberEditor(Class numberClass,
boolean allowEmpty) throws IllegalArgumentException {
this(numberClass, null, allowEmpty);
}
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 - Number subclass to generate
allowEmpty - if empty strings should be allowed
Throws:
IllegalArgumentException - if an invalid numberClass has been specified
Also see:
- org.springframework.util.NumberUtils#parseNumber(String, Class)
- Integer#valueOf
- Integer#toString
|
public CustomNumberEditor(Class numberClass,
NumberFormat numberFormat,
boolean allowEmpty) throws IllegalArgumentException {
if (numberClass == null || !Number.class.isAssignableFrom(numberClass)) {
throw new IllegalArgumentException("Property class must be a subclass of Number");
}
this.numberClass = numberClass;
this.numberFormat = numberFormat;
this.allowEmpty = allowEmpty;
}
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 - Number subclass to generate
numberFormat - NumberFormat to use for parsing and rendering
allowEmpty - if empty strings should be allowed
Throws:
IllegalArgumentException - if an invalid numberClass has been specified
Also see:
- org.springframework.util.NumberUtils#parseNumber(String, Class, java.text.NumberFormat)
- java.text.NumberFormat#parse
- java.text.NumberFormat#format
|
| Methods from java.beans.PropertyEditorSupport: |
|---|
|
addPropertyChangeListener, firePropertyChange, getAsText, getCustomEditor, getJavaInitializationString, getSource, getTags, getValue, isPaintable, paintValue, removePropertyChangeListener, setAsText, setSource, setValue, supportsCustomEditor |
| Method from org.springframework.beans.propertyeditors.CustomNumberEditor Detail: |
public String getAsText() {
Object value = getValue();
if (value == null) {
return "";
}
if (this.numberFormat != null) {
// Use NumberFormat for rendering value.
return this.numberFormat.format(value);
}
else {
// Use toString method for rendering value.
return value.toString();
}
}
Format the Number as String, using the specified NumberFormat. |
public void setAsText(String text) throws IllegalArgumentException {
if (this.allowEmpty && !StringUtils.hasText(text)) {
// Treat empty String as null value.
setValue(null);
}
else if (this.numberFormat != null) {
// Use given NumberFormat for parsing text.
setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
}
else {
// Use default valueOf methods for parsing text.
setValue(NumberUtils.parseNumber(text, this.numberClass));
}
}
Parse the Number from the given text, using the specified NumberFormat. |
public void setValue(Object value) {
if (value instanceof Number) {
super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
}
else {
super.setValue(value);
}
}
Coerce a Number value into the required target class, if necessary. |