Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-catalog-db / src / main / java / org / onap / so / db / catalog / utils / MavenLikeVersioning.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.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. The version is built on a string as
28  * ASDC provides a number like 1.2 or 2.0 ... This class supports also 1.2.3.4... (Maven like version)
29  *
30  *
31  */
32 public class MavenLikeVersioning implements Serializable {
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 It is assumed that the version is
46      * 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 || versionToCompare.trim().isEmpty() || this.version == null
54                 || this.version.trim().isEmpty()) {
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
65                     .parseInt(specifiedVersionArray[currentVersionIndex])) {
66                 return false;
67             } else if (Integer.parseInt(currentVersionArray[currentVersionIndex]) > Integer
68                     .parseInt(specifiedVersionArray[currentVersionIndex])) {
69                 return true;
70             }
71         }
72         try {
73             // Even if versionToCompare has more digits, it means versionToCompare is more recent
74             return Integer.parseInt(currentVersionArray[smalestStringLength - 1]) != Integer
75                     .parseInt(specifiedVersionArray[smalestStringLength - 1])
76                     || currentVersionArray.length > specifiedVersionArray.length;
77         } catch (NumberFormatException e) {
78             return false;
79         }
80     }
81
82     /**
83      * This method is used to compare the current object version to a specified one It is assumed that the version is
84      * like the maven one, eg: 2.0.1.5.6
85      *
86      * @param versionToCompare The version that will be used for comparison
87      * @return True if the current object is equal to the specified version, False otherwise
88      *
89      */
90     public boolean isTheSameVersion(String versionToCompare) {
91         if (versionToCompare == null && this.version == null) {
92             return true;
93         } else if (versionToCompare == null || versionToCompare.trim().equals("") || this.version == null
94                 || this.version.trim().equals("")) {
95             return false;
96         }
97         String[] currentVersionArray = this.version.split("\\.");
98         String[] specifiedVersionArray = versionToCompare.split("\\.");
99
100         if (currentVersionArray.length != specifiedVersionArray.length) {
101             return false;
102         }
103
104         for (int currentVersionIndex = 0; currentVersionIndex < currentVersionArray.length; ++currentVersionIndex) {
105
106             if (Integer.parseInt(currentVersionArray[currentVersionIndex]) != Integer
107                     .parseInt(specifiedVersionArray[currentVersionIndex])) {
108                 return false;
109             }
110         }
111
112         return true;
113     }
114 }