@@ -27,30 +27,21 @@ final class BasicAdapters {
27
27
private BasicAdapters () {}
28
28
29
29
static final ValidationContext .AnnotationFactory FACTORY =
30
- (annotationType , context , attributes ) -> {
31
- switch (annotationType .getSimpleName ()) {
32
- case "NotNull" :
33
- return new NotNullAdapter (context .message ("NotNull" , attributes ));
34
- case "AssertTrue" :
35
- return new AssertTrueAdapter (context .message ("AssertTrue" , attributes ));
36
- case "AssertFalse" :
37
- return new AssertFalseAdapter (context .message ("AssertFalse" , attributes ));
38
- case "NotBlank" :
39
- return new NotBlankAdapter (context .message ("NotBlank" , attributes ));
40
- case "Past" :
41
- case "PastOrPresent" :
42
- return new PastAdapter (context .message ("Past" , attributes ));
43
- case "Future" :
44
- case "FutureOrPresent" :
45
- return new FutureAdapter (context .message ("Future" , attributes ));
46
- case "Pattern" :
47
- return new PatternAdapter (context .message ("Pattern" , attributes ), attributes );
48
- case "Size" :
49
- return new SizeAdapter (context .message ("Size" , attributes ), attributes );
50
- default :
51
- return null ;
52
- }
53
- };
30
+ (annotationType , context , attributes ) ->
31
+ switch (annotationType .getSimpleName ()) {
32
+ case "NotNull" -> new NotNullAdapter (context .message ("NotNull" , attributes ));
33
+ case "AssertTrue" -> new AssertTrueAdapter (context .message ("AssertTrue" , attributes ));
34
+ case "AssertFalse" -> new AssertFalseAdapter (
35
+ context .message ("AssertFalse" , attributes ));
36
+ case "NotBlank" -> new NotBlankAdapter (context .message ("NotBlank" , attributes ));
37
+ case "Past" , "PastOrPresent" -> new PastAdapter (context .message ("Past" , attributes ));
38
+ case "Future" , "FutureOrPresent" -> new FutureAdapter (
39
+ context .message ("Future" , attributes ));
40
+ case "Pattern" -> new PatternAdapter (
41
+ context .message ("Pattern" , attributes ), attributes );
42
+ case "Size" -> new SizeAdapter (context .message ("Size" , attributes ), attributes );
43
+ default -> null ;
44
+ };
54
45
55
46
private static final class PatternAdapter implements ValidationAdapter <CharSequence > {
56
47
@@ -99,27 +90,24 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
99
90
return false ;
100
91
}
101
92
102
- if (value instanceof CharSequence ) {
103
- final var sequence = (CharSequence ) value ;
93
+ if (value instanceof final CharSequence sequence ) {
104
94
final var len = sequence .length ();
105
95
if (len > max || len < min ) {
106
96
req .addViolation (message , propertyName );
107
97
return false ;
108
98
}
109
99
}
110
100
111
- if (value instanceof Collection <?>) {
112
- final var col = (Collection <?>) value ;
101
+ if (value instanceof final Collection <?> col ) {
113
102
final var len = col .size ();
114
103
if (len > max || len < min ) {
115
104
req .addViolation (message , propertyName );
116
105
return len > 0 ;
117
106
}
118
107
}
119
108
120
- if (value instanceof Map <?, ?>) {
121
- final var col = (Map <?, ?>) value ;
122
- final var len = col .size ();
109
+ if (value instanceof final Map <?, ?> map ) {
110
+ final var len = map .size ();
123
111
if (len > max || len < min ) {
124
112
req .addViolation (message , propertyName );
125
113
return len > 0 ;
@@ -154,35 +142,26 @@ public boolean validate(Object obj, ValidationRequest req, String propertyName)
154
142
req .addViolation (message , propertyName );
155
143
return false ;
156
144
}
157
- if (obj instanceof Date ) {
158
- final Date date = (Date ) obj ;
145
+ if (obj instanceof final Date date ) {
159
146
if (date .before (Date .from (Instant .now ()))) {
160
147
req .addViolation (message , propertyName );
161
148
return false ;
162
149
}
163
- } else if (obj instanceof TemporalAccessor ) {
164
-
165
- final TemporalAccessor temporalAccessor = (TemporalAccessor ) obj ;
166
- if (temporalAccessor instanceof Instant
167
- && Instant .from (temporalAccessor ).isBefore (Instant .now ())
168
- || temporalAccessor instanceof LocalDate
169
- && LocalDate .from (temporalAccessor ).isBefore (LocalDate .now ())
170
- || temporalAccessor instanceof LocalDateTime
171
- && LocalDateTime .from (temporalAccessor ).isBefore (LocalDateTime .now ())
172
- || temporalAccessor instanceof LocalTime
173
- && LocalTime .from (temporalAccessor ).isBefore (LocalTime .now ())
174
- || temporalAccessor instanceof ZonedDateTime
175
- && ZonedDateTime .from (temporalAccessor ).isBefore (ZonedDateTime .now ())
176
- || temporalAccessor instanceof OffsetDateTime
177
- && OffsetDateTime .from (temporalAccessor ).isBefore (OffsetDateTime .now ())
178
- || temporalAccessor instanceof OffsetTime
179
- && OffsetTime .from (temporalAccessor ).isBefore (OffsetTime .now ())
180
- || temporalAccessor instanceof Year && Year .from (temporalAccessor ).isBefore (Year .now ())
181
- || temporalAccessor instanceof YearMonth
182
- && YearMonth .from (temporalAccessor ).isBefore (YearMonth .now ())) {
183
- req .addViolation (message , propertyName );
184
- return false ;
185
- }
150
+ } else if (obj instanceof final TemporalAccessor temporalAccessor
151
+ && (temporalAccessor instanceof final Instant ins && ins .isBefore (Instant .now ())
152
+ || temporalAccessor instanceof final LocalDate ld && ld .isBefore (LocalDate .now ())
153
+ || temporalAccessor instanceof final LocalDateTime ldt
154
+ && ldt .isBefore (LocalDateTime .now ())
155
+ || temporalAccessor instanceof final LocalTime lt && lt .isBefore (LocalTime .now ())
156
+ || temporalAccessor instanceof final ZonedDateTime zdt
157
+ && zdt .isBefore (ZonedDateTime .now ())
158
+ || temporalAccessor instanceof final OffsetDateTime odt
159
+ && odt .isBefore (OffsetDateTime .now ())
160
+ || temporalAccessor instanceof final OffsetTime ot && ot .isBefore (OffsetTime .now ())
161
+ || temporalAccessor instanceof final Year y && y .isBefore (Year .now ())
162
+ || temporalAccessor instanceof final YearMonth ym && ym .isBefore (YearMonth .now ()))) {
163
+ req .addViolation (message , propertyName );
164
+ return false ;
186
165
}
187
166
return true ;
188
167
}
@@ -203,33 +182,26 @@ public boolean validate(Object obj, ValidationRequest req, String propertyName)
203
182
req .addViolation (message , propertyName );
204
183
return false ;
205
184
}
206
- if (obj instanceof Date ) {
207
- final Date date = (Date ) obj ;
185
+ if (obj instanceof final Date date ) {
208
186
if (date .after (Date .from (Instant .now ()))) {
209
187
req .addViolation (message , propertyName );
210
188
return false ;
211
189
}
212
- } else if (obj instanceof TemporalAccessor ) {
213
-
214
- final TemporalAccessor temporalAccessor = (TemporalAccessor ) obj ;
215
- if (temporalAccessor instanceof LocalDate
216
- && LocalDate .from (temporalAccessor ).isAfter (LocalDate .now ())
217
- || temporalAccessor instanceof LocalDateTime
218
- && LocalDateTime .from (temporalAccessor ).isAfter (LocalDateTime .now ())
219
- || temporalAccessor instanceof LocalTime
220
- && LocalTime .from (temporalAccessor ).isAfter (LocalTime .now ())
221
- || temporalAccessor instanceof ZonedDateTime
222
- && ZonedDateTime .from (temporalAccessor ).isAfter (ZonedDateTime .now ())
223
- || temporalAccessor instanceof OffsetDateTime
224
- && OffsetDateTime .from (temporalAccessor ).isAfter (OffsetDateTime .now ())
225
- || temporalAccessor instanceof OffsetTime
226
- && OffsetTime .from (temporalAccessor ).isAfter (OffsetTime .now ())
227
- || temporalAccessor instanceof Year && Year .from (temporalAccessor ).isAfter (Year .now ())
228
- || temporalAccessor instanceof YearMonth
229
- && YearMonth .from (temporalAccessor ).isAfter (YearMonth .now ())) {
230
- req .addViolation (message , propertyName );
231
- return false ;
232
- }
190
+ } else if (obj instanceof final TemporalAccessor temporalAccessor
191
+ && (temporalAccessor instanceof final Instant ins && ins .isAfter (Instant .now ())
192
+ || temporalAccessor instanceof final LocalDate ld && ld .isAfter (LocalDate .now ())
193
+ || temporalAccessor instanceof final LocalDateTime ldt
194
+ && ldt .isAfter (LocalDateTime .now ())
195
+ || temporalAccessor instanceof final LocalTime lt && lt .isAfter (LocalTime .now ())
196
+ || temporalAccessor instanceof final ZonedDateTime zdt
197
+ && zdt .isAfter (ZonedDateTime .now ())
198
+ || temporalAccessor instanceof final OffsetDateTime odt
199
+ && odt .isAfter (OffsetDateTime .now ())
200
+ || temporalAccessor instanceof final OffsetTime ot && ot .isAfter (OffsetTime .now ())
201
+ || temporalAccessor instanceof final Year y && y .isAfter (Year .now ())
202
+ || temporalAccessor instanceof final YearMonth ym && ym .isAfter (YearMonth .now ()))) {
203
+ req .addViolation (message , propertyName );
204
+ return false ;
233
205
}
234
206
return true ;
235
207
}
0 commit comments