2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.util;
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.
32 public class SemanticVersioning {
33 public static final int BEFORE = -1;
34 public static final int EQUAL = 0;
35 public static final int AFTER = 1;
38 * The compare method that compare arg0 to arg1.
40 * @param arg0 A version in string for semantice versioning (a.b.c.d...)
41 * @param arg1 A version in string for semantice versioning (a.b.c.d...)
42 * @return objects (arg0, arg1) given as parameters. It returns the value: 0: if
43 * (arg0==arg1) -1: if (arg0 < arg1) 1: if (arg0 > arg1)
45 public static int compare(String arg0, String arg1) {
47 if (arg0 == null && arg1 == null) {
56 String[] arg0Array = arg0.split("\\.");
57 String[] arg1Array = arg1.split("\\.");
59 int smalestStringLength = Math.min(arg0Array.length, arg1Array.length);
61 for (int currentVersionIndex = 0; currentVersionIndex < smalestStringLength; ++currentVersionIndex) {
62 if (Integer.parseInt(arg0Array[currentVersionIndex]) < Integer.parseInt(arg1Array[currentVersionIndex])) {
64 } else if (Integer.parseInt(arg0Array[currentVersionIndex]) > Integer
65 .parseInt(arg1Array[currentVersionIndex])) {
68 // equals, so do not return anything, continue
70 if (arg0Array.length == arg1Array.length) {
73 return Integer.compare(arg0Array.length, arg1Array.length);