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