df1a1cb758f7562ee22773bf93b7089466845e21
[so.git] / mso-catalog-db / src / main / java / org / openecomp / mso / db / catalog / utils / MavenLikeVersioning.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.db.catalog.utils;
22
23
24 import java.io.Serializable;
25
26 /**
27  * This class is the base class for object that requires a Version in Catalog DB.
28  * The version is built on a string as ASDC provides a number like 1.2 or 2.0 ...
29  * This class supports also 1.2.3.4...  (Maven like version)
30  *
31  *
32  */
33 public class MavenLikeVersioning implements Serializable {
34
35         protected String version;
36
37         public String getVersion() {
38                 return version;
39         }
40
41         public void setVersion(String version) {
42                 this.version = version;
43         }
44
45         /**
46          * This method is used to compare the current object version to a specified one
47          * It is assumed that the version is like the maven one, eg: 2.0.1.5.6
48          *
49          * @param versionToCompare The version that will be used for comparison
50          * @return True if the current object is more recent than the specified version, False otherwise
51          *
52          */
53         public boolean isMoreRecentThan (String versionToCompare) {
54                 if (versionToCompare == null || this.version == null) {
55                         return false;
56                 }
57                 String [] currentVersionArray = this.version.split("\\.");
58                 String [] specifiedVersionArray = versionToCompare.split("\\.");
59
60         int smalestStringLength = Math.min(currentVersionArray.length, specifiedVersionArray.length);
61
62                 for (int currentVersionIndex=0;currentVersionIndex < smalestStringLength;++currentVersionIndex) {
63
64                         if (Integer.parseInt(currentVersionArray[currentVersionIndex]) < Integer.parseInt(specifiedVersionArray[currentVersionIndex])) {
65                                 return false;
66                         } else if (Integer.parseInt(currentVersionArray[currentVersionIndex]) > Integer.parseInt(specifiedVersionArray[currentVersionIndex])) {
67                                 return true;
68                         }
69                 }
70
71                 // Even if versionToCompare has more digits, it means versionToCompare is more recent
72                 if (Integer.parseInt(currentVersionArray[smalestStringLength-1]) == Integer.parseInt(specifiedVersionArray[smalestStringLength-1])) {
73                     return currentVersionArray.length > specifiedVersionArray.length;
74                 }
75
76                 return true;
77         }
78
79         /**
80          * This method is used to compare the current object version to a specified one
81          * It is assumed that the version is like the maven one, eg: 2.0.1.5.6
82          *
83          * @param versionToCompare The version that will be used for comparison
84          * @return True if the current object is equal to the specified version, False otherwise
85          *
86          */
87         public boolean isTheSameVersion (String versionToCompare) {
88                 if (versionToCompare == null && this.version == null) {
89                         return true;
90                 } else if (versionToCompare == null || this.version == null) {
91                         return false;
92                 }
93                 String [] currentVersionArray = this.version.split("\\.");
94                 String [] specifiedVersionArray = versionToCompare.split("\\.");
95
96                 if (currentVersionArray.length != specifiedVersionArray.length) {
97                         return false;
98                 }
99
100                 for (int currentVersionIndex=0;currentVersionIndex < currentVersionArray.length;++currentVersionIndex) {
101
102                         if (Integer.parseInt(currentVersionArray[currentVersionIndex]) != Integer.parseInt(specifiedVersionArray[currentVersionIndex])) {
103                                 return false;
104                         }
105                 }
106
107                 return true;
108         }
109 }