Fix sonar issue with double compare 43/86543/2 1.4.0 4.0.0-ONAP
authorJim Hahn <jrh3@att.com>
Mon, 29 Apr 2019 15:43:05 +0000 (11:43 -0400)
committerJim Hahn <jrh3@att.com>
Mon, 29 Apr 2019 16:24:15 +0000 (12:24 -0400)
The gson code to convert Double to Long/Integer use a direct comparison
of the original double with a long.  However, sonar does not like that
so changed the code to use the Double.compare() method instead.
Also fixed sonar issue with diamond operator.
Addressed some sonar issues in ServiceManager.
Addressed some sonar issues in Version.

Change-Id: I0959603918d251db671e87e12c295c6ec911f427
Issue-ID: POLICY-1707
Signed-off-by: Jim Hahn <jrh3@att.com>
gson/src/main/java/org/onap/policy/common/gson/MapDoubleAdapterFactory.java
utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
utils/src/main/java/org/onap/policy/common/utils/validation/Version.java

index 3892a07..e56f9dd 100644 (file)
@@ -55,7 +55,7 @@ public class MapDoubleAdapterFactory implements TypeAdapterFactory {
 
         TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
 
-        return new MapAdapter<T>(delegate);
+        return new MapAdapter<>(delegate);
     }
 
     /**
@@ -134,7 +134,7 @@ public class MapDoubleAdapterFactory implements TypeAdapterFactory {
             Double num = (Double) obj;
             long longval = num.longValue();
 
-            if (num.doubleValue() == longval) {
+            if (Double.compare(num.doubleValue(), longval) == 0) {
                 // it's integral - determine if it's an integer or a long
                 int intval = (int) longval;
 
index 13cd6de..5c8c01d 100644 (file)
@@ -98,7 +98,7 @@ public class ServiceManager implements Startable {
             throw new IllegalStateException(name + " is already running; cannot add " + stepName);
         }
 
-        items.add(new Service(stepName, () -> service.start(), () -> service.stop()));
+        items.add(new Service(stepName, service::start, service::stop));
         return this;
     }
 
@@ -173,7 +173,7 @@ public class ServiceManager implements Startable {
      * @param running services that are running, in the order they were started
      * @throws ServiceManagerException if a service fails to stop
      */
-    private void rewind(Deque<Service> running) throws ServiceManagerException {
+    private void rewind(Deque<Service> running) {
         Exception ex = null;
 
         logger.info("{} stopping", name);
index d8106ec..41ff832 100644 (file)
@@ -50,6 +50,26 @@ public class Version implements Comparable<Version> {
     private final int patch;
 
 
+    /**
+     * String constructor.
+     *
+     * @param versionString the version string
+     */
+    public Version(@NonNull final String versionString) {
+        Version newVersion = makeVersion("String", "constructor", versionString);
+
+        if (newVersion != null) {
+            this.major = newVersion.major;
+            this.minor = newVersion.minor;
+            this.patch = newVersion.patch;
+        }
+        else {
+            this.major = 0;
+            this.minor = 0;
+            this.patch = 0;
+        }
+    }
+
     /**
      * Creates a version object.
      *
@@ -83,26 +103,6 @@ public class Version implements Comparable<Version> {
         }
     }
 
-    /**
-     * String constructor.
-     *
-     * @param versionString the version string
-     */
-    public Version(@NonNull final String versionString) {
-        Version newVersion = makeVersion("String", "constructor", versionString);
-
-        if (newVersion != null) {
-            this.major = newVersion.major;
-            this.minor = newVersion.minor;
-            this.patch = newVersion.patch;
-        }
-        else {
-            this.major = 0;
-            this.minor = 0;
-            this.patch = 0;
-        }
-    }
-
     /**
      * Generates a new version from a string.
      *