Skip to content

Commit 2a0fed8

Browse files
authored
Merge pull request #48 from SentryMan/fuse
Fuse Null/NotNull Adapters
2 parents 093c737 + 1532582 commit 2a0fed8

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

validator-generator/src/main/java/io/avaje/validation/generator/AnnotationUtil.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface Handler {
2828
final String[] keys = {"AssertFalse", "AssertTrue", "Null", "NotNull", "NotBlank", "NotEmpty",
2929
"Size", "Email", "Past", "PastOrPresent", "Future", "FutureOrPresent",
3030
"Digits", "Positive", "PositiveOrZero", "Negative", "NegativeOrZero", "Max", "Min"};
31-
for (String key : keys) {
31+
for (final String key : keys) {
3232
handlers.put("io.avaje.validation.constraints." + key, commonHandler);
3333
handlers.put("jakarta.validation.constraints." + key, commonHandler);
3434
}
@@ -170,7 +170,6 @@ static class CommonHandler extends StandardHandler {
170170

171171
/** Prototype factory only */
172172
CommonHandler() {
173-
super();
174173
}
175174

176175
CommonHandler(AnnotationMirror annotationMirror, Element element) {
@@ -189,7 +188,7 @@ void writeAttribute(Name simpleName, AnnotationValue value, AnnotationValue defa
189188
if ("message".equals(name)) {
190189
writeAttributeKey("message");
191190
sb.append(messageKey(defaultValue));
192-
} else if (!name.equals("payload") && !name.equals("groups")) {
191+
} else if (!"payload".equals(name) && !"groups".equals(name)) {
193192
super.writeAttribute(simpleName, null, defaultValue);
194193
}
195194
} else {
@@ -206,7 +205,6 @@ static class DecimalHandler extends CommonHandler {
206205

207206
/** Prototype factory only */
208207
DecimalHandler() {
209-
super();
210208
}
211209

212210
@Override
@@ -218,6 +216,7 @@ public String attributes(AnnotationMirror annotationMirror, Element element) {
218216
super(annotationMirror, element);
219217
}
220218

219+
@Override
221220
String messageKey(AnnotationValue defaultValue) {
222221
final AnnotationValue inclusiveValue = memberValue("inclusive");
223222
final boolean inclusive = (inclusiveValue == null || "true".equals(inclusiveValue.toString()));

validator/src/main/java/io/avaje/validation/core/adapters/BasicAdapters.java

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ private BasicAdapters() {}
1818
public static final ValidationContext.AnnotationFactory FACTORY = (annotationType, context, attributes) ->
1919
switch (annotationType.getSimpleName()) {
2020
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);
2525
case "NotBlank" -> new NotBlankAdapter(context.message(attributes));
2626
case "NotEmpty" -> new NotEmptyAdapter(context.message(attributes));
2727
case "Past" -> new FuturePastAdapter(context.message(attributes), true, false);
@@ -43,7 +43,7 @@ private static final class PatternAdapter implements ValidationAdapter<CharSeque
4343
this.message = message;
4444
int flags = 0;
4545

46-
List<RegexFlag> flags1 = (List<RegexFlag>) attributes.get("flags");
46+
final List<RegexFlag> flags1 = (List<RegexFlag>) attributes.get("flags");
4747
if (flags1 != null) {
4848
for (final var flag : flags1) {
4949
flags |= flag.getValue();
@@ -86,25 +86,19 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
8686
req.addViolation(message, propertyName);
8787
return false;
8888
}
89-
}
90-
91-
if (value instanceof final Collection<?> col) {
89+
} else if (value instanceof final Collection<?> col) {
9290
final var len = col.size();
9391
if (len > max || len < min) {
9492
req.addViolation(message, propertyName);
9593
return len > 0;
9694
}
97-
}
98-
99-
if (value instanceof final Map<?, ?> map) {
95+
} else if (value instanceof final Map<?, ?> map) {
10096
final var len = map.size();
10197
if (len > max || len < min) {
10298
req.addViolation(message, propertyName);
10399
return len > 0;
104100
}
105-
}
106-
107-
if (value.getClass().isArray()) {
101+
} else if (value.getClass().isArray()) {
108102

109103
final var len = Array.getLength(value);
110104
if (len > max || len < min) {
@@ -158,14 +152,21 @@ private static final class NotEmptyAdapter implements ValidationAdapter<Object>
158152

159153
@Override
160154
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) {
164156
req.addViolation(message, propertyName);
165157
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+
}
166168
} else if (value instanceof final CharSequence sequence) {
167-
final var len = sequence.length();
168-
if (len == 0) {
169+
if (sequence.length() == 0) {
169170
req.addViolation(message, propertyName);
170171
return false;
171172
}
@@ -201,35 +202,19 @@ public boolean validate(Boolean type, ValidationRequest req, String propertyName
201202
}
202203
}
203204

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> {
223206

224207
private final ValidationContext.Message message;
208+
private final boolean shouldBeNull;
225209

226-
NullAdapter(ValidationContext.Message message) {
210+
NullableAdapter(ValidationContext.Message message, boolean shouldBeNull) {
227211
this.message = message;
212+
this.shouldBeNull = shouldBeNull;
228213
}
229214

230215
@Override
231216
public boolean validate(Object value, ValidationRequest req, String propertyName) {
232-
if (value != null) {
217+
if ((value == null) != shouldBeNull) {
233218
req.addViolation(message, propertyName);
234219
return false;
235220
}

0 commit comments

Comments
 (0)