Address more sonars in apex-pdp
[policy/apex-pdp.git] / tools / tools-common / src / main / java / org / onap / policy / apex / tools / common / Console.java
index 2675849..59c3c0e 100644 (file)
@@ -1,6 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.tools.common;
 
+import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
-
 import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.text.StrBuilder;
 import org.slf4j.ext.XLoggerFactory;
 import org.slf4j.helpers.MessageFormatter;
 
@@ -72,6 +73,9 @@ public final class Console {
     /** Configuration for a collecting warning messages. */
     public static final int CONFIG_COLLECT_WARNINGS = 0b0010;
 
+    // Input and output streams
+    private static final PrintStream ERR_STREAM = System.err;
+
     /** The setting for message types, set using type flags. */
     private int types;
 
@@ -185,7 +189,7 @@ public final class Console {
             return;
         }
 
-        final StrBuilder err = new StrBuilder();
+        final var err = new StringBuilder();
         if (appName != null) {
             err.append(this.getAppName()).append(": ");
         }
@@ -193,10 +197,10 @@ public final class Console {
         err.append(MessageFormatter.arrayFormat(message, objects).getMessage());
 
         if ((types & TYPE_ERROR) == TYPE_ERROR) {
-            System.err.println(err.build());
+            ERR_STREAM.println(err.toString());
         }
         if ((configuration & CONFIG_COLLECT_ERRORS) == CONFIG_COLLECT_ERRORS) {
-            errors.add(err.build());
+            errors.add(err.toString());
         }
     }
 
@@ -214,7 +218,7 @@ public final class Console {
             return;
         }
 
-        final StrBuilder warn = new StrBuilder();
+        final var warn = new StringBuilder();
         if (appName != null) {
             warn.append(this.getAppName()).append(": ");
         }
@@ -222,10 +226,10 @@ public final class Console {
         warn.append(MessageFormatter.arrayFormat(message, objects).getMessage());
 
         if ((types & TYPE_WARNING) == TYPE_WARNING) {
-            System.err.println(warn.build());
+            ERR_STREAM.println(warn.toString());
         }
         if ((configuration & CONFIG_COLLECT_WARNINGS) == CONFIG_COLLECT_WARNINGS) {
-            warnings.add(warn.build());
+            warnings.add(warn.toString());
         }
     }
 
@@ -243,9 +247,9 @@ public final class Console {
 
         if ((types & TYPE_INFO) == TYPE_INFO) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -263,10 +267,10 @@ public final class Console {
 
         if ((types & TYPE_PROGRESS) == TYPE_PROGRESS) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.print("progress: ");
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.print("progress: ");
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -284,10 +288,10 @@ public final class Console {
 
         if ((types & TYPE_DEBUG) == TYPE_DEBUG) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.print("debug: ");
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.print("debug: ");
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -305,10 +309,10 @@ public final class Console {
 
         if ((types & TYPE_TRACE) == TYPE_TRACE) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.print("trace: ");
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.print("trace: ");
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -324,13 +328,13 @@ public final class Console {
 
         if ((types & TYPE_STACKTRACE) == TYPE_STACKTRACE) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.println(" exception message: " + exception.getMessage());
+            ERR_STREAM.println(" exception message: " + exception.getMessage());
             if (exception.getCause() != null) {
-                System.err.println(" exception cause: " + exception.getCause());
+                ERR_STREAM.println(" exception cause: " + exception.getCause());
             }
-            System.err.println("for exception stack trace, please refer logs.");
+            ERR_STREAM.println("for exception stack trace, please refer logs.");
             XLoggerFactory.getXLogger(Console.class).error("stacktrace", exception);
         }
     }