update SDC-TOSCA package names
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / tosca / parser / utils / GeneralUtility.java
1 package org.onap.sdc.tosca.parser.utils;
2
3 import java.util.Arrays;
4
5 public class GeneralUtility {
6
7     public static boolean isEmptyString(String str) {
8         return str == null || str.trim().isEmpty();
9     }
10
11
12     /**
13      * Compares two version strings.
14      * <p>
15      * Use this instead of String.compareTo() for a non-lexicographical
16      * comparison that works for version strings. e.g. "1.10".compareTo("1.6").
17      *
18      * @param str1 a string of ordinal numbers separated by decimal points.
19      * @param str2 a string of ordinal numbers separated by decimal points.
20      * @return The result is a negative integer if str1 is _numerically_ less than str2.
21      * The result is a positive integer if str1 is _numerically_ greater than str2.
22      * The result is zero if the strings are _numerically_ equal.
23      * It does not work if "1.10" is supposed to be equal to "1.10.0".
24      */
25     public static int conformanceLevelCompare(String str1, String str2) {
26         String[] vals1 = str1.split("\\.");
27         String[] vals2 = str2.split("\\.");
28         int i = 0;
29         // set index to first non-equal ordinal or length of shortest version string
30         while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
31             i++;
32         }
33         // compare first non-equal ordinal number
34         if (i < vals1.length && i < vals2.length) {
35             int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
36             return Integer.signum(diff);
37         }
38         //in case of 0 after the . e.g: "3" = "3.0" or "3.0.0.0" = "3.0"
39         str2 = str2.substring(i).replace(".", "");
40         str1 = str1.substring(i).replace(".", "");
41         if ((!(str1.equals("")))  && Integer.valueOf(str1) == 0){
42             vals1 = Arrays.copyOf(vals1, i);
43         }
44         if ((!(str2.equals("")))  && Integer.valueOf(str2) == 0){
45             vals2 = Arrays.copyOf(vals2, i);
46         }
47
48         // the strings are equal or one string is a substring of the other
49         // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
50         return Integer.signum(vals1.length - vals2.length);
51     }
52
53 }