Initial OpenECOMP MSO commit
[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
25 /**
26  * This class is the base class for object that requires a Version in Catalog DB.
27  * The version is built on a string as ASDC provides a number like 1.2 or 2.0 ...
28  * This class supports also 1.2.3.4...  (Maven like version)
29  * 
30  *
31  */
32 public class MavenLikeVersioning {
33
34         protected String version;
35
36         public String getVersion() {
37                 return version;
38         }
39
40         public void setVersion(String version) {
41                 this.version = version;
42         }
43
44         /**
45          * This method is used to compare the current object version to a specified one
46          * It is assumed that the version is like the maven one, eg: 2.0.1.5.6
47          *
48          * @param versionToCompare The version that will be used for comparison
49          * @return True if the current object is more recent than the specified version, False otherwise
50          *
51          */
52         public Boolean isMoreRecentThan (String versionToCompare) {
53                 if (versionToCompare == null || this.version == null) {
54                         return Boolean.FALSE;
55                 }
56                 String [] currentVersionArray = this.version.split("\\.");
57                 String [] specifiedVersionArray = versionToCompare.split("\\.");
58
59                 int smalestStringLength = 0;
60
61                 if (currentVersionArray.length > specifiedVersionArray.length) {
62                         smalestStringLength = specifiedVersionArray.length;
63                 } else {
64                         smalestStringLength = currentVersionArray.length;
65                 }
66
67                 for (int currentVersionIndex=0;currentVersionIndex < smalestStringLength;++currentVersionIndex) {
68
69                         if (Integer.valueOf(currentVersionArray[currentVersionIndex]) < Integer.valueOf(specifiedVersionArray[currentVersionIndex])) {
70                                 return Boolean.FALSE;
71                         } else if (Integer.valueOf(currentVersionArray[currentVersionIndex]) > Integer.valueOf(specifiedVersionArray[currentVersionIndex])) {
72                                 return Boolean.TRUE;
73                         }
74                 }
75
76                 // Even if versionToCompare has more digits, it means versionToCompare is more recent
77                 if (Integer.valueOf(currentVersionArray[smalestStringLength-1]).intValue () == Integer.valueOf(specifiedVersionArray[smalestStringLength-1]).intValue ()) {
78                         if (currentVersionArray.length > specifiedVersionArray.length) {
79                                 return Boolean.TRUE;
80                         } else {
81                                 return Boolean.FALSE;
82                         }
83                 }
84
85                 return Boolean.TRUE;
86         }
87
88         /**
89          * This method is used to compare the current object version to a specified one
90          * It is assumed that the version is like the maven one, eg: 2.0.1.5.6
91          *
92          * @param versionToCompare The version that will be used for comparison
93          * @return True if the current object is equal to the specified version, False otherwise
94          *
95          */
96         public Boolean isTheSameVersion (String versionToCompare) {
97                 if (versionToCompare == null && this.version == null) {
98                         return Boolean.TRUE;
99                 } else if (versionToCompare == null || this.version == null) {
100                         return Boolean.FALSE;
101                 }
102                 String [] currentVersionArray = this.version.split("\\.");
103                 String [] specifiedVersionArray = versionToCompare.split("\\.");
104
105                 if (currentVersionArray.length != specifiedVersionArray.length) {
106                         return Boolean.FALSE;
107                 }
108
109                 for (int currentVersionIndex=0;currentVersionIndex < currentVersionArray.length;++currentVersionIndex) {
110
111                         if (Integer.valueOf(currentVersionArray[currentVersionIndex]).intValue () != Integer.valueOf(specifiedVersionArray[currentVersionIndex]).intValue ()) {
112                                 return Boolean.FALSE;
113                         }
114                 }
115
116                 return Boolean.TRUE;
117         }
118 }