Repository: bval Updated Branches: refs/heads/bv2 eab5a0123 -> 0247c2361
handle threeten chrono* types by introducing Comparator to TimeValidator+ Project: http://git-wip-us.apache.org/repos/asf/bval/repo Commit: http://git-wip-us.apache.org/repos/asf/bval/commit/0247c236 Tree: http://git-wip-us.apache.org/repos/asf/bval/tree/0247c236 Diff: http://git-wip-us.apache.org/repos/asf/bval/diff/0247c236 Branch: refs/heads/bv2 Commit: 0247c23619dd1eaf5c4dc00b58e8303db192814f Parents: eab5a01 Author: Matt Benson <[email protected]> Authored: Thu Mar 22 15:28:46 2018 -0500 Committer: Matt Benson <[email protected]> Committed: Thu Mar 22 15:28:46 2018 -0500 ---------------------------------------------------------------------- .../constraints/FutureOrPresentValidator.java | 17 +++++-- .../bval/constraints/FutureValidator.java | 16 +++++-- .../constraints/FutureValidatorForCalendar.java | 48 -------------------- .../constraints/FutureValidatorForDate.java | 48 -------------------- .../constraints/PastOrPresentValidator.java | 16 +++++-- .../apache/bval/constraints/PastValidator.java | 16 +++++-- .../constraints/PastValidatorForCalendar.java | 48 -------------------- .../bval/constraints/PastValidatorForDate.java | 48 -------------------- .../apache/bval/constraints/TimeValidator.java | 25 ++++++++-- 9 files changed, 71 insertions(+), 211 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/FutureOrPresentValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureOrPresentValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/FutureOrPresentValidator.java index 4cc85b1..fc358b4 100644 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureOrPresentValidator.java +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/FutureOrPresentValidator.java @@ -33,9 +33,11 @@ import java.time.chrono.ChronoLocalDate; import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.ChronoZonedDateTime; import java.util.Calendar; +import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.function.Function; +import java.util.function.IntPredicate; import javax.validation.ConstraintValidator; import javax.validation.constraints.FutureOrPresent; @@ -72,14 +74,14 @@ public abstract class FutureOrPresentValidator<T extends Comparable<T>> extends public static class ForChronoLocalDate extends FutureOrPresentValidator<ChronoLocalDate> { public ForChronoLocalDate() { - super(LocalDate::now); + super(LocalDate::now, CHRONO_LOCAL_DATE_COMPARATOR); } } public static class ForChronoLocalDateTime extends FutureOrPresentValidator<ChronoLocalDateTime<?>> { public ForChronoLocalDateTime() { - super(LocalDateTime::now); + super(LocalDateTime::now, CHRONO_LOCAL_DATE_TIME_COMPARATOR); } } @@ -107,7 +109,7 @@ public abstract class FutureOrPresentValidator<T extends Comparable<T>> extends public static class ForChronoZonedDateTime extends FutureOrPresentValidator<ChronoZonedDateTime<?>> { public ForChronoZonedDateTime() { - super(ZonedDateTime::now); + super(ZonedDateTime::now, CHRONO_ZONED_DATE_TIME_COMPARATOR); } } @@ -132,7 +134,14 @@ public abstract class FutureOrPresentValidator<T extends Comparable<T>> extends } } + private static final IntPredicate TEST = n -> n >= 0; + protected FutureOrPresentValidator(Function<Clock, T> now) { - super(now, n -> n >= 0); + super(now, TEST); + } + + protected FutureOrPresentValidator(Function<Clock, T> now, Comparator<? super T> cmp) { + super(now, cmp, TEST); } + } http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidator.java index dd6116b..1362cae 100644 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidator.java +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidator.java @@ -33,9 +33,11 @@ import java.time.chrono.ChronoLocalDate; import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.ChronoZonedDateTime; import java.util.Calendar; +import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.function.Function; +import java.util.function.IntPredicate; import javax.validation.ConstraintValidator; import javax.validation.constraints.Future; @@ -72,14 +74,14 @@ public abstract class FutureValidator<T extends Comparable<T>> extends TimeValid public static class ForChronoLocalDate extends FutureValidator<ChronoLocalDate> { public ForChronoLocalDate() { - super(LocalDate::now); + super(LocalDate::now, CHRONO_LOCAL_DATE_COMPARATOR); } } public static class ForChronoLocalDateTime extends FutureValidator<ChronoLocalDateTime<?>> { public ForChronoLocalDateTime() { - super(LocalDateTime::now); + super(LocalDateTime::now, CHRONO_LOCAL_DATE_TIME_COMPARATOR); } } @@ -107,7 +109,7 @@ public abstract class FutureValidator<T extends Comparable<T>> extends TimeValid public static class ForChronoZonedDateTime extends FutureValidator<ChronoZonedDateTime<?>> { public ForChronoZonedDateTime() { - super(ZonedDateTime::now); + super(ZonedDateTime::now, CHRONO_ZONED_DATE_TIME_COMPARATOR); } } @@ -132,7 +134,13 @@ public abstract class FutureValidator<T extends Comparable<T>> extends TimeValid } } + private static final IntPredicate TEST = n -> n > 0; + protected FutureValidator(Function<Clock, T> now) { - super(now, n -> n > 0); + super(now, TEST); + } + + protected FutureValidator(Function<Clock, T> now, Comparator<T> cmp) { + super(now, cmp, TEST); } } http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForCalendar.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForCalendar.java b/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForCalendar.java deleted file mode 100644 index ecc50bb..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForCalendar.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.Future; -import java.util.Calendar; - -/** - * Description: validate a date or calendar representing a date in the future <br/> - */ -public class FutureValidatorForCalendar implements ConstraintValidator<Future, Calendar> { - - @Override - public void initialize(Future annotation) { - } - - @Override - public boolean isValid(Calendar cal, ConstraintValidatorContext context) { - return cal == null || cal.after(now()); - } - - /** - * overwrite when you need a different algorithm for 'now'. - * - * @return current date/time - */ - protected Calendar now() { - return Calendar.getInstance(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForDate.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForDate.java b/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForDate.java deleted file mode 100644 index a962f64..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/FutureValidatorForDate.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.Future; -import java.util.Date; - -/** - * Description: validate a date or calendar representing a date in the future <br/> - */ -public class FutureValidatorForDate implements ConstraintValidator<Future, Date> { - - @Override - public void initialize(Future annotation) { - } - - @Override - public boolean isValid(Date date, ConstraintValidatorContext context) { - return date == null || date.after(now()); - } - - /** - * overwrite when you need a different algorithm for 'now'. - * - * @return current date/time - */ - protected Date now() { - return new Date(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java index d1e3e19..c678827 100644 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java @@ -33,9 +33,11 @@ import java.time.chrono.ChronoLocalDate; import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.ChronoZonedDateTime; import java.util.Calendar; +import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.function.Function; +import java.util.function.IntPredicate; import javax.validation.ConstraintValidator; import javax.validation.constraints.PastOrPresent; @@ -72,14 +74,14 @@ public abstract class PastOrPresentValidator<T extends Comparable<T>> extends Ti public static class ForChronoLocalDate extends PastOrPresentValidator<ChronoLocalDate> { public ForChronoLocalDate() { - super(LocalDate::now); + super(LocalDate::now, CHRONO_LOCAL_DATE_COMPARATOR); } } public static class ForChronoLocalDateTime extends PastOrPresentValidator<ChronoLocalDateTime<?>> { public ForChronoLocalDateTime() { - super(LocalDateTime::now); + super(LocalDateTime::now, CHRONO_LOCAL_DATE_TIME_COMPARATOR); } } @@ -107,7 +109,7 @@ public abstract class PastOrPresentValidator<T extends Comparable<T>> extends Ti public static class ForChronoZonedDateTime extends PastOrPresentValidator<ChronoZonedDateTime<?>> { public ForChronoZonedDateTime() { - super(ZonedDateTime::now); + super(ZonedDateTime::now, CHRONO_ZONED_DATE_TIME_COMPARATOR); } } @@ -132,7 +134,13 @@ public abstract class PastOrPresentValidator<T extends Comparable<T>> extends Ti } } + private static final IntPredicate TEST = n -> n <= 0; + protected PastOrPresentValidator(Function<Clock, T> now) { - super(now, n -> n <= 0); + super(now, TEST); + } + + protected PastOrPresentValidator(Function<Clock, T> now, Comparator<? super T> cmp) { + super(now, cmp, TEST); } } http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java index 0136d83..ae61fcc 100644 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java @@ -33,9 +33,11 @@ import java.time.chrono.ChronoLocalDate; import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.ChronoZonedDateTime; import java.util.Calendar; +import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.function.Function; +import java.util.function.IntPredicate; import javax.validation.ConstraintValidator; import javax.validation.constraints.Past; @@ -72,14 +74,14 @@ public abstract class PastValidator<T extends Comparable<T>> extends TimeValidat public static class ForChronoLocalDate extends PastValidator<ChronoLocalDate> { public ForChronoLocalDate() { - super(LocalDate::now); + super(LocalDate::now, CHRONO_LOCAL_DATE_COMPARATOR); } } public static class ForChronoLocalDateTime extends PastValidator<ChronoLocalDateTime<?>> { public ForChronoLocalDateTime() { - super(LocalDateTime::now); + super(LocalDateTime::now, CHRONO_LOCAL_DATE_TIME_COMPARATOR); } } @@ -107,7 +109,7 @@ public abstract class PastValidator<T extends Comparable<T>> extends TimeValidat public static class ForChronoZonedDateTime extends PastValidator<ChronoZonedDateTime<?>> { public ForChronoZonedDateTime() { - super(ZonedDateTime::now); + super(ZonedDateTime::now, PastValidator.CHRONO_ZONED_DATE_TIME_COMPARATOR); } } @@ -132,7 +134,13 @@ public abstract class PastValidator<T extends Comparable<T>> extends TimeValidat } } + private static final IntPredicate TEST = n -> n < 0; + protected PastValidator(Function<Clock, T> now) { - super(now, n -> n < 0); + super(now, TEST); + } + + protected PastValidator(Function<Clock, T> now, Comparator<? super T> cmp) { + super(now, cmp, TEST); } } http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForCalendar.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForCalendar.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForCalendar.java deleted file mode 100644 index bcd87df..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForCalendar.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.Past; -import java.util.Calendar; - -/** - * Description: validate a date or calendar representing a date in the past<br/> - */ -public class PastValidatorForCalendar implements ConstraintValidator<Past, Calendar> { - - @Override - public void initialize(Past annotation) { - } - - @Override - public boolean isValid(Calendar cal, ConstraintValidatorContext context) { - return cal == null || cal.before(now()); - } - - /** - * overwrite when you need a different algorithm for 'now'. - * - * @return current date/time - */ - protected Calendar now() { - return Calendar.getInstance(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForDate.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForDate.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForDate.java deleted file mode 100644 index 565cca2..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidatorForDate.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.Past; -import java.util.Date; - -/** - * Description: validate a date or calendar representing a date in the past<br/> - */ -public class PastValidatorForDate implements ConstraintValidator<Past, Date> { - - @Override - public void initialize(Past annotation) { - } - - @Override - public boolean isValid(Date date, ConstraintValidatorContext context) { - return date == null || date.before(now()); - } - - /** - * overwrite when you need a different algorithm for 'now'. - * - * @return current date/time - */ - protected Date now() { - return new Date(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/0247c236/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java index 02e3836..034e9d1 100644 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java @@ -20,25 +20,44 @@ package org.apache.bval.constraints; import java.lang.annotation.Annotation; import java.time.Clock; +import java.time.chrono.ChronoLocalDate; +import java.time.chrono.ChronoLocalDateTime; +import java.time.chrono.ChronoZonedDateTime; +import java.util.Comparator; import java.util.function.Function; import java.util.function.IntPredicate; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; -public abstract class TimeValidator<A extends Annotation, T extends Comparable<T>> implements ConstraintValidator<A, T> { +public abstract class TimeValidator<A extends Annotation, T> implements ConstraintValidator<A, T> { + protected static final Comparator<ChronoLocalDate> CHRONO_LOCAL_DATE_COMPARATOR = + Comparator.nullsFirst((quid, quo) -> quid.isBefore(quo) ? -1 : quid.isAfter(quo) ? 1 : 0); + + protected static final Comparator<ChronoLocalDateTime<?>> CHRONO_LOCAL_DATE_TIME_COMPARATOR = + Comparator.nullsFirst((quid, quo) -> quid.isBefore(quo) ? -1 : quid.isAfter(quo) ? 1 : 0); + + protected static final Comparator<ChronoZonedDateTime<?>> CHRONO_ZONED_DATE_TIME_COMPARATOR = + Comparator.nullsFirst((quid, quo) -> quid.isBefore(quo) ? -1 : quid.isAfter(quo) ? 1 : 0); private final Function<Clock, T> now; + private final Comparator<? super T> cmp; private final IntPredicate test; - + + @SuppressWarnings("unchecked") protected TimeValidator(Function<Clock, T> now, IntPredicate test) { + this(now, (Comparator<T>) Comparator.naturalOrder(), test); + } + + protected TimeValidator(Function<Clock, T> now, Comparator<? super T> cmp,IntPredicate test) { super(); this.now = now; + this.cmp = cmp; this.test = test; } @Override public final boolean isValid(T value, ConstraintValidatorContext context) { - return value == null || test.test(value.compareTo(now.apply(context.getClockProvider().getClock()))); + return value == null || test.test(cmp.compare(value, now.apply(context.getClockProvider().getClock()))); } }
