@@ -18,10 +18,10 @@ private BasicAdapters() {}
18
18
public static final ValidationContext .AnnotationFactory FACTORY = (annotationType , context , attributes ) ->
19
19
switch (annotationType .getSimpleName ()) {
20
20
case "Email" -> new EmailAdapter (context .message (attributes ), attributes );
21
- case "Null" -> new NullAdapter (context .message (attributes ));
22
- case "NotNull" , "NonNull" -> new NotNullAdapter (context .message (attributes ));
23
- case "AssertTrue" -> new AssertBooleanAdapter (context .message (attributes ), false );
24
- case "AssertFalse" -> new AssertBooleanAdapter (context .message (attributes ), true );
21
+ case "Null" -> new NullableAdapter (context .message (attributes ), true );
22
+ case "NotNull" , "NonNull" -> new NullableAdapter (context .message (attributes ), false );
23
+ case "AssertTrue" -> new AssertBooleanAdapter (context .message (attributes ), Boolean . FALSE );
24
+ case "AssertFalse" -> new AssertBooleanAdapter (context .message (attributes ), Boolean . TRUE );
25
25
case "NotBlank" -> new NotBlankAdapter (context .message (attributes ));
26
26
case "NotEmpty" -> new NotEmptyAdapter (context .message (attributes ));
27
27
case "Past" -> new FuturePastAdapter (context .message (attributes ), true , false );
@@ -43,7 +43,7 @@ private static final class PatternAdapter implements ValidationAdapter<CharSeque
43
43
this .message = message ;
44
44
int flags = 0 ;
45
45
46
- List <RegexFlag > flags1 = (List <RegexFlag >) attributes .get ("flags" );
46
+ final List <RegexFlag > flags1 = (List <RegexFlag >) attributes .get ("flags" );
47
47
if (flags1 != null ) {
48
48
for (final var flag : flags1 ) {
49
49
flags |= flag .getValue ();
@@ -86,25 +86,19 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
86
86
req .addViolation (message , propertyName );
87
87
return false ;
88
88
}
89
- }
90
-
91
- if (value instanceof final Collection <?> col ) {
89
+ } else if (value instanceof final Collection <?> col ) {
92
90
final var len = col .size ();
93
91
if (len > max || len < min ) {
94
92
req .addViolation (message , propertyName );
95
93
return len > 0 ;
96
94
}
97
- }
98
-
99
- if (value instanceof final Map <?, ?> map ) {
95
+ } else if (value instanceof final Map <?, ?> map ) {
100
96
final var len = map .size ();
101
97
if (len > max || len < min ) {
102
98
req .addViolation (message , propertyName );
103
99
return len > 0 ;
104
100
}
105
- }
106
-
107
- if (value .getClass ().isArray ()) {
101
+ } else if (value .getClass ().isArray ()) {
108
102
109
103
final var len = Array .getLength (value );
110
104
if (len > max || len < min ) {
@@ -158,14 +152,21 @@ private static final class NotEmptyAdapter implements ValidationAdapter<Object>
158
152
159
153
@ Override
160
154
public boolean validate (Object value , ValidationRequest req , String propertyName ) {
161
- if (value == null
162
- || value instanceof final Collection <?> col && col .isEmpty ()
163
- || value instanceof final Map <?, ?> map && map .isEmpty ()) {
155
+ if (value == null ) {
164
156
req .addViolation (message , propertyName );
165
157
return false ;
158
+ } else if (value instanceof final Collection <?> col ) {
159
+ if (col .isEmpty ()) {
160
+ req .addViolation (message , propertyName );
161
+ return false ;
162
+ }
163
+ } else if (value instanceof final Map <?, ?> map ) {
164
+ if (map .isEmpty ()) {
165
+ req .addViolation (message , propertyName );
166
+ return false ;
167
+ }
166
168
} else if (value instanceof final CharSequence sequence ) {
167
- final var len = sequence .length ();
168
- if (len == 0 ) {
169
+ if (sequence .length () == 0 ) {
169
170
req .addViolation (message , propertyName );
170
171
return false ;
171
172
}
@@ -201,35 +202,19 @@ public boolean validate(Boolean type, ValidationRequest req, String propertyName
201
202
}
202
203
}
203
204
204
- private static final class NotNullAdapter implements ValidationAdapter <Object > {
205
-
206
- private final ValidationContext .Message message ;
207
-
208
- NotNullAdapter (ValidationContext .Message message ) {
209
- this .message = message ;
210
- }
211
-
212
- @ Override
213
- public boolean validate (Object value , ValidationRequest req , String propertyName ) {
214
- if (value == null ) {
215
- req .addViolation (message , propertyName );
216
- return false ;
217
- }
218
- return true ;
219
- }
220
- }
221
-
222
- private static final class NullAdapter implements ValidationAdapter <Object > {
205
+ private static final class NullableAdapter implements ValidationAdapter <Object > {
223
206
224
207
private final ValidationContext .Message message ;
208
+ private final boolean shouldBeNull ;
225
209
226
- NullAdapter (ValidationContext .Message message ) {
210
+ NullableAdapter (ValidationContext .Message message , boolean shouldBeNull ) {
227
211
this .message = message ;
212
+ this .shouldBeNull = shouldBeNull ;
228
213
}
229
214
230
215
@ Override
231
216
public boolean validate (Object value , ValidationRequest req , String propertyName ) {
232
- if (value != null ) {
217
+ if (( value == null ) != shouldBeNull ) {
233
218
req .addViolation (message , propertyName );
234
219
return false ;
235
220
}
0 commit comments