2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.asdctool.migration.core;
23 import java.math.BigInteger;
25 public class DBVersion implements Comparable<DBVersion>{
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;
34 * The current db version. should be tested against real db to verify it is compatible to the db version
36 public static final DBVersion DEFAULT_VERSION = new DBVersion(1710, 0);
38 private DBVersion(BigInteger major, BigInteger minor) {
43 private DBVersion(int major, int minor) {
44 this.major = BigInteger.valueOf(major);
45 this.minor = BigInteger.valueOf(minor);
48 public BigInteger getMajor() {
52 public BigInteger getMinor() {
56 public static DBVersion from(BigInteger major, BigInteger minor) {
57 return new DBVersion(major, minor);
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>");
65 return new DBVersion(getVersionPart(split[MAJOR_PART_IDX]),
66 getVersionPart(split[MINOR_PART_IDX]));
70 private static BigInteger getVersionPart(String versionPart) {
72 return new BigInteger(versionPart);
73 } catch (NumberFormatException e) {
74 throw new MigrationException(String.format("version part %s is non numeric", versionPart));
79 public String toString() {
80 return String.format("%s.%s", major, minor);
84 public boolean equals(Object o) {
85 if (this == o) return true;
86 if (o == null || getClass() != o.getClass()) return false;
88 DBVersion dbVersion = (DBVersion) o;
90 return major.equals(dbVersion.major) && minor.equals(dbVersion.minor);
94 public int hashCode() {
95 int result = major.hashCode();
96 result = 31 * result + minor.hashCode();
101 public int compareTo(DBVersion o) {
105 int majorsComparision = this.major.compareTo(o.major);
106 if (majorsComparision != 0) {
107 return majorsComparision;
109 int minorsComparision = this.minor.compareTo(o.minor);
110 if (minorsComparision != 0) {
111 return minorsComparision;