[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.Transient;
24 import com.datastax.driver.mapping.annotations.UDT;
25
26 @UDT(name = "version", keyspace = "dox")
27 public class Version {
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   @Transient
34   private VersionStatus status = VersionStatus.Available;
35
36   public Version() {
37   }
38
39   public Version(int major, int minor) {
40     this.major = major;
41     this.minor = minor;
42   }
43
44   /**
45    * Value of version.
46    *
47    * @param versionString the version string
48    * @return the version
49    */
50   public static Version valueOf(String versionString) {
51     if (versionString == null) {
52       return null;
53     }
54     String[] versionLevels = versionString.split("\\.");
55     Version version;
56     if (versionLevels.length != 2) {
57       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
58     }
59     try {
60       version = new Version(Integer.parseInt(versionLevels[0]), Integer.parseInt(versionLevels[1]));
61     } catch (Exception ex) {
62       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
63     }
64
65     return version;
66   }
67
68   public int getMajor() {
69     return major;
70   }
71
72   public void setMajor(int major) {
73     this.major = major;
74   }
75
76   public int getMinor() {
77     return minor;
78   }
79
80   public void setMinor(int minor) {
81     this.minor = minor;
82   }
83
84   public VersionStatus getStatus() {
85     return status;
86   }
87
88   public void setStatus(VersionStatus status) {
89     this.status = status;
90   }
91
92   public Version calculateNextCandidate() {
93     return new Version(major, minor + 1);
94   }
95
96   public Version calculateNextFinal() {
97     return new Version(major + 1, 0);
98   }
99
100   public boolean isFinal() {
101     return major != 0 && minor == 0;
102   }
103
104   @Override
105   public int hashCode() {
106     int result = major;
107     result = 31 * result + minor;
108     return result;
109   }
110
111   @Override
112   public boolean equals(Object obj) {
113     if (this == obj) {
114       return true;
115     }
116     if (obj == null || getClass() != obj.getClass()) {
117       return false;
118     }
119
120     Version version = (Version) obj;
121     return major == version.major && minor == version.minor;
122   }
123
124   @Override
125   public String toString() {
126     return major + "." + minor;
127   }
128 }