2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.model.tosca.version;
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
30 * http://www.apache.org/licenses/LICENSE-2.0
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
40 import java.util.StringTokenizer;
41 import java.util.regex.Pattern;
44 * Default implementation of artifact versioning.
46 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
48 public class Version implements Comparable<Version> {
50 private Integer majorVersion;
51 private Integer minorVersion;
52 private Integer incrementalVersion;
53 private Integer buildNumber;
54 private String qualifier;
55 private ComparableVersion comparable;
57 public Version(String version) {
58 parseVersion(version);
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 + "'");
66 return Integer.valueOf(s);
70 public int hashCode() {
71 return 11 + comparable.hashCode();
75 public boolean equals(Object other) {
79 if (!(other instanceof Version)) {
82 return compareTo((Version) other) == 0;
85 public int compareTo(Version otherVersion) {
86 return this.comparable.compareTo(otherVersion.comparable);
89 public int getMajorVersion() {
90 return majorVersion != null ? majorVersion : 0;
93 public int getMinorVersion() {
94 return minorVersion != null ? minorVersion : 0;
97 public int getIncrementalVersion() {
98 return incrementalVersion != null ? incrementalVersion : 0;
101 public int getBuildNumber() {
102 return buildNumber != null ? buildNumber : 0;
105 public String getQualifier() {
109 public final void parseVersion(String version) {
110 comparable = new ComparableVersion(version);
111 int index = version.indexOf("-");
117 part1 = version.substring(0, index);
118 part2 = version.substring(index + 1);
122 if ((part2.length() == 1) || !part2.startsWith("0")) {
123 buildNumber = Integer.valueOf(part2);
127 } catch (NumberFormatException e) {
131 if ((!part1.contains(".")) && !part1.startsWith("0")) {
133 majorVersion = Integer.valueOf(part1);
134 } catch (NumberFormatException e) {
135 // qualifier is the whole version, including "-"
140 boolean fallback = false;
141 StringTokenizer tok = new StringTokenizer(part1, ".");
143 majorVersion = getNextIntegerToken(tok);
144 if (tok.hasMoreTokens()) {
145 minorVersion = getNextIntegerToken(tok);
147 if (tok.hasMoreTokens()) {
148 incrementalVersion = getNextIntegerToken(tok);
150 if (tok.hasMoreTokens()) {
151 qualifier = tok.nextToken();
152 fallback = Pattern.compile("\\d+").matcher(qualifier).matches();
154 // string tokenzier won't detect these and ignores them
155 if (part1.contains("..") || part1.startsWith(".") || part1.endsWith(".")) {
158 } catch (NumberFormatException e) {
162 // qualifier is the whole version, including "-"
166 incrementalVersion = null;
173 public String toString() {
174 return comparable.toString();