summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/data/util/converter/DoubleToStringConverter.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/vaadin/data/util/converter/DoubleToStringConverter.java')
-rw-r--r--src/com/vaadin/data/util/converter/DoubleToStringConverter.java64
1 files changed, 58 insertions, 6 deletions
diff --git a/src/com/vaadin/data/util/converter/DoubleToStringConverter.java b/src/com/vaadin/data/util/converter/DoubleToStringConverter.java
index 18b7f85a13..405d034ed7 100644
--- a/src/com/vaadin/data/util/converter/DoubleToStringConverter.java
+++ b/src/com/vaadin/data/util/converter/DoubleToStringConverter.java
@@ -8,15 +8,49 @@ import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
+/**
+ * A converter that converts from {@link Double} to {@link String} and back.
+ * Uses the given locale and a {@link NumberFormat} instance for formatting and
+ * parsing.
+ * <p>
+ * Override and overwrite {@link #getFormat(Locale)} to use a different format.
+ * </p>
+ *
+ * @author Vaadin Ltd
+ * @version
+ * @VERSION@
+ * @since 7.0
+ */
public class DoubleToStringConverter implements Converter<Double, String> {
- protected NumberFormat getFormatter(Locale locale) {
- return NumberFormat.getNumberInstance(locale);
+ /**
+ * Returns the format used by
+ * {@link #convertFromSourceToTarget(Double, Locale)} and
+ * {@link #convertFromTargetToSource(String, Locale)}.
+ *
+ * @param locale
+ * The locale to use
+ * @return A NumberFormat instance
+ */
+ protected NumberFormat getFormat(Locale locale) {
+ if (locale == null) {
+ return NumberFormat.getNumberInstance();
+ } else {
+ return NumberFormat.getNumberInstance(locale);
+ }
}
- public Double convertFromTargetToSource(String value, Locale locale) {
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java
+ * .lang.Object, java.util.Locale)
+ */
+ public Double convertFromTargetToSource(String value, Locale locale)
+ throws ConversionException {
ParsePosition parsePosition = new ParsePosition(0);
- Number parsedValue = getFormatter(locale).parse(value, parsePosition);
+ Number parsedValue = getFormat(locale).parse(value, parsePosition);
if (parsePosition.getIndex() != value.length()) {
throw new ConversionException("Could not convert '" + value
+ "' to " + getTargetType().getName());
@@ -24,18 +58,36 @@ public class DoubleToStringConverter implements Converter<Double, String> {
return parsedValue.doubleValue();
}
- public String convertFromSourceToTarget(Double value, Locale locale) {
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java
+ * .lang.Object, java.util.Locale)
+ */
+ public String convertFromSourceToTarget(Double value, Locale locale)
+ throws ConversionException {
if (value == null) {
return null;
}
- return getFormatter(locale).format(value);
+ return getFormat(locale).format(value);
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getSourceType()
+ */
public Class<Double> getSourceType() {
return Double.class;
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getTargetType()
+ */
public Class<String> getTargetType() {
return String.class;
}