b822f13c76a256880e5bd3067fb7df5c20ee7e87
[sdc.git] /
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 import java.util.Date;
27 import java.util.Map;
28
29 @UDT(name = "version", keyspace = "dox")
30 public class Version {
31   public static final String VERSION_STRING_VIOLATION_MSG =
32       "Version string must be in the format of: {integer}.{integer}";
33
34   @Transient
35   private String id;
36   private int major; // TODO: 6/7/2017 remove!
37   private int minor; // TODO: 6/7/2017 remove!
38   @Transient
39   private String name;
40   @Transient
41   private String description;
42   @Transient
43   private String baseId;
44   @Transient
45   private Date creationTime;
46   @Transient
47   private Date modificationTime;
48   @Transient
49   private VersionStatus status = VersionStatus.Draft;
50   @Transient
51   private VersionState state;
52   @Transient
53   private Map<String, Object> additionalInfo;
54
55   public Version() {
56   }
57
58   public Version(String id) {
59     this.id = id;
60   }
61
62   @Deprecated
63   public Version(int major, int minor) {
64     this.major = major;
65     this.minor = minor;
66   }
67
68   /**
69    * Value of version.
70    *
71    * @param versionString the version string
72    * @return the version
73    */
74   public static Version valueOf(String versionString) {
75     if (versionString == null) {
76       return null;
77     }
78     String[] versionLevels = versionString.split("\\.");
79     Version version;
80     if (versionLevels.length != 2) {
81       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
82     }
83     try {
84       version = new Version(Integer.parseInt(versionLevels[0]), Integer.parseInt(versionLevels[1]));
85     } catch (Exception ex) {
86       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
87     }
88
89     return version;
90   }
91
92   public int getMajor() {
93     return major;
94   }
95
96   public void setMajor(int major) {
97     this.major = major;
98   }
99
100   public int getMinor() {
101     return minor;
102   }
103
104   public void setMinor(int minor) {
105     this.minor = minor;
106   }
107
108   public String getId() {
109     return id;
110   }
111
112   public void setId(String id) {
113     this.id = id;
114   }
115
116   public String getName() {
117     return name;
118   }
119
120   public void setName(String name) {
121     this.name = name;
122   }
123
124   public String getDescription() {
125     return description;
126   }
127
128   public void setDescription(String description) {
129     this.description = description;
130   }
131
132   public String getBaseId() {
133     return baseId;
134   }
135
136   public void setBaseId(String baseId) {
137     this.baseId = baseId;
138   }
139
140   public Date getCreationTime() {
141     return creationTime;
142   }
143
144   public void setCreationTime(Date creationTime) {
145     this.creationTime = creationTime;
146   }
147
148   public Date getModificationTime() {
149     return modificationTime;
150   }
151
152   public void setModificationTime(Date modificationTime) {
153     this.modificationTime = modificationTime;
154   }
155
156   public VersionStatus getStatus() {
157     return status;
158   }
159
160   public void setStatus(VersionStatus status) {
161     this.status = status;
162   }
163
164   public VersionState getState() {
165     return state;
166   }
167
168   public void setState(VersionState state) {
169     this.state = state;
170   }
171
172   public Version calculateNextCandidate() {
173     return new Version(major, minor + 1);
174   }
175
176   public Version calculateNextFinal() {
177     return new Version(major + 1, 0);
178   }
179
180   @Transient
181   public boolean isFinal() {
182     return major != 0 && minor == 0;
183   }
184
185   public Map<String, Object> getAdditionalInfo() {
186     return additionalInfo;
187   }
188
189   public void setAdditionalInfo(Map<String, Object> additionalInfo) {
190     this.additionalInfo = additionalInfo;
191   }
192
193   @Override
194   public int hashCode() {
195     int result = major;
196     result = 31 * result + minor;
197     return result;
198   }
199
200   @Override
201   public boolean equals(Object obj) {
202     if (this == obj) {
203       return true;
204     }
205     if (obj == null || getClass() != obj.getClass()) {
206       return false;
207     }
208
209     Version version = (Version) obj;
210     return major == version.major && minor == version.minor;
211   }
212
213   public int compareTo(Version other){
214     if (this.major>other.major) {
215       return 1;
216     } else if(this.major<other.major){
217       return -1;
218     } else if(this.major == other.major){
219       return Integer.compare(this.minor,other.minor);
220     }
221     return 0;
222   }
223
224   @Override
225   public String toString() {
226     return name != null ? name : major + "." + minor;
227   }
228
229   @Override
230   public Version clone() {
231     Version version = new Version();
232     version.setStatus(this.getStatus());
233     version.setCreationTime(this.getCreationTime());
234     version.setName(this.getName());
235     version.setBaseId(this.getBaseId());
236     version.setMajor(this.major);
237     version.setMinor(this.minor);
238     version.setModificationTime(this.getModificationTime());
239     version.setDescription(this.description);
240     version.setId(this.getId());
241     return version;
242   }
243 }