com.vaadin.data.util.converter
Class AbstractStringToNumberConverter<T>

java.lang.Object
  extended by com.vaadin.data.util.converter.AbstractStringToNumberConverter<T>
All Implemented Interfaces:
Converter<java.lang.String,T>, java.io.Serializable
Direct Known Subclasses:
StringToBigDecimalConverter, StringToBigIntegerConverter, StringToByteConverter, StringToDoubleConverter, StringToFloatConverter, StringToIntegerConverter, StringToLongConverter, StringToShortConverter

public abstract class AbstractStringToNumberConverter<T>
extends java.lang.Object
implements Converter<java.lang.String,T>

A converter that converts from the number type T to String and back. Uses the given locale and NumberFormat for formatting and parsing. Automatically trims the input string, removing any leading and trailing white space.

Override and overwrite getFormat(Locale) to use a different format.

Since:
7.1
Author:
Vaadin Ltd
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.vaadin.data.util.converter.Converter
Converter.ConversionException
 
Constructor Summary
AbstractStringToNumberConverter()
           
 
Method Summary
protected  java.lang.Number convertToNumber(java.lang.String value, java.lang.Class<? extends java.lang.Number> targetType, java.util.Locale locale)
          Convert the value to a Number using the given locale and getFormat(Locale).
 java.lang.String convertToPresentation(T value, java.lang.Class<? extends java.lang.String> targetType, java.util.Locale locale)
          Converts the given value from source type to target type.
protected  java.text.NumberFormat getFormat(java.util.Locale locale)
          Returns the format used by #convertToPresentation(Object, Locale) and #convertToModel(Object, Locale).
 java.lang.Class<java.lang.String> getPresentationType()
          The target type of the converter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.vaadin.data.util.converter.Converter
convertToModel, getModelType
 

Constructor Detail

AbstractStringToNumberConverter

public AbstractStringToNumberConverter()
Method Detail

getFormat

protected java.text.NumberFormat getFormat(java.util.Locale locale)
Returns the format used by #convertToPresentation(Object, Locale) and #convertToModel(Object, Locale).

Parameters:
locale - The locale to use
Returns:
A NumberFormat instance
Since:
7.1

convertToNumber

protected java.lang.Number convertToNumber(java.lang.String value,
                                           java.lang.Class<? extends java.lang.Number> targetType,
                                           java.util.Locale locale)
                                    throws Converter.ConversionException
Convert the value to a Number using the given locale and getFormat(Locale).

Parameters:
value - The value to convert
locale - The locale to use for conversion
Returns:
The converted value
Throws:
ConversionException - If there was a problem converting the value
Converter.ConversionException
Since:
7.1

convertToPresentation

public java.lang.String convertToPresentation(T value,
                                              java.lang.Class<? extends java.lang.String> targetType,
                                              java.util.Locale locale)
                                       throws Converter.ConversionException
Description copied from interface: Converter
Converts the given value from source type to target type.

A converter can optionally use locale to do the conversion.

A converter should in most cases be symmetric so chaining Converter.convertToPresentation(Object, Class, Locale) and Converter.convertToModel(Object, Class, Locale) should return the original value.

Specified by:
convertToPresentation in interface Converter<java.lang.String,T>
Parameters:
value - The value to convert, compatible with the target type. Can be null
targetType - The requested type of the return value
locale - The locale to use for conversion. Can be null.
Returns:
The converted value compatible with the source type
Throws:
Converter.ConversionException - If the value could not be converted

getPresentationType

public java.lang.Class<java.lang.String> getPresentationType()
Description copied from interface: Converter
The target type of the converter. Values of this type can be passed to Converter.convertToModel(Object, Class, Locale).

Specified by:
getPresentationType in interface Converter<java.lang.String,T>
Returns:
The target type


Copyright © 2000-2014 Vaadin Ltd. All Rights Reserved.