summaryrefslogtreecommitdiffstats
path: root/server/src
diff options
context:
space:
mode:
authorAhmed Ashour <asashour@yahoo.com>2017-10-20 10:54:25 +0200
committerPéter Török <31210544+torok-peter@users.noreply.github.com>2017-10-20 11:54:25 +0300
commit3929d0ac6f8e3d7370412d1d181db1b2361d13e3 (patch)
treee59252649924608b756d2dbfac8ad3fb116cec7b /server/src
parentafb9d3b6c1d143b59506e08c3f27eb088555801c (diff)
downloadvaadin-framework-3929d0ac6f8e3d7370412d1d181db1b2361d13e3.tar.gz
vaadin-framework-3929d0ac6f8e3d7370412d1d181db1b2361d13e3.zip
AbstractDateField.formatDate() to be abstract (#10186)
* AbstractDateField.formatDate to be abstract Remove invalidDateString paramter, as it can be deduced * Fix test * Remove AbstractDateField.getResolutionVariable VAbstractTextualDate: rename updateDateVariables() to updateBufferedResolutions() * Revert to use fixed value of MONDAY. * release notes * updateAndSendBufferedValues() * Missed call to updateAndSendBufferedValues(); * release note
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractComponent.java2
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractDateField.java106
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java5
3 files changed, 60 insertions, 53 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractComponent.java b/server/src/main/java/com/vaadin/ui/AbstractComponent.java
index 88b42d5757..7a87230125 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractComponent.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractComponent.java
@@ -91,7 +91,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
/**
* The internal error message of the component.
*/
- private ErrorMessage componentError = null;
+ private ErrorMessage componentError;
/**
* Locale of this component.
diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
index 67067bd070..965c2f652a 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
@@ -77,10 +77,9 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
private AbstractDateFieldServerRpc rpc = new AbstractDateFieldServerRpc() {
@Override
- public void update(String newDateString, boolean invalidDateString,
+ public void update(String newDateString,
Map<String, Integer> resolutions) {
- Set<String> resolutionNames = getResolutions()
- .map(AbstractDateField.this::getResolutionVariable)
+ Set<String> resolutionNames = getResolutions().map(Enum::name)
.collect(Collectors.toSet());
resolutionNames.retainAll(resolutions.keySet());
if (!isReadOnly()
@@ -109,7 +108,8 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
if (newDateString == null || newDateString.isEmpty()) {
setValue(newDate, true);
} else {
- if (invalidDateString) {
+ // invalid date string
+ if (resolutions.isEmpty()) {
Result<T> parsedDate = handleUnparsableDateString(
dateString);
parsedDate.ifOk(v -> setValue(v, true));
@@ -173,7 +173,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
/* Constructors */
/**
- * Constructs an empty <code>AbstractDateField</code> with no caption and
+ * Constructs an empty {@code AbstractDateField} with no caption and
* specified {@code resolution}.
*
* @param resolution
@@ -185,10 +185,10 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
}
/**
- * Constructs an empty <code>AbstractDateField</code> with caption.
+ * Constructs an empty {@code AbstractDateField} with caption.
*
* @param caption
- * the caption of the datefield.
+ * the caption of the datefield
* @param resolution
* initial resolution for the field, not {@code null}
*/
@@ -198,11 +198,11 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
}
/**
- * Constructs a new <code>AbstractDateField</code> with the given caption
- * and initial text contents.
+ * Constructs a new {@code AbstractDateField} with the given caption and
+ * initial text contents.
*
* @param caption
- * the caption <code>String</code> for the editor.
+ * the caption {@code String} for the editor.
* @param value
* the date/time value.
* @param resolution
@@ -227,24 +227,31 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
* Construct a date object from the individual field values received from
* the client.
*
+ * @param resolutions
+ * map of time unit (resolution) name and value, the key is the
+ * resolution name e.g. "HOUR", "MINUTE", the value can be
+ * {@code null}
+ * @param oldDate
+ * used as a fallback to get needed values if they are not
+ * defined in the specified {@code resolutions}
+ *
+ * @return the date object built from the specified resolutions
* @since
*/
- protected T reconstructDateFromFields(Map<String, Integer> variables,
+ protected T reconstructDateFromFields(Map<String, Integer> resolutions,
T oldDate) {
Map<R, Integer> calendarFields = new HashMap<>();
for (R resolution : getResolutionsHigherOrEqualTo(getResolution())) {
// Only handle what the client is allowed to send. The same
// resolutions that are painted
- String variableName = getResolutionVariable(resolution);
+ String resolutionName = resolution.name();
- Integer newValue = variables.get(variableName);
- if (newValue != null) {
- calendarFields.put(resolution, newValue);
- } else {
- calendarFields.put(resolution,
- getDatePart(oldDate, resolution));
+ Integer newValue = resolutions.get(resolutionName);
+ if (newValue == null) {
+ newValue = getDatePart(oldDate, resolution);
}
+ calendarFields.put(resolution, newValue);
}
return buildDate(calendarFields);
}
@@ -252,8 +259,8 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
/**
* Sets the start range for this component. If the value is set before this
* date (taking the resolution into account), the component will not
- * validate. If <code>startDate</code> is set to <code>null</code>, any
- * value before <code>endDate</code> will be accepted by the range
+ * validate. If {@code startDate} is set to {@code null}, any value before
+ * {@code endDate} will be accepted by the range
*
* @param startDate
* - the allowed range's start date
@@ -315,12 +322,12 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
/**
* Sets the end range for this component. If the value is set after this
* date (taking the resolution into account), the component will not
- * validate. If <code>endDate</code> is set to <code>null</code>, any value
- * after <code>startDate</code> will be accepted by the range.
+ * validate. If {@code endDate} is set to {@code null}, any value after
+ * {@code startDate} will be accepted by the range.
*
* @param endDate
- * - the allowed range's end date (inclusive, based on the
- * current resolution)
+ * the allowed range's end date (inclusive, based on the current
+ * resolution)
*/
public void setRangeEnd(T endDate) {
Date date = convertToDate(endDate);
@@ -336,7 +343,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
/**
* Returns the precise rangeStart used.
*
- * @return the precise rangeStart used, may be null.
+ * @return the precise rangeStart used, may be {@code null}.
*/
public T getRangeStart() {
return convertFromDate(getState(false).rangeStart);
@@ -345,7 +352,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
/**
* Returns the precise rangeEnd used.
*
- * @return the precise rangeEnd used, may be null.
+ * @return the precise rangeEnd used, may be {@code null}.
*/
public T getRangeEnd() {
return convertFromDate(getState(false).rangeEnd);
@@ -423,14 +430,14 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
// Only paint variables for the resolution and up, e.g. Resolution DAY
// paints DAY,MONTH,YEAR
- for (R res : getResolutionsHigherOrEqualTo(getResolution())) {
- String variableName = getResolutionVariable(res);
+ for (R resolution : getResolutionsHigherOrEqualTo(getResolution())) {
+ String resolutionName = resolution.name();
- Integer value = getValuePart(currentDate, res);
- resolutions.put(variableName, value);
+ Integer value = getValuePart(currentDate, resolution);
+ resolutions.put(resolutionName, value);
- Integer defaultValuePart = getValuePart(defaultValue, res);
- resolutions.put("default-" + variableName, defaultValuePart);
+ Integer defaultValuePart = getValuePart(defaultValue, resolution);
+ resolutions.put("default-" + resolutionName, defaultValuePart);
}
}
@@ -501,6 +508,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
* default value is set, current date/time is used.
*
* @param defaultValue
+ * the default value, may be {@code null}
* @since 8.1.2
*/
public void setDefaultValue(T defaultValue) {
@@ -581,9 +589,11 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
* {@link #handleUnparsableDateString(String)} method is overridden, the
* localized message from its exception is used.
*
+ * @param parsingErrorMessage
+ * the default parsing error message
+ *
* @see #getParseErrorMessage()
* @see #handleUnparsableDateString(String)
- * @param parsingErrorMessage
*/
public void setParseErrorMessage(String parsingErrorMessage) {
defaultParseErrorMessage = parsingErrorMessage;
@@ -628,8 +638,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
}
/**
- * Formats date according to the components locale. To be reimplemented in
- * subclasses.
+ * Formats date according to the components locale.
*
* @param value
* the date or {@code null}
@@ -637,9 +646,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
* {@code null}
* @since 8.1.1
*/
- protected String formatDate(T value) {
- return Objects.toString(value, "");
- }
+ protected abstract String formatDate(T value);
@Override
public void writeDesign(Element design, DesignContext designContext) {
@@ -684,11 +691,10 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
this.value = value;
// Also set the internal dateString
- if (value != null) {
- dateString = formatDate(value);
- } else {
- dateString = formatDate(getEmptyValue());
+ if (value == null) {
+ value = getEmptyValue();
}
+ dateString = formatDate(value);
RangeValidator<T> validator = getRangeValidator();// TODO move range
// check to internal
// validator?
@@ -717,9 +723,10 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
* given {@code resolution}.
*
* @param date
- * the given date
+ * the given date, can be {@code null}
* @param resolution
- * the resolution to extract a value from the date by
+ * the resolution to extract a value from the date by, not
+ * {@code null}
* @return the integer value part of the date by the given resolution
*/
protected abstract int getDatePart(T date, R resolution);
@@ -764,10 +771,6 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
*/
protected abstract Date convertToDate(T date);
- private String getResolutionVariable(R resolution) {
- return resolution.name();
- }
-
@SuppressWarnings("unchecked")
private Stream<R> getResolutions() {
Type resolutionType = GenericTypeReflector.getTypeParameter(getClass(),
@@ -776,11 +779,10 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
Class<?> clazz = (Class<?>) resolutionType;
return Stream.of(clazz.getEnumConstants())
.map(object -> (R) object);
- } else {
- throw new RuntimeException("Cannot detect resoluton type "
- + Optional.ofNullable(resolutionType).map(Type::getTypeName)
- .orElse(null));
}
+ throw new RuntimeException("Cannot detect resoluton type "
+ + Optional.ofNullable(resolutionType).map(Type::getTypeName)
+ .orElse(null));
}
private Iterable<R> getResolutionsHigherOrEqualTo(R resoution) {
diff --git a/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java b/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java
index 4edc2a30df..eea1e33137 100644
--- a/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java
+++ b/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java
@@ -50,6 +50,11 @@ public class DateFieldListenersTest extends AbstractListenerMethodsTestBase {
protected Date convertToDate(LocalDateTime date) {
return null;
}
+
+ @Override
+ protected String formatDate(LocalDateTime value) {
+ return null;
+ }
}
@Test