aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin
diff options
context:
space:
mode:
authorDenis Anisimov <denis@vaadin.com>2016-08-09 16:47:56 +0300
committerVaadin Code Review <review@vaadin.com>2016-08-11 12:04:10 +0000
commit81b849c1af199be481e00c86ca324cfaffe8a7a0 (patch)
treec6387ced1dc828134c7766e1bdbc859ed9ba0668 /server/src/main/java/com/vaadin
parent5c515c1680d053d9087c2dc7783a6b8ec181f9b8 (diff)
downloadvaadin-framework-81b849c1af199be481e00c86ca324cfaffe8a7a0.tar.gz
vaadin-framework-81b849c1af199be481e00c86ca324cfaffe8a7a0.zip
Convert old validators (#87).
Change-Id: I6e4a56855f78595975b645a08390fb56e0e52ef9
Diffstat (limited to 'server/src/main/java/com/vaadin')
-rw-r--r--server/src/main/java/com/vaadin/data/validator/AbstractValidator.java85
-rw-r--r--server/src/main/java/com/vaadin/data/validator/BeanValidator.java195
-rw-r--r--server/src/main/java/com/vaadin/data/validator/BigDecimalRangeValidator.java51
-rw-r--r--server/src/main/java/com/vaadin/data/validator/BigIntegerRangeValidator.java51
-rw-r--r--server/src/main/java/com/vaadin/data/validator/ByteRangeValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/DateRangeValidator.java62
-rw-r--r--server/src/main/java/com/vaadin/data/validator/DoubleRangeValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/EmailValidator.java44
-rw-r--r--server/src/main/java/com/vaadin/data/validator/FloatRangeValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/IntegerRangeValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/LongRangeValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/NotNullValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/RangeValidator.java224
-rw-r--r--server/src/main/java/com/vaadin/data/validator/RegexpValidator.java115
-rw-r--r--server/src/main/java/com/vaadin/data/validator/ShortRangeValidator.java49
-rw-r--r--server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java104
16 files changed, 1274 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java b/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java
new file mode 100644
index 0000000000..276a991b62
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Objects;
+import java.util.function.Function;
+
+import com.vaadin.data.Result;
+import com.vaadin.data.Validator;
+
+/**
+ * An abstract base class for typed validators.
+ *
+ * @param <T>
+ * The value type
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+public abstract class AbstractValidator<T> implements Validator<T> {
+
+ private Function<T, String> messageProvider;
+
+ /**
+ * Constructs a validator with the given error message. The substring "{0}"
+ * is replaced by the value that failed validation.
+ *
+ * @param errorMessage
+ * the message to be included in a failed result, not null
+ */
+ protected AbstractValidator(String errorMessage) {
+ Objects.requireNonNull(errorMessage, "error message cannot be null");
+ this.messageProvider = value -> errorMessage.replace("{0}",
+ String.valueOf(value));
+ }
+
+ /**
+ * Returns the error message for the given value.
+ *
+ * @param value
+ * an invalid value
+ * @return the formatted error message
+ */
+ protected String getMessage(T value) {
+ return messageProvider.apply(value);
+ }
+
+ /**
+ * A helper method for creating a {@code Result} from a value and a validity
+ * flag. If the flag is true, returns {@code Result.ok}, otherwise yields
+ * {@code Result.error} bearing the error message returned by
+ * {@link #getMessage(T)}.
+ * <p>
+ * For instance, the following {@code apply} method only accepts even
+ * numbers:
+ *
+ * <pre>
+ * &#64;Override
+ * public Result&lt;T&gt; apply(Integer value) {
+ * return toResult(value, value % 2 == 0);
+ * }
+ * </pre>
+ *
+ * @param value
+ * the validated value
+ * @param isValid
+ * whether the value is valid or not
+ * @return the validation result
+ */
+ protected Result<T> toResult(T value, boolean isValid) {
+ return isValid ? Result.ok(value) : Result.error(getMessage(value));
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java
new file mode 100644
index 0000000000..0c09c89371
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.data.validator;
+
+import java.util.Locale;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.BinaryOperator;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.MessageInterpolator.Context;
+import javax.validation.Validation;
+import javax.validation.ValidatorFactory;
+import javax.validation.metadata.ConstraintDescriptor;
+
+import com.vaadin.data.Result;
+import com.vaadin.data.Validator;
+
+/**
+ * A {@code Validator} using the JSR-303 (javax.validation) annotation-based
+ * bean validation mechanism. Values passed to this validator are compared
+ * against the constraints, if any, specified by annotations on the
+ * corresponding bean property.
+ * <p>
+ * Note that a JSR-303 implementation (e.g. Hibernate Validator or Apache Bean
+ * Validation - formerly agimatec validation) must be present on the project
+ * classpath when using bean validation.
+ *
+ * @author Petri Hakala
+ * @author Vaadin Ltd.
+ *
+ * @since 8.0
+ */
+public class BeanValidator implements Validator<Object> {
+
+ private static final long serialVersionUID = 1L;
+ private static ValidatorFactory factory;
+
+ private String propertyName;
+ private Class<?> beanType;
+ private Locale locale;
+
+ /**
+ * Creates a new JSR-303 {@code BeanValidator} that validates values of the
+ * specified property. Localizes validation messages using the
+ * {@linkplain Locale#getDefault() default locale}.
+ *
+ * @param beanType
+ * the bean type declaring the property, not null
+ * @param propertyName
+ * the property to validate, not null
+ */
+ public BeanValidator(Class<?> beanType, String propertyName) {
+ this(beanType, propertyName, Locale.getDefault());
+ }
+
+ /**
+ * Creates a new JSR-303 {@code BeanValidator} that validates values of the
+ * specified property. Localizes validation messages using the given locale.
+ *
+ * @param beanType
+ * the bean class declaring the property, not null
+ * @param propertyName
+ * the property to validate, not null
+ * @param locale
+ * the locale to use, not null
+ */
+ public BeanValidator(Class<?> beanType, String propertyName,
+ Locale locale) {
+ Objects.requireNonNull(beanType, "bean class cannot be null");
+ Objects.requireNonNull(propertyName, "property name cannot be null");
+ this.beanType = beanType;
+ this.propertyName = propertyName;
+ setLocale(locale);
+ }
+
+ /**
+ * Validates the given value as if it were the value of the bean property
+ * configured for this validator. Returns {@code Result.ok} if there are no
+ * JSR-303 constraint violations, a {@code Result.error} of chained
+ * constraint violation messages otherwise.
+ * <p>
+ * Null values are accepted unless the property has an {@code @NotNull}
+ * annotation or equivalent.
+ */
+ @Override
+ public Result<Object> apply(final Object value) {
+ Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator()
+ .validateValue(beanType, propertyName, value);
+
+ BinaryOperator<Result<Object>> accumulator = (result1,
+ result2) -> result1.flatMap(val -> result2);
+
+ return violations.stream().map(v -> Result.error(getMessage(v)))
+ .reduce(Result.ok(value), accumulator);
+ }
+
+ /**
+ * Returns the locale used for validation error messages.
+ *
+ * @return the locale used for error messages
+ */
+ public Locale getLocale() {
+ return locale;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s[%s.%s]", getClass().getSimpleName(),
+ beanType.getSimpleName(), propertyName);
+ }
+
+ /**
+ * Returns the underlying JSR-303 bean validator factory used. A factory is
+ * created using {@link Validation} if necessary.
+ *
+ * @return the validator factory to use
+ */
+ protected static ValidatorFactory getJavaxBeanValidatorFactory() {
+ if (factory == null) {
+ factory = Validation.buildDefaultValidatorFactory();
+ }
+ return factory;
+ }
+
+ /**
+ * Returns a shared JSR-303 validator instance to use.
+ *
+ * @return the validator to use
+ */
+ protected javax.validation.Validator getJavaxBeanValidator() {
+ return getJavaxBeanValidatorFactory().getValidator();
+ }
+
+ /**
+ * Returns the interpolated error message for the given constraint violation
+ * using the locale specified for this validator.
+ *
+ * @param v
+ * the constraint violation
+ * @return the localized error message
+ */
+ protected String getMessage(ConstraintViolation<?> v) {
+ return getJavaxBeanValidatorFactory().getMessageInterpolator()
+ .interpolate(v.getMessageTemplate(), createContext(v), locale);
+ }
+
+ /**
+ * Creates a simple message interpolation context based on the given
+ * constraint violation.
+ *
+ * @param v
+ * the constraint violation
+ * @return the message interpolation context
+ */
+ protected Context createContext(ConstraintViolation<?> v) {
+ return new Context() {
+ @Override
+ public ConstraintDescriptor<?> getConstraintDescriptor() {
+ return v.getConstraintDescriptor();
+ }
+
+ @Override
+ public Object getValidatedValue() {
+ return v.getInvalidValue();
+ }
+ };
+ }
+
+ /**
+ * Sets the locale used for validation error messages. Revalidation is not
+ * automatically triggered by setting the locale.
+ *
+ * @param locale
+ * the locale to use for error messages, not null
+ */
+ private void setLocale(Locale locale) {
+ Objects.requireNonNull(locale, "locale cannot be null");
+ this.locale = locale;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/BigDecimalRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/BigDecimalRangeValidator.java
new file mode 100644
index 0000000000..0bdbd70356
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/BigDecimalRangeValidator.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.math.BigDecimal;
+import java.util.Comparator;
+
+/**
+ * Validator for validating that an {@link BigDecimal} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class BigDecimalRangeValidator extends RangeValidator<BigDecimal> {
+
+ /**
+ * Creates a validator for checking that an BigDecimal is within a given
+ * range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public BigDecimalRangeValidator(String errorMessage, BigDecimal minValue,
+ BigDecimal maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/BigIntegerRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/BigIntegerRangeValidator.java
new file mode 100644
index 0000000000..87ab5605a7
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/BigIntegerRangeValidator.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.math.BigInteger;
+import java.util.Comparator;
+
+/**
+ * Validator for validating that an {@link BigInteger} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class BigIntegerRangeValidator extends RangeValidator<BigInteger> {
+
+ /**
+ * Creates a validator for checking that an BigInteger is within a given
+ * range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public BigIntegerRangeValidator(String errorMessage, BigInteger minValue,
+ BigInteger maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/ByteRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/ByteRangeValidator.java
new file mode 100644
index 0000000000..fd11fc6aa4
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/ByteRangeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+
+/**
+ * Validator for validating that an {@link Byte} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class ByteRangeValidator extends RangeValidator<Byte> {
+
+ /**
+ * Creates a validator for checking that an Byte is within a given range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public ByteRangeValidator(String errorMessage, Byte minValue,
+ Byte maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/DateRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/DateRangeValidator.java
new file mode 100644
index 0000000000..43164d0fe2
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/DateRangeValidator.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+import java.util.Date;
+
+import com.vaadin.shared.ui.datefield.Resolution;
+
+/**
+ * Validator for validating that a Date is inside a given range.
+ *
+ * <p>
+ * Note that the comparison is done directly on the Date object so take care
+ * that the hours/minutes/seconds/milliseconds of the min/max values are
+ * properly set.
+ * </p>
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+public class DateRangeValidator extends RangeValidator<Date> {
+
+ /**
+ * Creates a validator for checking that an Date is within a given range.
+ * <p>
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ * </p>
+ * <p>
+ * Note that the comparison is done directly on the Date object so take care
+ * that the hours/minutes/seconds/milliseconds of the min/max values are
+ * properly set.
+ * </p>
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public DateRangeValidator(String errorMessage, Date minValue, Date maxValue,
+ Resolution resolution) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/DoubleRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/DoubleRangeValidator.java
new file mode 100644
index 0000000000..aa02ba9fe4
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/DoubleRangeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+
+/**
+ * Validator for validating that a {@link Double} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class DoubleRangeValidator extends RangeValidator<Double> {
+
+ /**
+ * Creates a validator for checking that an Double is within a given range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public DoubleRangeValidator(String errorMessage, Double minValue,
+ Double maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/EmailValidator.java b/server/src/main/java/com/vaadin/data/validator/EmailValidator.java
new file mode 100644
index 0000000000..44243b0712
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/EmailValidator.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+/**
+ * A string validator for e-mail addresses. The e-mail address syntax is not
+ * complete according to RFC 822 but handles the vast majority of valid e-mail
+ * addresses correctly.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class EmailValidator extends RegexpValidator {
+
+ private static final String PATTERN = "^" + "([a-zA-Z0-9_\\.\\-+])+" // local
+ + "@" + "[a-zA-Z0-9-.]+" // domain
+ + "\\." + "[a-zA-Z0-9-]{2,}" // tld
+ + "$";
+
+ /**
+ * Creates a validator for checking that a string is a syntactically valid
+ * e-mail address.
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ */
+ public EmailValidator(String errorMessage) {
+ super(errorMessage, PATTERN, true);
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/FloatRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/FloatRangeValidator.java
new file mode 100644
index 0000000000..ef57900f2f
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/FloatRangeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+
+/**
+ * Validator for validating that a {@link Float} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class FloatRangeValidator extends RangeValidator<Float> {
+
+ /**
+ * Creates a validator for checking that an Float is within a given range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public FloatRangeValidator(String errorMessage, Float minValue,
+ Float maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/IntegerRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/IntegerRangeValidator.java
new file mode 100644
index 0000000000..c7dc7cbee9
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/IntegerRangeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+
+/**
+ * Validator for validating that an {@link Integer} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class IntegerRangeValidator extends RangeValidator<Integer> {
+
+ /**
+ * Creates a validator for checking that an Integer is within a given range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public IntegerRangeValidator(String errorMessage, Integer minValue,
+ Integer maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/LongRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/LongRangeValidator.java
new file mode 100644
index 0000000000..85cdb907ac
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/LongRangeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+
+/**
+ * Validator for validating that an {@link Long} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class LongRangeValidator extends RangeValidator<Long> {
+
+ /**
+ * Creates a validator for checking that an Long is within a given range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public LongRangeValidator(String errorMessage, Long minValue,
+ Long maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java b/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java
new file mode 100644
index 0000000000..e44e086c0f
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.data.validator;
+
+import java.util.Objects;
+
+import com.vaadin.data.Result;
+
+/**
+ * This validator is used for validating properties that do not allow null
+ * values.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class NotNullValidator extends AbstractValidator<String> {
+
+ /**
+ * Creates a new NullValidator.
+ *
+ * @param errorMessage
+ * the error message to display on invalidation.
+ */
+ public NotNullValidator(String errorMessage) {
+ super(errorMessage);
+ }
+
+ @Override
+ public Result<String> apply(String value) {
+ return Objects.isNull(value) ? Result.error(getMessage(value))
+ : Result.ok(value);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/RangeValidator.java b/server/src/main/java/com/vaadin/data/validator/RangeValidator.java
new file mode 100644
index 0000000000..eed79d0b8b
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/RangeValidator.java
@@ -0,0 +1,224 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+import java.util.Objects;
+
+import com.vaadin.data.Result;
+
+/**
+ * Verifies that a value is within the given range.
+ *
+ * @param <T>
+ * the type to validate
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+public class RangeValidator<T> extends AbstractValidator<T> {
+
+ private T minValue = null;
+ private T maxValue = null;
+ private boolean minValueIncluded = true;
+ private boolean maxValueIncluded = true;
+ private Comparator<? super T> comparator;
+
+ /**
+ * Creates a new range validator of the given type. Passing null to either
+ * {@code minValue} or {@code maxValue} means there is no limit in that
+ * direction. Both limits may be null; this can be useful if the limits are
+ * resolved programmatically. The result of passing null to {@code apply}
+ * depends on the given comparator.
+ *
+ * @param errorMessage
+ * the error message to return if validation fails, not null
+ * @param comparator
+ * the comparator to compare with, not null
+ * @param minValue
+ * the least value of the accepted range or null for no limit
+ * @param maxValue
+ * the greatest value of the accepted range or null for no limit
+ */
+ public RangeValidator(String errorMessage, Comparator<? super T> comparator,
+ T minValue, T maxValue) {
+ super(errorMessage);
+ Objects.requireNonNull(comparator, "comparator cannot be null");
+
+ this.minValue = minValue;
+ this.maxValue = maxValue;
+ this.minValueIncluded = minValue != null;
+ this.maxValueIncluded = maxValue != null;
+ this.comparator = comparator;
+ }
+
+ /**
+ * Returns a {@code RangeValidator} comparing values of a {@code Comparable}
+ * type using their <i>natural order</i>. Passing null to either
+ * {@code minValue} or {@code maxValue} means there is no limit in that
+ * direction. Both limits may be null; this can be useful if the limits are
+ * resolved programmatically.
+ * <p>
+ * Null is considered to be less than any non-null value. This means null
+ * never passes validation if a minimum value is specified.
+ *
+ * @param <C>
+ * the {@code Comparable} value type
+ * @param errorMessage
+ * the error message to return if validation fails, not null
+ * @param minValue
+ * the least value of the accepted range or null for no limit
+ * @param maxValue
+ * the greatest value of the accepted range or null for no limit
+ * @return the new validator
+ */
+ public static <C extends Comparable<? super C>> RangeValidator<C> of(
+ String errorMessage, C minValue, C maxValue) {
+ return new RangeValidator<>(errorMessage,
+ Comparator.nullsFirst(Comparator.naturalOrder()), minValue,
+ maxValue);
+ }
+
+ /**
+ * Returns {@code Result.ok} if the value is within the specified bounds,
+ * {@code Result.error} otherwise. If null is passed to {@code apply}, the
+ * behavior depends on the used comparator.
+ */
+ @Override
+ public Result<T> apply(T value) {
+ return toResult(value, isValid(value));
+ }
+
+ /**
+ * Returns whether the minimum value is part of the accepted range.
+ *
+ * @return true if the minimum value is part of the range, false otherwise
+ */
+ public boolean isMinValueIncluded() {
+ return minValueIncluded;
+ }
+
+ /**
+ * Sets whether the minimum value is part of the accepted range.
+ *
+ * @param minValueIncluded
+ * true if the minimum value should be part of the range, false
+ * otherwise
+ */
+ public void setMinValueIncluded(boolean minValueIncluded) {
+ this.minValueIncluded = minValueIncluded;
+ }
+
+ /**
+ * Returns whether the maximum value is part of the accepted range.
+ *
+ * @return true if the maximum value is part of the range, false otherwise
+ */
+ public boolean isMaxValueIncluded() {
+ return maxValueIncluded;
+ }
+
+ /**
+ * Sets whether the maximum value is part of the accepted range.
+ *
+ * @param maxValueIncluded
+ * true if the maximum value should be part of the range, false
+ * otherwise
+ */
+ public void setMaxValueIncluded(boolean maxValueIncluded) {
+ this.maxValueIncluded = maxValueIncluded;
+ }
+
+ /**
+ * Returns the minimum value of the range.
+ *
+ * @return the minimum value
+ */
+ public T getMinValue() {
+ return minValue;
+ }
+
+ /**
+ * Sets the minimum value of the range. Use
+ * {@link #setMinValueIncluded(boolean)} to control whether this value is
+ * part of the range or not.
+ *
+ * @param minValue
+ * the minimum value
+ */
+ public void setMinValue(T minValue) {
+ this.minValue = minValue;
+ }
+
+ /**
+ * Gets the maximum value of the range.
+ *
+ * @return the maximum value
+ */
+ public T getMaxValue() {
+ return maxValue;
+ }
+
+ /**
+ * Sets the maximum value of the range. Use
+ * {@link #setMaxValueIncluded(boolean)} to control whether this value is
+ * part of the range or not.
+ *
+ * @param maxValue
+ * the maximum value
+ */
+ public void setMaxValue(T maxValue) {
+ this.maxValue = maxValue;
+ }
+
+ @Override
+ public String toString() {
+ T min = getMinValue();
+ T max = getMaxValue();
+ return String.format("%s %c%s, %s%c", getClass().getSimpleName(),
+ isMinValueIncluded() ? '[' : '(', min != null ? min : "-∞",
+ max != null ? max : "∞", isMaxValueIncluded() ? ']' : ')');
+ }
+
+ /**
+ * Returns whether the given value lies in the valid range.
+ *
+ * @param value
+ * the value to validate
+ * @return true if the value is valid, false otherwise
+ */
+ protected boolean isValid(T value) {
+ if (value == null) {
+ return true;
+ }
+ if (getMinValue() != null) {
+ int result = comparator.compare(value, getMinValue());
+ if (result < 0) {
+ return false;
+ } else if (result == 0 && !isMinValueIncluded()) {
+ return false;
+ }
+ }
+ if (getMaxValue() != null) {
+ int result = comparator.compare(value, getMaxValue());
+ if (result > 0) {
+ return false;
+ } else if (result == 0 && !isMaxValueIncluded()) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java b/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java
new file mode 100644
index 0000000000..ce72d79e74
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.vaadin.data.Result;
+
+/**
+ * A string validator comparing the string against a Java regular expression.
+ * Both complete matches and substring matches are supported.
+ * <p>
+ * For the Java regular expression syntax, see {@link java.util.regex.Pattern}.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class RegexpValidator extends AbstractValidator<String> {
+
+ private Pattern pattern;
+ private boolean complete;
+ private transient Matcher matcher = null;
+
+ /**
+ * Creates a validator for checking that the regular expression matches the
+ * complete string to validate.
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param regexp
+ * a Java regular expression
+ */
+ public RegexpValidator(String errorMessage, String regexp) {
+ this(errorMessage, regexp, true);
+ }
+
+ /**
+ * Creates a validator for checking that the regular expression matches the
+ * string to validate.
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param regexp
+ * a Java regular expression
+ * @param complete
+ * true to use check for a complete match, false to look for a
+ * matching substring
+ *
+ */
+ public RegexpValidator(String errorMessage, String regexp,
+ boolean complete) {
+ super(errorMessage);
+ pattern = Pattern.compile(regexp);
+ this.complete = complete;
+ }
+
+ @Override
+ public Result<String> apply(String value) {
+ return toResult(value, isValid(value));
+ }
+
+ @Override
+ public String toString() {
+ return "RegexpValidator[" + pattern + "]";
+ }
+
+ /**
+ * Returns whether the given string matches the regular expression.
+ *
+ * @param value
+ * the string to match
+ * @return true if the string matched, false otherwise
+ */
+ protected boolean isValid(String value) {
+ if (value == null) {
+ return true;
+ }
+ if (complete) {
+ return getMatcher(value).matches();
+ } else {
+ return getMatcher(value).find();
+ }
+ }
+
+ /**
+ * Returns a new or reused matcher for the pattern.
+ *
+ * @param value
+ * the string to find matches in
+ * @return a matcher for the string
+ */
+ private Matcher getMatcher(String value) {
+ if (matcher == null) {
+ matcher = pattern.matcher(value);
+ } else {
+ matcher.reset(value);
+ }
+ return matcher;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/ShortRangeValidator.java b/server/src/main/java/com/vaadin/data/validator/ShortRangeValidator.java
new file mode 100644
index 0000000000..4fee15bca0
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/ShortRangeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Comparator;
+
+/**
+ * Validator for validating that an {@link Short} is inside a given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class ShortRangeValidator extends RangeValidator<Short> {
+
+ /**
+ * Creates a validator for checking that an Short is within a given range.
+ *
+ * By default the range is inclusive i.e. both minValue and maxValue are
+ * valid values. Use {@link #setMinValueIncluded(boolean)} or
+ * {@link #setMaxValueIncluded(boolean)} to change it.
+ *
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ * @param minValue
+ * The minimum value to accept or null for no limit
+ * @param maxValue
+ * The maximum value to accept or null for no limit
+ */
+ public ShortRangeValidator(String errorMessage, Short minValue,
+ Short maxValue) {
+ super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java b/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java
new file mode 100644
index 0000000000..2e796cfdb2
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.data.validator;
+
+import com.vaadin.data.Result;
+
+/**
+ * Verifies that the length of a string is within the given range.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.0
+ */
+@SuppressWarnings("serial")
+public class StringLengthValidator extends AbstractValidator<String> {
+
+ private RangeValidator<Integer> validator;
+
+ /**
+ * Creates a new StringLengthValidator with a given error message and
+ * minimum and maximum length limits.
+ *
+ * @param errorMessage
+ * the error message to return if validation fails
+ * @param minLength
+ * the minimum permissible length of the string or null for no
+ * limit.
+ * @param maxLength
+ * the maximum permissible length of the string or null for no
+ * limit.
+ */
+ public StringLengthValidator(String errorMessage, Integer minLength,
+ Integer maxLength) {
+ super(errorMessage);
+ validator = RangeValidator.of(errorMessage, minLength, maxLength);
+ }
+
+ @Override
+ public Result<String> apply(String value) {
+ if (value == null) {
+ return toResult(value, true);
+ }
+ Result<?> lengthCheck = validator.apply(value.length());
+ return toResult(value, !lengthCheck.isError());
+ }
+
+ /**
+ * Gets the maximum permissible length of the string.
+ *
+ * @return the maximum length of the string or null if there is no limit
+ */
+ public Integer getMaxLength() {
+ return validator.getMaxValue();
+ }
+
+ /**
+ * Gets the minimum permissible length of the string.
+ *
+ * @return the minimum length of the string or null if there is no limit
+ */
+ public Integer getMinLength() {
+ return validator.getMinValue();
+ }
+
+ /**
+ * Sets the maximum permissible length of the string.
+ *
+ * @param maxLength
+ * the maximum length to accept or null for no limit
+ */
+ public void setMaxLength(Integer maxLength) {
+ validator.setMaxValue(maxLength);
+ }
+
+ /**
+ * Sets the minimum permissible length.
+ *
+ * @param minLength
+ * the minimum length to accept or null for no limit
+ */
+ public void setMinLength(Integer minLength) {
+ validator.setMaxValue(minLength);
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s[%d, %d]", getClass().getSimpleName(),
+ getMinLength(), getMaxLength());
+ }
+
+}