[SDC] rebase 1710 code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / core / DBVersion.java
1 package org.openecomp.sdc.asdctool.migration.core;
2
3 import java.math.BigInteger;
4
5 public class DBVersion implements Comparable<DBVersion>{
6
7     private static final String VERSION_PARTS_SEPARATOR = "\\.";
8     private static final int MAJOR_PART_IDX = 0;
9     private static final int MINOR_PART_IDX = 1;
10     private BigInteger major;
11     private BigInteger minor;
12
13     /**
14      * The current db version. should be tested against real db to verify it is compatible to the db version
15      */
16     public static final DBVersion CURRENT_VERSION = new DBVersion(1710, 0);
17
18     private DBVersion(BigInteger major, BigInteger minor) {
19         this.major = major;
20         this.minor = minor;
21     }
22
23     private DBVersion(int major, int minor) {
24         this.major = BigInteger.valueOf(major);
25         this.minor = BigInteger.valueOf(minor);
26     }
27
28     public BigInteger getMajor() {
29         return major;
30     }
31
32     public BigInteger getMinor() {
33         return minor;
34     }
35
36     public static DBVersion from(BigInteger major, BigInteger minor) {
37         return new DBVersion(major, minor);
38     }
39
40     public static DBVersion fromString(String version) {
41         String[] split = version.split(VERSION_PARTS_SEPARATOR);
42         if (split.length != 2) {
43             throw new MigrationException("version must be of pattern: <major>.<minor>");
44         }
45         return new DBVersion(getVersionPart(split[MAJOR_PART_IDX]),
46                              getVersionPart(split[MINOR_PART_IDX]));
47
48     }
49
50     private static BigInteger getVersionPart(String versionPart) {
51         try {
52             return new BigInteger(versionPart);
53         } catch (NumberFormatException e) {
54             throw new MigrationException(String.format("version part %s is non numeric", versionPart));
55         }
56     }
57
58     @Override
59     public String toString() {
60         return String.format("%s.%s", major, minor);
61     }
62
63     @Override
64     public boolean equals(Object o) {
65         if (this == o) return true;
66         if (o == null || getClass() != o.getClass()) return false;
67
68         DBVersion dbVersion = (DBVersion) o;
69
70         return major.equals(dbVersion.major) && minor.equals(dbVersion.minor);
71     }
72
73     @Override
74     public int hashCode() {
75         int result = major.hashCode();
76         result = 31 * result + minor.hashCode();
77         return result;
78     }
79
80     @Override
81     public int compareTo(DBVersion o) {
82         if (o == null) {
83             return 1;
84         }
85         int majorsComparision = this.major.compareTo(o.major);
86         if (majorsComparision != 0) {
87             return majorsComparision;
88         }
89         int minorsComparision = this.minor.compareTo(o.minor);
90         if (minorsComparision != 0) {
91             return minorsComparision;
92         }
93         return 0;
94     }
95 }