Simplify code with new Map features
[controller.git] / opendaylight / config / config-api / src / main / java / org / opendaylight / controller / config / api / ValidationException.java
index da54a8341a8b477a9ebf4ba8f7cc02238668297f..1d528e5b001c5c5335fff5abbba5c41e2b7ca87c 100644 (file)
@@ -16,21 +16,23 @@ import java.util.Map.Entry;
 
 /**
  * This exception is not intended to be used while implementing modules,
- * itaggregates validation exceptions and sends them back to the user.
+ * it aggregates validation exceptions and sends them back to the user.
+ * Use {@link org.opendaylight.controller.config.api.JmxAttributeValidationException} for
+ * validating modules instead.
  */
-public class ValidationException extends RuntimeException {
+public class ValidationException extends Exception {
     private static final long serialVersionUID = -6072893219820274247L;
 
     private final Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> failedValidations;
 
     public ValidationException(
-            Map<String /* module name */, Map<String /* instance name */, ExceptionMessageWithStackTrace>> failedValidations) {
+            final Map<String /* module name */, Map<String /* instance name */, ExceptionMessageWithStackTrace>> failedValidations) {
         super(failedValidations.toString());
         this.failedValidations = Collections.unmodifiableMap(failedValidations);
     }
 
     public static ValidationException createFromCollectedValidationExceptions(
-            List<ValidationException> collectedExceptions) {
+            final List<ValidationException> collectedExceptions) {
         Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
         for (ValidationException ve : collectedExceptions) {
             for (Entry<String, Map<String, ExceptionMessageWithStackTrace>> outerEntry : ve
@@ -41,11 +43,7 @@ public class ValidationException extends RuntimeException {
                     String instanceName = innerEntry.getKey();
                     ExceptionMessageWithStackTrace ex = innerEntry.getValue();
                     Map<String, ExceptionMessageWithStackTrace> instanceToExMap = failedValidations
-                            .get(moduleName);
-                    if (instanceToExMap == null) {
-                        instanceToExMap = new HashMap<>();
-                        failedValidations.put(moduleName, instanceToExMap);
-                    }
+                            .computeIfAbsent(moduleName, k -> new HashMap<>());
                     if (instanceToExMap.containsKey(instanceName)) {
                         throw new IllegalArgumentException(
                                 "Cannot merge with same module name "
@@ -60,7 +58,7 @@ public class ValidationException extends RuntimeException {
     }
 
     public static ValidationException createForSingleException(
-            ModuleIdentifier moduleIdentifier, Exception e) {
+            final ModuleIdentifier moduleIdentifier, final Exception e) {
         Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
         Map<String, ExceptionMessageWithStackTrace> innerMap = new HashMap<>();
 
@@ -80,12 +78,12 @@ public class ValidationException extends RuntimeException {
         public ExceptionMessageWithStackTrace() {
         }
 
-        public ExceptionMessageWithStackTrace(String message, String stackTrace) {
+        public ExceptionMessageWithStackTrace(final String message, final String stackTrace) {
             this.message = message;
             this.stackTrace = stackTrace;
         }
 
-        public ExceptionMessageWithStackTrace(Exception e) {
+        public ExceptionMessageWithStackTrace(final Exception e) {
             this(e.getMessage(), Arrays.toString(e.getStackTrace()));
         }
 
@@ -97,11 +95,11 @@ public class ValidationException extends RuntimeException {
             return stackTrace;
         }
 
-        public void setMessage(String message) {
+        public void setMessage(final String message) {
             this.message = message;
         }
 
-        public void setTrace(String stackTrace) {
+        public void setTrace(final String stackTrace) {
             this.stackTrace = stackTrace;
         }
 
@@ -117,31 +115,37 @@ public class ValidationException extends RuntimeException {
         }
 
         @Override
-        public boolean equals(Object obj) {
-            if (this == obj)
+        public boolean equals(final Object obj) {
+            if (this == obj) {
                 return true;
-            if (obj == null)
+            }
+            if (obj == null) {
                 return false;
-            if (getClass() != obj.getClass())
+            }
+            if (getClass() != obj.getClass()) {
                 return false;
+            }
             ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
             if (message == null) {
-                if (other.message != null)
+                if (other.message != null) {
                     return false;
-            } else if (!message.equals(other.message))
+                }
+            } else if (!message.equals(other.message)) {
                 return false;
+            }
             if (stackTrace == null) {
-                if (other.stackTrace != null)
+                if (other.stackTrace != null) {
                     return false;
-            } else if (!stackTrace.equals(other.stackTrace))
+                }
+            } else if (!stackTrace.equals(other.stackTrace)) {
                 return false;
+            }
             return true;
         }
 
         @Override
         public String toString() {
-            return "ExceptionMessageWithStackTrace [message=" + message
-                    + ", stackTrace=" + stackTrace + "]";
+            return message;
         }
 
     }