aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/util/SemanticVersioning.java
blob: 8852e2a4f1971806233dc58f7ea4fff07c7fcb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.clamp.util;

/**
 * This class is the base class for object that requires semantic versioning.
 * ... This class supports also a.b.c.d... etc ... as a version.
 */
public class SemanticVersioning {
    public static final int BEFORE = -1;
    public static final int EQUAL = 0;
    public static final int AFTER = 1;
    public static final String DEFAULT_VERSION = "1.0.0";

    /**
     * The compare method that compare arg0 to arg1.
     *
     * @param arg0 A version in string for semantic versioning (a.b.c.d...)
     * @param arg1 A version in string for semantic versioning (a.b.c.d...)
     * @return objects (arg0, arg1) given as parameters. It returns the value: 0: if
     *      (arg0==arg1) -1: if (arg0 < arg1) 1: if (arg0 > arg1)
     */
    public static int compare(String arg0, String arg1) {

        if (arg0 == null && arg1 == null) {
            return EQUAL;
        }
        if (arg0 == null) {
            return BEFORE;
        }
        if (arg1 == null) {
            return AFTER;
        }
        String[] arg0Array = arg0.split("\\.");
        String[] arg1Array = arg1.split("\\.");

        int smalestStringLength = Math.min(arg0Array.length, arg1Array.length);

        for (int currentVersionIndex =
             0; currentVersionIndex < smalestStringLength; ++currentVersionIndex) {
            if (Integer.parseInt(arg0Array[currentVersionIndex]) < Integer
                    .parseInt(arg1Array[currentVersionIndex])) {
                return BEFORE;
            } else if (Integer.parseInt(arg0Array[currentVersionIndex]) > Integer
                    .parseInt(arg1Array[currentVersionIndex])) {
                return AFTER;
            }
            // equals, so do not return anything, continue
        }
        if (arg0Array.length == arg1Array.length) {
            return EQUAL;
        } else {
            return Integer.compare(arg0Array.length, arg1Array.length);
        }
    }

    /**
     * Method to increment a version from its current version.
     *
     * @param currentVersion The current Version
     * @return the increment version string
     */
    public static String incrementMajorVersion(String currentVersion) {
        if (currentVersion == null || currentVersion.isEmpty()) {
            return DEFAULT_VERSION;
        }
        String[] versionArray = currentVersion.split("\\.");
        return String.valueOf(Integer.parseInt(versionArray[0]) + 1) + ".0.0";
    }
}