82b79c6bda374365f1bd7e0ac4834a09eee8a744
[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 org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27
28 @UDT(name = "version", keyspace = "dox")
29 public class Version {
30   private static final Logger logger = LoggerFactory.getLogger(Version.class);
31   public static final String VERSION_STRING_VIOLATION_MSG =
32       "Version string must be in the format of: {integer}.{integer}";
33
34   private int major;
35   private int minor;
36   @Transient
37   private VersionStatus status = VersionStatus.Available;
38
39   public Version() {
40   }
41
42   public Version(int major, int minor) {
43     this.major = major;
44     this.minor = minor;
45   }
46
47   /**
48    * Value of version.
49    *
50    * @param versionString the version string
51    * @return the version
52    */
53   public static Version valueOf(String versionString) {
54     if (versionString == null) {
55       return null;
56     }
57     String[] versionLevels = versionString.split("\\.");
58     Version version;
59     if (versionLevels.length != 2) {
60       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
61     }
62     try {
63       version = new Version(Integer.parseInt(versionLevels[0]), Integer.parseInt(versionLevels[1]));
64     } catch (Exception ex) {
65       logger.debug(ex.getMessage(), ex);
66       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
67     }
68
69     return version;
70   }
71
72   public int getMajor() {
73     return major;
74   }
75
76   public void setMajor(int major) {
77     this.major = major;
78   }
79
80   public int getMinor() {
81     return minor;
82   }
83
84   public void setMinor(int minor) {
85     this.minor = minor;
86   }
87
88   public VersionStatus getStatus() {
89     return status;
90   }
91
92   public void setStatus(VersionStatus status) {
93     this.status = status;
94   }
95
96   public Version calculateNextCandidate() {
97     return new Version(major, minor + 1);
98   }
99
100   public Version calculateNextFinal() {
101     return new Version(major + 1, 0);
102   }
103
104   public boolean isFinal() {
105     return major != 0 && minor == 0;
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 boolean equals(Object obj) {
117     if (this == obj) {
118       return true;
119     }
120     if (obj == null || getClass() != obj.getClass()) {
121       return false;
122     }
123
124     Version version = (Version) obj;
125     return major == version.major && minor == version.minor;
126   }
127
128   public int compateTo(Version other){
129     if (this.major>other.major) {
130       return 1;
131     } else if(this.major<other.major){
132       return -1;
133     } else if(this.major == other.major){
134       return Integer.compare(this.minor,other.minor);
135     }
136     return 0;
137   }
138
139   @Override
140   public String toString() {
141     return major + "." + minor;
142   }
143 }