aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-09-04 23:35:42 +0300
committerArtur Signell <artur@vaadin.com>2016-09-05 11:34:20 +0300
commit6941d683eb15a8ae10e0d302bcf9608be8825f8a (patch)
tree77a8d97f0342b3f6ff2eaeb4649c7ec893ea3127 /server/src/main/java/com
parentccaabe6db025f7e73adc83b4d0e2671c7fa16d40 (diff)
downloadvaadin-framework-6941d683eb15a8ae10e0d302bcf9608be8825f8a.tar.gz
vaadin-framework-6941d683eb15a8ae10e0d302bcf9608be8825f8a.zip
Add error message constructor to all converters which can fail
Change-Id: I3ec60effc75e22765d21e0223ee1537ffbdb29e7
Diffstat (limited to 'server/src/main/java/com')
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java34
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java14
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java22
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java20
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java21
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java23
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java37
-rw-r--r--server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java21
-rw-r--r--server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java19
9 files changed, 157 insertions, 54 deletions
diff --git a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
index bd2ea690a1..c06a9baa61 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
@@ -20,6 +20,8 @@ import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
+import com.vaadin.data.Result;
+
/**
* A converter that converts from the number type T to {@link String} and back.
* Uses the given locale and {@link NumberFormat} for formatting and parsing.
@@ -35,6 +37,18 @@ import java.util.Locale;
public abstract class AbstractStringToNumberConverter<T>
implements Converter<String, T> {
+ private final String errorMessage;
+
+ /**
+ * Creates a new converter instance with the given error message.
+ *
+ * @param errorMessage
+ * the error message to use if conversion fails
+ */
+ protected AbstractStringToNumberConverter(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
/**
* Returns the format used by {@link #convertToPresentation(Object, Locale)}
* and {@link #convertToModel(Object, Locale)}.
@@ -61,9 +75,9 @@ public abstract class AbstractStringToNumberConverter<T>
* The locale to use for conversion
* @return The converted value
*/
- protected Number convertToNumber(String value, Locale locale) {
+ protected Result<Number> convertToNumber(String value, Locale locale) {
if (value == null) {
- return null;
+ return Result.ok(null);
}
// Remove leading and trailing white space
@@ -74,16 +88,24 @@ public abstract class AbstractStringToNumberConverter<T>
ParsePosition parsePosition = new ParsePosition(0);
Number parsedValue = getFormat(locale).parse(value, parsePosition);
if (parsePosition.getIndex() != value.length()) {
- throw new IllegalArgumentException(
- "Could not convert '" + value + "'");
+ return Result.error(getErrorMessage());
}
if (parsedValue == null) {
// Convert "" to null
- return null;
+ return Result.ok(null);
}
- return parsedValue;
+ return Result.ok(parsedValue);
+ }
+
+ /**
+ * Gets the error message to use when conversion fails.
+ *
+ * @return the error message
+ */
+ protected String getErrorMessage() {
+ return errorMessage;
}
@Override
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
index 639523516e..af0a63fc7a 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
@@ -38,6 +38,17 @@ import com.vaadin.data.Result;
*/
public class StringToBigDecimalConverter
extends AbstractStringToNumberConverter<BigDecimal> {
+
+ /**
+ * Creates a new converter instance with the given error message.
+ *
+ * @param errorMessage
+ * the error message to use if conversion fails
+ */
+ public StringToBigDecimalConverter(String errorMessage) {
+ super(errorMessage);
+ }
+
@Override
protected NumberFormat getFormat(Locale locale) {
NumberFormat numberFormat = super.getFormat(locale);
@@ -50,7 +61,8 @@ public class StringToBigDecimalConverter
@Override
public Result<BigDecimal> convertToModel(String value, Locale locale) {
- return Result.ok((BigDecimal) convertToNumber(value, locale));
+ return convertToNumber(value, locale)
+ .map(number -> (BigDecimal) number);
}
}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
index 58080013b6..d469db7055 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
@@ -39,6 +39,15 @@ import com.vaadin.data.Result;
*/
public class StringToBigIntegerConverter
extends AbstractStringToNumberConverter<BigInteger> {
+ /**
+ * Creates a new converter instance with the given error message.
+ *
+ * @param errorMessage
+ * the error message to use if conversion fails
+ */
+ public StringToBigIntegerConverter(String errorMessage) {
+ super(errorMessage);
+ }
@Override
protected NumberFormat getFormat(Locale locale) {
@@ -52,12 +61,13 @@ public class StringToBigIntegerConverter
@Override
public Result<BigInteger> convertToModel(String value, Locale locale) {
-
- BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value,
- locale);
-
- return (bigDecimalValue != null)
- ? Result.ok(bigDecimalValue.toBigInteger()) : Result.ok(null);
+ return convertToNumber(value, locale).map(number -> {
+ if (number == null) {
+ return null;
+ } else {
+ return ((BigDecimal) number).toBigInteger();
+ }
+ });
}
}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
index 23ce228619..7b9e1850f0 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
@@ -41,31 +41,39 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
private final String falseString;
+ private String errorMessage;
+
/**
* Creates converter with default string representations - "true" and
* "false".
*
+ * @param errorMessage
+ * the error message to use if conversion fails
*/
- public StringToBooleanConverter() {
- this(Boolean.TRUE.toString(), Boolean.FALSE.toString());
+ public StringToBooleanConverter(String errorMessage) {
+ this(errorMessage, Boolean.TRUE.toString(), Boolean.FALSE.toString());
}
/**
* Creates converter with custom string representation.
*
+ * @param errorMessage
+ * the error message to use if conversion fails
* @param falseString
* string representation for <code>false</code>
* @param trueString
* string representation for <code>true</code>
*/
- public StringToBooleanConverter(String trueString, String falseString) {
+ public StringToBooleanConverter(String errorMessage, String trueString,
+ String falseString) {
+ this.errorMessage = errorMessage;
this.trueString = trueString;
this.falseString = falseString;
}
@Override
public Result<Boolean> convertToModel(String value, Locale locale) {
- if (value == null || value.isEmpty()) {
+ if (value == null) {
return Result.ok(null);
}
@@ -76,8 +84,10 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
return Result.ok(true);
} else if (getFalseString(locale).equals(value)) {
return Result.ok(false);
+ } else if (value.isEmpty()) {
+ return Result.ok(null);
} else {
- throw new IllegalArgumentException("Cannot convert " + value);
+ return Result.error(errorMessage);
}
}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
index 374a553e8e..463a7f6b36 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
@@ -38,10 +38,27 @@ import com.vaadin.data.Result;
public class StringToDoubleConverter
extends AbstractStringToNumberConverter<Double> {
+ /**
+ * Creates a new converter instance with the given error message.
+ *
+ * @param errorMessage
+ * the error message to use if conversion fails
+ */
+ public StringToDoubleConverter(String errorMessage) {
+ super(errorMessage);
+ }
+
@Override
public Result<Double> convertToModel(String value, Locale locale) {
- Number n = convertToNumber(value, locale);
- return n == null ? Result.ok(null) : Result.ok(n.doubleValue());
+ Result<Number> n = convertToNumber(value, locale);
+
+ return n.map(number -> {
+ if (number == null) {
+ return null;
+ } else {
+ return number.doubleValue();
+ }
+ });
}
}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
index d05374a3cc..be592665cf 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
@@ -27,10 +27,8 @@ import com.vaadin.data.Result;
* parsing.
* <p>
* Leading and trailing white spaces are ignored when converting from a String.
- * </p>
* <p>
* Override and overwrite {@link #getFormat(Locale)} to use a different format.
- * </p>
*
* @author Vaadin Ltd
* @since 8.0
@@ -38,10 +36,27 @@ import com.vaadin.data.Result;
public class StringToFloatConverter
extends AbstractStringToNumberConverter<Float> {
+ /**
+ * Creates a new converter instance with the given error message.
+ *
+ * @param errorMessage
+ * the error message to use if conversion fails
+ */
+ public StringToFloatConverter(String errorMessage) {
+ super(errorMessage);
+ }
+
@Override
public Result<Float> convertToModel(String value, Locale locale) {
- Number n = convertToNumber(value, locale);
- return n == null ? Result.ok(null) : Result.ok(n.floatValue());
+ Result<Number> n = convertToNumber(value, locale);
+
+ return n.map(number -> {
+ if (number == null) {
+ return null;
+ } else {
+ return number.floatValue();
+ }
+ });
}
}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
index 7a5ee69aed..ec05647a51 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
@@ -35,8 +35,6 @@ import com.vaadin.data.Result;
public class StringToIntegerConverter
extends AbstractStringToNumberConverter<Integer> {
- private final String errorMessage;
-
/**
* Creates a new converter instance with the given error message.
*
@@ -44,7 +42,7 @@ public class StringToIntegerConverter
* the error message to use if conversion fails
*/
public StringToIntegerConverter(String errorMessage) {
- this.errorMessage = errorMessage;
+ super(errorMessage);
}
/**
@@ -66,22 +64,23 @@ public class StringToIntegerConverter
@Override
public Result<Integer> convertToModel(String value, Locale locale) {
- Number n = convertToNumber(value, locale);
-
- if (n == null) {
- return Result.ok(null);
- }
-
- int intValue = n.intValue();
- if (intValue == n.longValue()) {
- // If the value of n is outside the range of long, the return value
- // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The
- // above comparison promotes int to long and thus does not need to
- // consider wrap-around.
- return Result.ok(intValue);
- } else {
- return Result.error(errorMessage);
- }
+ Result<Number> n = convertToNumber(value, locale);
+ return n.flatMap(number -> {
+ if (number == null) {
+ return Result.ok(null);
+ } else {
+ int intValue = number.intValue();
+ if (intValue == number.longValue()) {
+ // If the value of n is outside the range of long, the
+ // return value of longValue() is either Long.MIN_VALUE or
+ // Long.MAX_VALUE. The/ above comparison promotes int to
+ // long and thus does not need to consider wrap-around.
+ return Result.ok(intValue);
+ } else {
+ return Result.error(getErrorMessage());
+ }
+ }
+ });
}
}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
index d82dec9ae9..9b7bfa4fc6 100644
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
+++ b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
@@ -36,6 +36,16 @@ public class StringToLongConverter
extends AbstractStringToNumberConverter<Long> {
/**
+ * Creates a new converter instance with the given error message.
+ *
+ * @param errorMessage
+ * the error message to use if conversion fails
+ */
+ public StringToLongConverter(String errorMessage) {
+ super(errorMessage);
+ }
+
+ /**
* Returns the format used by {@link #convertToPresentation(Long, Locale)}
* and {@link #convertToModel(String, Locale)}.
*
@@ -53,9 +63,14 @@ public class StringToLongConverter
@Override
public Result<Long> convertToModel(String value, Locale locale) {
- Number n = convertToNumber(value, locale);
- return n == null ? Result.ok(null) : Result.ok(n.longValue());
-
+ Result<Number> n = convertToNumber(value, locale);
+ return n.map(number -> {
+ if (number == null) {
+ return null;
+ } else {
+ return number.longValue();
+ }
+ });
}
}
diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java
index 7414c0b1e7..0fcefe0a27 100644
--- a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java
+++ b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java
@@ -109,7 +109,8 @@ public class DesignFormatter implements Serializable {
final DecimalFormat fmt = new DecimalFormat("0.###", symbols);
fmt.setGroupingUsed(false);
- Converter<String, ?> floatConverter = new StringToFloatConverter() {
+ Converter<String, ?> floatConverter = new StringToFloatConverter(
+ "Error converting value") {
@Override
protected NumberFormat getFormat(Locale locale) {
return fmt;
@@ -118,7 +119,8 @@ public class DesignFormatter implements Serializable {
converterMap.put(Float.class, floatConverter);
converterMap.put(float.class, floatConverter);
- Converter<String, ?> doubleConverter = new StringToDoubleConverter() {
+ Converter<String, ?> doubleConverter = new StringToDoubleConverter(
+ "Error converting value") {
@Override
protected NumberFormat getFormat(Locale locale) {
return fmt;
@@ -130,12 +132,13 @@ public class DesignFormatter implements Serializable {
final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols);
bigDecimalFmt.setGroupingUsed(false);
bigDecimalFmt.setParseBigDecimal(true);
- converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() {
- @Override
- protected NumberFormat getFormat(Locale locale) {
- return bigDecimalFmt;
- };
- });
+ converterMap.put(BigDecimal.class,
+ new StringToBigDecimalConverter("Error converting value") {
+ @Override
+ protected NumberFormat getFormat(Locale locale) {
+ return bigDecimalFmt;
+ };
+ });
// strings do nothing
converterMap.put(String.class, new Converter<String, String>() {