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