Fixing sonar issues 11/98411/3
authorDmitry Puzikov <d.puzikov2@partner.samsung.com>
Thu, 14 Nov 2019 13:10:22 +0000 (14:10 +0100)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Thu, 21 Nov 2019 15:39:16 +0000 (15:39 +0000)
Reduce cyclomatic complexity.

Splitted complex method to a set of more simple methods.

Change-Id: Ic0be3cccefa520f38af83872ddc3e981205edc36
Issue-ID: SDC-2654
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java

index 32adf55..e7a4c45 100644 (file)
@@ -384,63 +384,24 @@ public class ConfigurationUtils {
     }
 
     public static Object getPrimitiveArray(Collection collection, Class clazz) {
-        if (clazz == int.class) {
-            int[] array = new int[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (int) objArray[i];
-            }
-            return array;
-        }
-        if (clazz == byte.class) {
-            byte[] array = new byte[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (byte) objArray[i];
-            }
-            return array;
-        }
-        if (clazz == short.class) {
-            short[] array = new short[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (short) objArray[i];
-            }
-            return array;
-        }
-        if (clazz == long.class) {
-            long[] array = new long[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (long) objArray[i];
-            }
-            return array;
-        }
-        if (clazz == float.class) {
-            float[] array = new float[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (float) objArray[i];
-            }
-            return array;
-        }
-        if (clazz == double.class) {
-            double[] array = new double[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (double) objArray[i];
-            }
-            return array;
-        }
-        if (clazz == boolean.class) {
-            boolean[] array = new boolean[collection.size()];
-            Object[] objArray = collection.toArray();
-            for (int i = 0; i < collection.size(); i++) {
-                array[i] = (boolean) objArray[i];
-            }
-            return array;
+        switch (clazz.getName()) {
+            case "int":
+                return getIntsPrimitiveArray(collection);
+            case "byte":
+                return getBytesPrimitiveArray(collection);
+            case "short":
+                return getShortsPrimitiveArray(collection);
+            case "long":
+                return getLongsPrimitiveArray(collection);
+            case "float":
+                return getFloatsPrimitiveArray(collection);
+            case "double":
+                return getDoublesPrimitiveArray(collection);
+            case "boolean":
+                return getBooleansPrimitiveArray(collection);
+            default:
+                return null;
         }
-        return null;
     }
 
     public static String getCollectionString(String input) {
@@ -695,4 +656,76 @@ public class ConfigurationUtils {
     public static boolean isBlank(String value) {
         return value == null || value.trim().isEmpty();
     }
+
+    // private methods section starts here
+
+    private static int[] getIntsPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        int[] array = new int[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (int) objArray[i];
+        }
+        return array;
+    }
+
+    private static byte[] getBytesPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        byte[] array = new byte[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (byte) objArray[i];
+        }
+        return array;
+    }
+
+    private static short[] getShortsPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        short[] array = new short[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (short) objArray[i];
+        }
+        return array;
+    }
+
+    private static long[] getLongsPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        long[] array = new long[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (long) objArray[i];
+        }
+        return array;
+    }
+
+    private static float[] getFloatsPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        float[] array = new float[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (float) objArray[i];
+        }
+        return array;
+    }
+
+    private static double[] getDoublesPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        double[] array = new double[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (double) objArray[i];
+        }
+        return array;
+    }
+
+    private static boolean[] getBooleansPrimitiveArray(Collection collection) {
+        int collectionSize = collection.size();
+        boolean[] array = new boolean[collectionSize];
+        Object[] objArray = collection.toArray();
+        for (int i = 0; i < collectionSize; i++) {
+            array[i] = (boolean) objArray[i];
+        }
+        return array;
+    }
 }