Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / core / DBVersion.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.sdc.asdctool.migration.core;
22
23 import java.math.BigInteger;
24
25 public class DBVersion implements Comparable<DBVersion>{
26
27     private static final String VERSION_PARTS_SEPARATOR = "\\.";
28     private static final int MAJOR_PART_IDX = 0;
29     private static final int MINOR_PART_IDX = 1;
30     private BigInteger major;
31     private BigInteger minor;
32
33     /**
34      * The current db version. should be tested against real db to verify it is compatible to the db version
35      */
36     public static final DBVersion DEFAULT_VERSION = new DBVersion(1710, 0);
37
38     private DBVersion(BigInteger major, BigInteger minor) {
39         this.major = major;
40         this.minor = minor;
41     }
42
43     private DBVersion(int major, int minor) {
44         this.major = BigInteger.valueOf(major);
45         this.minor = BigInteger.valueOf(minor);
46     }
47
48     public BigInteger getMajor() {
49         return major;
50     }
51
52     public BigInteger getMinor() {
53         return minor;
54     }
55
56     public static DBVersion from(BigInteger major, BigInteger minor) {
57         return new DBVersion(major, minor);
58     }
59
60     public static DBVersion fromString(String version) {
61         String[] split = version.split(VERSION_PARTS_SEPARATOR);
62         if (split.length != 2) {
63             throw new MigrationException("version must be of pattern: <major>.<minor>");
64         }
65         return new DBVersion(getVersionPart(split[MAJOR_PART_IDX]),
66                              getVersionPart(split[MINOR_PART_IDX]));
67
68     }
69
70     private static BigInteger getVersionPart(String versionPart) {
71         try {
72             return new BigInteger(versionPart);
73         } catch (NumberFormatException e) {
74             throw new MigrationException(String.format("version part %s is non numeric", versionPart));
75         }
76     }
77
78     @Override
79     public String toString() {
80         return String.format("%s.%s", major, minor);
81     }
82
83     @Override
84     public boolean equals(Object o) {
85         if (this == o) return true;
86         if (o == null || getClass() != o.getClass()) return false;
87
88         DBVersion dbVersion = (DBVersion) o;
89
90         return major.equals(dbVersion.major) && minor.equals(dbVersion.minor);
91     }
92
93     @Override
94     public int hashCode() {
95         int result = major.hashCode();
96         result = 31 * result + minor.hashCode();
97         return result;
98     }
99
100     @Override
101     public int compareTo(DBVersion o) {
102         if (o == null) {
103             return 1;
104         }
105         int majorsComparision = this.major.compareTo(o.major);
106         if (majorsComparision != 0) {
107             return majorsComparision;
108         }
109         int minorsComparision = this.minor.compareTo(o.minor);
110         if (minorsComparision != 0) {
111             return minorsComparision;
112         }
113         return 0;
114     }
115 }