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   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
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                 // qualifier is the whole version, including "-"
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             // string tokenizer won't detect these and ignores them
154             if (part1.contains("..") || part1.startsWith(".") || part1.endsWith(".")) {
155                 fallback = true;
156             }
157 
158             if (fallback) {
159                 // qualifier is the whole version, including "-"
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         // for performance, check digits instead of relying later on catching NumberFormatException
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             // should never happen since checked isDigits(s) before
204             return null;
205         }
206     }
207 
208     @Override
209     public String toString() {
210         return comparable.toString();
211     }
212 }