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