aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-01-06 11:15:45 -0500
committerJim Hahn <jrh3@att.com>2021-01-06 13:04:34 -0500
commit8208cc4b1d8855eee3fe59c7a832abccb2a67ed7 (patch)
tree13676da07d974013935ed2980f7b08a7917bf0c3 /common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
parentf9add40e0d87baff4ed56529bc61c31a577dc93d (diff)
Support annotations on parameterized types
It appears that java.validation allows validation annotations to be used on the type parameters of Collection and Map classes. Updated the validation code to support that. Once policy-models has been updated to use this approach, the original @Items and @Entries annotations will be deprecated. Issue-ID: POLICY-2648 Change-Id: Ic953be485ebafc9869f72407518f6549585353c9 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java61
1 files changed, 60 insertions, 1 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
index 51b11402..6791c616 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
@@ -288,6 +288,9 @@ public class BeanValidator {
}
ItemValidator itemValidator = makeItemValidator(annot);
+ if (itemValidator.isEmpty()) {
+ return true;
+ }
return verCollection(result, fieldName, itemValidator, value);
}
@@ -304,7 +307,7 @@ public class BeanValidator {
public boolean verCollection(BeanValidationResult result, String fieldName, ValueValidator itemValidator,
Object value) {
- if (!(value instanceof Collection) || itemValidator.isEmpty()) {
+ if (!(value instanceof Collection)) {
return true;
}
@@ -376,6 +379,62 @@ public class BeanValidator {
}
/**
+ * Validates the items in a Map.
+ *
+ * @param result where to add the validation result
+ * @param fieldName name of the field containing the map
+ * @param keyValidator validator for an individual key within the Map entry
+ * @param valueValidator validator for an individual value within the Map entry
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verMap(BeanValidationResult result, String fieldName, ValueValidator keyValidator,
+ ValueValidator valueValidator, Object value) {
+
+ if (!(value instanceof Map)) {
+ return true;
+ }
+
+ Map<?, ?> map = (Map<?, ?>) value;
+
+ BeanValidationResult result2 = new BeanValidationResult(fieldName, value);
+
+ for (Entry<?, ?> entry : map.entrySet()) {
+ String name = getEntryName(entry);
+
+ BeanValidationResult result3 = new BeanValidationResult(name, entry);
+ keyValidator.validateValue(result3, "key", entry.getKey());
+ valueValidator.validateValue(result3, "value", entry.getValue());
+
+ if (!result3.isClean()) {
+ result2.addResult(result3);
+ }
+ }
+
+ if (result2.isClean()) {
+ return true;
+ }
+
+ result.addResult(result2);
+ return false;
+ }
+
+ /**
+ * Gets a name for an entry.
+ *
+ * @param entry entry whose name is to be determined
+ * @return a name for the entry
+ */
+ protected <K, V> String getEntryName(Map.Entry<K, V> entry) {
+ K key = entry.getKey();
+ if (key == null) {
+ return "";
+ }
+
+ return key.toString();
+ }
+
+ /**
* Makes a field validator.
*
* @param clazz class containing the field