Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / version / 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 package org.openecomp.sdc.be.model.tosca.version;
21 /*
22  * Licensed to the Apache Software Foundation (ASF) under one
23  * or more contributor license agreements.  See the NOTICE file
24  * distributed with this work for additional information
25  * regarding copyright ownership.  The ASF licenses this file
26  * to you under the Apache License, Version 2.0 (the
27  * "License"); you may not use this file except in compliance
28  * with the License.  You may obtain a copy of the License at
29  *
30  *  http://www.apache.org/licenses/LICENSE-2.0
31  *
32  * Unless required by applicable law or agreed to in writing,
33  * software distributed under the License is distributed on an
34  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35  * KIND, either express or implied.  See the License for the
36  * specific language governing permissions and limitations
37  * under the License.
38  */
39
40 import java.util.StringTokenizer;
41 import java.util.regex.Pattern;
42
43 /**
44  * Default implementation of artifact versioning.
45  *
46  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
47  */
48 public class Version implements Comparable<Version> {
49
50     private Integer majorVersion;
51     private Integer minorVersion;
52     private Integer incrementalVersion;
53     private Integer buildNumber;
54     private String qualifier;
55     private ComparableVersion comparable;
56
57     public Version(String version) {
58         parseVersion(version);
59     }
60
61     private static Integer getNextIntegerToken(StringTokenizer tok) {
62         String s = tok.nextToken();
63         if ((s.length() > 1) && s.startsWith("0")) {
64             throw new NumberFormatException("Number part has a leading 0: '" + s + "'");
65         }
66         return Integer.valueOf(s);
67     }
68
69     @Override
70     public int hashCode() {
71         return 11 + comparable.hashCode();
72     }
73
74     @Override
75     public boolean equals(Object other) {
76         if (this == other) {
77             return true;
78         }
79         if (!(other instanceof Version)) {
80             return false;
81         }
82         return compareTo((Version) other) == 0;
83     }
84
85     public int compareTo(Version otherVersion) {
86         return this.comparable.compareTo(otherVersion.comparable);
87     }
88
89     public int getMajorVersion() {
90         return majorVersion != null ? majorVersion : 0;
91     }
92
93     public int getMinorVersion() {
94         return minorVersion != null ? minorVersion : 0;
95     }
96
97     public int getIncrementalVersion() {
98         return incrementalVersion != null ? incrementalVersion : 0;
99     }
100
101     public int getBuildNumber() {
102         return buildNumber != null ? buildNumber : 0;
103     }
104
105     public String getQualifier() {
106         return qualifier;
107     }
108
109     public final void parseVersion(String version) {
110         comparable = new ComparableVersion(version);
111         int index = version.indexOf("-");
112         String part1;
113         String part2 = null;
114         if (index < 0) {
115             part1 = version;
116         } else {
117             part1 = version.substring(0, index);
118             part2 = version.substring(index + 1);
119         }
120         if (part2 != null) {
121             try {
122                 if ((part2.length() == 1) || !part2.startsWith("0")) {
123                     buildNumber = Integer.valueOf(part2);
124                 } else {
125                     qualifier = part2;
126                 }
127             } catch (NumberFormatException e) {
128                 qualifier = part2;
129             }
130         }
131         if ((!part1.contains(".")) && !part1.startsWith("0")) {
132             try {
133                 majorVersion = Integer.valueOf(part1);
134             } catch (NumberFormatException e) {
135                 // qualifier is the whole version, including "-"
136                 qualifier = version;
137                 buildNumber = null;
138             }
139         } else {
140             boolean fallback = false;
141             StringTokenizer tok = new StringTokenizer(part1, ".");
142             try {
143                 majorVersion = getNextIntegerToken(tok);
144                 if (tok.hasMoreTokens()) {
145                     minorVersion = getNextIntegerToken(tok);
146                 }
147                 if (tok.hasMoreTokens()) {
148                     incrementalVersion = getNextIntegerToken(tok);
149                 }
150                 if (tok.hasMoreTokens()) {
151                     qualifier = tok.nextToken();
152                     fallback = Pattern.compile("\\d+").matcher(qualifier).matches();
153                 }
154                 // string tokenzier won't detect these and ignores them
155                 if (part1.contains("..") || part1.startsWith(".") || part1.endsWith(".")) {
156                     fallback = true;
157                 }
158             } catch (NumberFormatException e) {
159                 fallback = true;
160             }
161             if (fallback) {
162                 // qualifier is the whole version, including "-"
163                 qualifier = version;
164                 majorVersion = null;
165                 minorVersion = null;
166                 incrementalVersion = null;
167                 buildNumber = null;
168             }
169         }
170     }
171
172     @Override
173     public String toString() {
174         return comparable.toString();
175     }
176 }