View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.versioning;
20  
21  /**
22   * Default implementation of artifact versioning.
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 artifactVersion) {
54              return compareTo(artifactVersion) == 0;
55          }
56  
57          return false;
58      }
59  
60      @Override
61      public int compareTo(ArtifactVersion otherVersion) {
62          if (otherVersion instanceof DefaultArtifactVersion defaultArtifactVersion) {
63              return this.comparable.compareTo(defaultArtifactVersion.comparable);
64          } else {
65              return compareTo(new DefaultArtifactVersion(otherVersion.toString()));
66          }
67      }
68  
69      @Override
70      public int getMajorVersion() {
71          return majorVersion != null ? majorVersion : 0;
72      }
73  
74      @Override
75      public int getMinorVersion() {
76          return minorVersion != null ? minorVersion : 0;
77      }
78  
79      @Override
80      public int getIncrementalVersion() {
81          return incrementalVersion != null ? incrementalVersion : 0;
82      }
83  
84      @Override
85      public int getBuildNumber() {
86          return buildNumber != null ? buildNumber : 0;
87      }
88  
89      @Override
90      public String getQualifier() {
91          return qualifier;
92      }
93  
94      @Override
95      public final void parseVersion(String version) {
96          comparable = new ComparableVersion(version);
97  
98          int index = version.indexOf('-');
99  
100         String part1;
101         String part2 = null;
102 
103         if (index < 0) {
104             part1 = version;
105         } else {
106             part1 = version.substring(0, index);
107             part2 = version.substring(index + 1);
108         }
109 
110         if (part2 != null) {
111             if (part2.length() == 1 || !part2.startsWith("0")) {
112                 buildNumber = tryParseInt(part2);
113                 if (buildNumber == null) {
114                     qualifier = part2;
115                 }
116             } else {
117                 qualifier = part2;
118             }
119         }
120 
121         if ((!part1.contains(".")) && !part1.startsWith("0")) {
122             majorVersion = tryParseInt(part1);
123             if (majorVersion == null) {
124                 // qualifier is the whole version, including "-"
125                 qualifier = version;
126                 buildNumber = null;
127             }
128         } else {
129             boolean fallback = false;
130 
131             String[] tok = part1.split("\\.");
132             int idx = 0;
133             if (idx < tok.length) {
134                 majorVersion = getNextIntegerToken(tok[idx++]);
135                 if (majorVersion == null) {
136                     fallback = true;
137                 }
138             } else {
139                 fallback = true;
140             }
141             if (idx < tok.length) {
142                 minorVersion = getNextIntegerToken(tok[idx++]);
143                 if (minorVersion == null) {
144                     fallback = true;
145                 }
146             }
147             if (idx < tok.length) {
148                 incrementalVersion = getNextIntegerToken(tok[idx++]);
149                 if (incrementalVersion == null) {
150                     fallback = true;
151                 }
152             }
153             if (idx < tok.length) {
154                 qualifier = tok[idx];
155                 fallback = isDigits(qualifier);
156             }
157 
158             // string tokenizer won't detect these and ignores them
159             if (part1.contains("..") || part1.startsWith(".") || part1.endsWith(".")) {
160                 fallback = true;
161             }
162 
163             if (fallback) {
164                 // qualifier is the whole version, including "-"
165                 qualifier = version;
166                 majorVersion = null;
167                 minorVersion = null;
168                 incrementalVersion = null;
169                 buildNumber = null;
170             }
171         }
172     }
173 
174     private static boolean isDigits(String cs) {
175         if (cs == null || cs.isEmpty()) {
176             return false;
177         }
178         final int sz = cs.length();
179         for (int i = 0; i < sz; i++) {
180             if (!Character.isDigit(cs.charAt(i))) {
181                 return false;
182             }
183         }
184         return true;
185     }
186 
187     private static Integer getNextIntegerToken(String s) {
188         if ((s.length() > 1) && s.startsWith("0")) {
189             return null;
190         }
191         return tryParseInt(s);
192     }
193 
194     private static Integer tryParseInt(String s) {
195         // for performance, check digits instead of relying later on catching NumberFormatException
196         if (!isDigits(s)) {
197             return null;
198         }
199 
200         try {
201             long longValue = Long.parseLong(s);
202             if (longValue > Integer.MAX_VALUE) {
203                 return null;
204             }
205             return (int) longValue;
206         } catch (NumberFormatException e) {
207             // should never happen since checked isDigits(s) before
208             return null;
209         }
210     }
211 
212     @Override
213     public String toString() {
214         return comparable.toString();
215     }
216 }