e37b9d92482cf60e11f26c2f44d19012a1e46887
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.database.data;
23
24 import java.text.ParseException;
25 import org.eclipse.jdt.annotation.NonNull;
26
27
28 /**
29  * @author Michael Dürre
30  *
31  */
32 public class DatabaseVersion {
33
34     private final String raw;
35     private final int major;
36     private final int minor;
37     private final int revision;
38
39     public DatabaseVersion(@NonNull String version) throws ParseException {
40         String[] hlp = version.split("\\.");
41         if (hlp.length < 3) {
42             throw new ParseException("unable to parse version string: " + version, 0);
43         }
44         this.raw = version;
45         this.major = Integer.parseInt(hlp[0]);
46         this.minor = Integer.parseInt(hlp[1]);
47         this.revision = Integer.parseInt(hlp[2]);
48     }
49
50     @Override
51     public String toString() {
52         return String.format("%d.%d.%d", this.major, this.minor, this.revision);
53     }
54
55     /**
56      * @param major
57      * @param minor
58      * @param revision
59      */
60     public DatabaseVersion(int major, int minor, int revision) {
61         this.raw = String.format("%d.%d.%d", major, minor, revision);
62         this.major = major;
63         this.minor = minor;
64         this.revision = revision;
65     }
66
67     /**
68      * @return the revision
69      */
70     public int getRevision() {
71         return revision;
72     }
73
74     /**
75      * @return the minor
76      */
77     public int getMinor() {
78         return minor;
79     }
80
81     /**
82      * @return the major
83      */
84     public int getMajor() {
85         return major;
86     }
87
88     @Override
89     public boolean equals(Object obj) {
90         if (!(obj instanceof DatabaseVersion)) {
91             return false;
92         }
93         DatabaseVersion esobj = (DatabaseVersion) obj;
94         return this.major == esobj.major && this.minor == esobj.minor && this.revision == esobj.revision;
95     }
96
97     @Override
98     public int hashCode() {
99         return this.raw.hashCode();
100     }
101
102     public boolean isNewerOrEqualThan(DatabaseVersion v) {
103         if (this.equals(v)) {
104             return true;
105         }
106         return this.isNewerThan(v);
107     }
108     public boolean isNewerThan(DatabaseVersion v) {
109         if (this.major > v.major) {
110             return true;
111         } else if (this.major < v.major) {
112             return false;
113         }
114         if (this.minor > v.minor) {
115             return true;
116         } else if (this.minor < v.minor) {
117             return false;
118         }
119         if (this.revision > v.revision) {
120             return true;
121         }
122         return false;
123     }
124
125     public boolean isOlderOrEqualThan(DatabaseVersion v) {
126         if (this.equals(v)) {
127             return true;
128         }
129         return this.isOlderThan(v);
130     }
131
132     public boolean isOlderThan(DatabaseVersion v) {
133         if (this.major < v.major) {
134             return true;
135         } else if (this.major > v.major) {
136             return false;
137         }
138         if (this.minor < v.minor) {
139             return true;
140         } else if (this.minor > v.minor) {
141             return false;
142         }
143         if (this.revision < v.revision) {
144             return true;
145         }
146         return false;
147     }
148
149 }