push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-api / src / main / java / org / openecomp / sdc / versioning / dao / types / Version.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.versioning.dao.types;
22
23 import com.datastax.driver.mapping.annotations.UDT;
24
25 @UDT(name = "version", keyspace = "dox")
26 public class Version {
27   public static final String VERSION_REGEX = "^\\d+\\.\\d+$";
28   public static final String VERSION_STRING_VIOLATION_MSG =
29       "Version string must be in the format of: {integer}.{integer}";
30
31   private int major;
32   private int minor;
33
34   public Version() {
35   }
36
37   public Version(int major, int minor) {
38     this.major = major;
39     this.minor = minor;
40   }
41
42   /**
43    * Value of version.
44    *
45    * @param versionString the version string
46    * @return the version
47    */
48   public static Version valueOf(String versionString) {
49     if (versionString == null) {
50       return null;
51     }
52     String[] versionLevels = versionString.split("\\.");
53     Version version;
54     if (versionLevels.length != 2) {
55       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
56     }
57     try {
58       version = new Version(Integer.parseInt(versionLevels[0]), Integer.parseInt(versionLevels[1]));
59     } catch (Exception exception) {
60       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
61     }
62
63     return version;
64   }
65
66   public int getMajor() {
67     return major;
68   }
69
70   public void setMajor(int major) {
71     this.major = major;
72   }
73
74   public int getMinor() {
75     return minor;
76   }
77
78   public void setMinor(int minor) {
79     this.minor = minor;
80   }
81
82   public Version calculateNextCandidate() {
83     return new Version(major, minor + 1);
84   }
85
86   public Version calculateNextFinal() {
87     return new Version(major + 1, 0);
88   }
89
90   public boolean isFinal() {
91     return major != 0 && minor == 0;
92   }
93
94   @Override
95   public boolean equals(Object obj) {
96     if (this == obj) {
97       return true;
98     }
99     if (obj == null || getClass() != obj.getClass()) {
100       return false;
101     }
102
103     Version version = (Version) obj;
104
105     return major == version.major && minor == version.minor;
106   }
107
108   @Override
109   public int hashCode() {
110     int result = major;
111     result = 31 * result + minor;
112     return result;
113   }
114
115   @Override
116   public String toString() {
117     return major + "." + minor;
118   }
119 }