1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact.versioning;
20
21 import java.util.StringTokenizer;
22
23
24
25
26
27
28 public class DefaultArtifactVersion implements ArtifactVersion {
29 private Integer majorVersion;
30
31 private Integer minorVersion;
32
33 private Integer incrementalVersion;
34
35 private Integer buildNumber;
36
37 private String qualifier;
38
39 private ComparableVersion comparable;
40
41 public DefaultArtifactVersion(String version) {
42 parseVersion(version);
43 }
44
45 @Override
46 public int hashCode() {
47 return 11 + comparable.hashCode();
48 }
49
50 @Override
51 public boolean equals(Object other) {
52 if (this == other) {
53 return true;
54 }
55
56 if (!(other instanceof ArtifactVersion)) {
57 return false;
58 }
59
60 return compareTo((ArtifactVersion) other) == 0;
61 }
62
63 public int compareTo(ArtifactVersion otherVersion) {
64 if (otherVersion instanceof DefaultArtifactVersion) {
65 return this.comparable.compareTo(((DefaultArtifactVersion) otherVersion).comparable);
66 } else {
67 return compareTo(new DefaultArtifactVersion(otherVersion.toString()));
68 }
69 }
70
71 public int getMajorVersion() {
72 return majorVersion != null ? majorVersion : 0;
73 }
74
75 public int getMinorVersion() {
76 return minorVersion != null ? minorVersion : 0;
77 }
78
79 public int getIncrementalVersion() {
80 return incrementalVersion != null ? incrementalVersion : 0;
81 }
82
83 public int getBuildNumber() {
84 return buildNumber != null ? buildNumber : 0;
85 }
86
87 public String getQualifier() {
88 return qualifier;
89 }
90
91 public final void parseVersion(String version) {
92 comparable = new ComparableVersion(version);
93
94 int index = version.indexOf('-');
95
96 String part1;
97 String part2 = null;
98
99 if (index < 0) {
100 part1 = version;
101 } else {
102 part1 = version.substring(0, index);
103 part2 = version.substring(index + 1);
104 }
105
106 if (part2 != null) {
107 if (part2.length() == 1 || !part2.startsWith("0")) {
108 buildNumber = tryParseInt(part2);
109 if (buildNumber == null) {
110 qualifier = part2;
111 }
112 } else {
113 qualifier = part2;
114 }
115 }
116
117 if ((!part1.contains(".")) && !part1.startsWith("0")) {
118 majorVersion = tryParseInt(part1);
119 if (majorVersion == null) {
120
121 qualifier = version;
122 buildNumber = null;
123 }
124 } else {
125 boolean fallback = false;
126
127 StringTokenizer tok = new StringTokenizer(part1, ".");
128 if (tok.hasMoreTokens()) {
129 majorVersion = getNextIntegerToken(tok);
130 if (majorVersion == null) {
131 fallback = true;
132 }
133 } else {
134 fallback = true;
135 }
136 if (tok.hasMoreTokens()) {
137 minorVersion = getNextIntegerToken(tok);
138 if (minorVersion == null) {
139 fallback = true;
140 }
141 }
142 if (tok.hasMoreTokens()) {
143 incrementalVersion = getNextIntegerToken(tok);
144 if (incrementalVersion == null) {
145 fallback = true;
146 }
147 }
148 if (tok.hasMoreTokens()) {
149 qualifier = tok.nextToken();
150 fallback = isDigits(qualifier);
151 }
152
153
154 if (part1.contains("..") || part1.startsWith(".") || part1.endsWith(".")) {
155 fallback = true;
156 }
157
158 if (fallback) {
159
160 qualifier = version;
161 majorVersion = null;
162 minorVersion = null;
163 incrementalVersion = null;
164 buildNumber = null;
165 }
166 }
167 }
168
169 private static Integer getNextIntegerToken(StringTokenizer tok) {
170 String s = tok.nextToken();
171 if ((s.length() > 1) && s.startsWith("0")) {
172 return null;
173 }
174 return tryParseInt(s);
175 }
176
177 private static boolean isDigits(String str) {
178 if (str == null || str.trim().isEmpty()) {
179 return false;
180 }
181 for (int i = 0; i < str.length(); i++) {
182 char c = str.charAt(i);
183 if (!(c >= '0' && c <= '9')) {
184 return false;
185 }
186 }
187 return true;
188 }
189
190 private static Integer tryParseInt(String s) {
191
192 if (!isDigits(s)) {
193 return null;
194 }
195
196 try {
197 long longValue = Long.parseLong(s);
198 if (longValue > Integer.MAX_VALUE) {
199 return null;
200 }
201 return (int) longValue;
202 } catch (NumberFormatException e) {
203
204 return null;
205 }
206 }
207
208 @Override
209 public String toString() {
210 return comparable.toString();
211 }
212 }