-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add "validator" package with all classes inside it Part of Binder task. * Add BigDecimalRangeValidator * Add BigIntegerRangeValidator * Add ByteRangeValidator * Add DateRangeValidator * Add DateTimeRangeValidator * Add DoubleRangeValidator * Add EmailValidator * Add FloatRangeValidator * Add IntegerRangeValidator * Add LongRangeValidator * Add RangeValidator * Add RegexpValidator * Add ShortRangeValidator * Add StringLengthValidator * Correct java import * Correct javadoc * Sonar Qube fixes.
- Loading branch information
Denis
authored
Aug 25, 2017
1 parent
9ed8a02
commit e37833f
Showing
15 changed files
with
1,076 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...nt/flow-components-commons/src/main/java/com/vaadin/data/validator/AbstractValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2000-2017 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.ValidationResult; | ||
import com.vaadin.data.Validator; | ||
import com.vaadin.server.SerializableFunction; | ||
|
||
/** | ||
* 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 final SerializableFunction<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(Object)}. | ||
* <p> | ||
* For instance, the following {@code apply} method only accepts even | ||
* numbers: | ||
* | ||
* <pre> | ||
* @Override | ||
* public Result<T> 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 ValidationResult toResult(T value, boolean isValid) { | ||
return isValid ? ValidationResult.ok() | ||
: ValidationResult.error(getMessage(value)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...-components-commons/src/main/java/com/vaadin/data/validator/BigDecimalRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2000-2017 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); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...-components-commons/src/main/java/com/vaadin/data/validator/BigIntegerRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2000-2017 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); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...t/flow-components-commons/src/main/java/com/vaadin/data/validator/ByteRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2000-2017 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); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...t/flow-components-commons/src/main/java/com/vaadin/data/validator/DateRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2000-2017 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.time.LocalDate; | ||
import java.util.Comparator; | ||
|
||
/** | ||
* Validator for validating that a {@link LocalDate} is inside a given range. | ||
* | ||
* @author Vaadin Ltd. | ||
* @since 8.0 | ||
*/ | ||
public class DateRangeValidator extends RangeValidator<LocalDate> { | ||
|
||
/** | ||
* Creates a validator for checking that a LocalDate 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> | ||
* | ||
* @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, LocalDate minValue, | ||
LocalDate maxValue) { | ||
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ow-components-commons/src/main/java/com/vaadin/data/validator/DateTimeRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2000-2017 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.time.LocalDateTime; | ||
import java.util.Comparator; | ||
|
||
/** | ||
* Validator for validating that a {@link LocalDateTime} is inside a given | ||
* range. | ||
* | ||
* @author Vaadin Ltd. | ||
* @since 8.0 | ||
*/ | ||
public class DateTimeRangeValidator extends RangeValidator<LocalDateTime> { | ||
|
||
/** | ||
* Creates a validator for checking that a {@link LocalDateTime} 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> | ||
* | ||
* @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 DateTimeRangeValidator(String errorMessage, LocalDateTime minValue, | ||
LocalDateTime maxValue) { | ||
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...flow-components-commons/src/main/java/com/vaadin/data/validator/DoubleRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2000-2017 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); | ||
} | ||
|
||
} |
Oops, something went wrong.