d0efcf93fe36af4733f00525e3b59ad7ce054dc1
[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 import lombok.Getter;
26 import lombok.NoArgsConstructor;
27 import lombok.Setter;
28
29 import java.util.Date;
30 import java.util.Map;
31
32 @UDT(name = "version", keyspace = "dox")
33 @Getter
34 @Setter
35 @NoArgsConstructor
36 public class Version {
37   public static final String VERSION_STRING_VIOLATION_MSG =
38       "Version string must be in the format of: {integer}.{integer}";
39
40   @Transient
41   private String id;
42   private int major; // TODO: 6/7/2017 remove!
43   private int minor; // TODO: 6/7/2017 remove!
44   @Transient
45   private String name;
46   @Transient
47   private String description;
48   @Transient
49   private String baseId;
50   @Transient
51   private Date creationTime;
52   @Transient
53   private Date modificationTime;
54   @Transient
55   private VersionStatus status = VersionStatus.Draft;
56   @Transient
57   private VersionState state;
58   @Transient
59   private Map<String, Object> additionalInfo;
60
61   public Version(String id) {
62     this.id = id;
63   }
64
65   @Deprecated
66   public Version(int major, int minor) {
67     this.major = major;
68     this.minor = minor;
69   }
70
71   /**
72    * Value of version.
73    *
74    * @param versionString the version string
75    * @return the version
76    */
77   public static Version valueOf(String versionString) {
78     if (versionString == null) {
79       return null;
80     }
81     String[] versionLevels = versionString.split("\\.");
82     Version version;
83     if (versionLevels.length != 2) {
84       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
85     }
86     try {
87       version = new Version(Integer.parseInt(versionLevels[0]), Integer.parseInt(versionLevels[1]));
88     } catch (Exception ex) {
89       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
90     }
91
92     return version;
93   }
94   public Version calculateNextCandidate() {
95     return new Version(major, minor + 1);
96   }
97
98   public Version calculateNextFinal() {
99     return new Version(major + 1, 0);
100   }
101
102   @Transient
103   public boolean isFinal() {
104     return major != 0 && minor == 0;
105   }
106
107   @Override
108   public int hashCode() {
109     int result = major;
110     result = 31 * result + minor;
111     return result;
112   }
113
114   @Override
115   public boolean equals(Object obj) {
116     if (this == obj) {
117       return true;
118     }
119     if (obj == null || getClass() != obj.getClass()) {
120       return false;
121     }
122
123     Version version = (Version) obj;
124     return major == version.major && minor == version.minor;
125   }
126
127   public int compareTo(Version other){
128     if (this.major>other.major) {
129       return 1;
130     } else if(this.major<other.major){
131       return -1;
132     } else if(this.major == other.major){
133       return Integer.compare(this.minor,other.minor);
134     }
135     return 0;
136   }
137
138   @Override
139   public String toString() {
140     return name != null ? name : major + "." + minor;
141   }
142
143   @Override
144   public Version clone() {
145     Version version = new Version();
146     version.setStatus(this.getStatus());
147     version.setCreationTime(this.getCreationTime());
148     version.setName(this.getName());
149     version.setBaseId(this.getBaseId());
150     version.setMajor(this.major);
151     version.setMinor(this.minor);
152     version.setModificationTime(this.getModificationTime());
153     version.setDescription(this.description);
154     version.setId(this.getId());
155     return version;
156   }
157 }