Operational policy modification
[clamp.git] / src / main / java / org / onap / clamp / util / SemanticVersioning.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.util;
25
26 /**
27  * This class is the base class for object that requires semantic versioning.
28  * ... This class supports also a.b.c.d... etc ... as a version.
29  */
30 public class SemanticVersioning {
31     public static final int BEFORE = -1;
32     public static final int EQUAL = 0;
33     public static final int AFTER = 1;
34     public static final String DEFAULT_VERSION = "1.0.0";
35
36     /**
37      * The compare method that compare arg0 to arg1.
38      *
39      * @param arg0 A version in string for semantic versioning (a.b.c.d...)
40      * @param arg1 A version in string for semantic versioning (a.b.c.d...)
41      * @return objects (arg0, arg1) given as parameters. It returns the value: 0: if
42      *      (arg0==arg1) -1: if (arg0 < arg1) 1: if (arg0 > arg1)
43      */
44     public static int compare(String arg0, String arg1) {
45
46         if (arg0 == null && arg1 == null) {
47             return EQUAL;
48         }
49         if (arg0 == null) {
50             return BEFORE;
51         }
52         if (arg1 == null) {
53             return AFTER;
54         }
55         String[] arg0Array = arg0.split("\\.");
56         String[] arg1Array = arg1.split("\\.");
57
58         int smalestStringLength = Math.min(arg0Array.length, arg1Array.length);
59
60         for (int currentVersionIndex =
61              0; currentVersionIndex < smalestStringLength; ++currentVersionIndex) {
62             if (Integer.parseInt(arg0Array[currentVersionIndex]) < Integer
63                     .parseInt(arg1Array[currentVersionIndex])) {
64                 return BEFORE;
65             } else if (Integer.parseInt(arg0Array[currentVersionIndex]) > Integer
66                     .parseInt(arg1Array[currentVersionIndex])) {
67                 return AFTER;
68             }
69             // equals, so do not return anything, continue
70         }
71         if (arg0Array.length == arg1Array.length) {
72             return EQUAL;
73         } else {
74             return Integer.compare(arg0Array.length, arg1Array.length);
75         }
76     }
77
78     /**
79      * Method to increment a version from its current version.
80      *
81      * @param currentVersion The current Version
82      * @return the increment version string
83      */
84     public static String incrementMajorVersion(String currentVersion) {
85         if (currentVersion == null || currentVersion.isEmpty()) {
86             return DEFAULT_VERSION;
87         }
88         String[] versionArray = currentVersion.split("\\.");
89         return String.valueOf(Integer.parseInt(versionArray[0]) + 1) + ".0.0";
90     }
91 }