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.eclipse.aether.util.version;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  
24  import org.eclipse.aether.version.InvalidVersionSpecificationException;
25  import org.eclipse.aether.version.VersionScheme;
26  
27  import static java.util.Objects.requireNonNull;
28  
29  /**
30   * A version scheme using a generic version syntax and common sense sorting.
31   * <p>
32   * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
33   * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
34   * version segments. Delimiters are treated as equivalent.
35   * </p>
36   * <p>
37   * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
38   * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" &lt;
39   * "beta" = "b" &lt; "milestone" = "m" &lt; "cr" = "rc" &lt; "snapshot" &lt; "final" = "ga" &lt; "sp". All of those
40   * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
41   * </p>
42   * <p>
43   * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
44   * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
45   * the 1.2 line, "1.2.max" denotes the greatest version in the 1.2 line. A version range of the form "[M.N.*]" is short
46   * for "[M.N.min, M.N.max]".
47   * </p>
48   * <p>
49   * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
50   * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
51   * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" &lt; "1.0.1-ga" = "1.0.1".
52   * </p>
53   */
54  public final class GenericVersionScheme implements VersionScheme {
55  
56      /**
57       * Creates a new instance of the version scheme for parsing versions.
58       */
59      public GenericVersionScheme() {}
60  
61      @Override
62      public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException {
63          return new GenericVersion(version);
64      }
65  
66      @Override
67      public GenericVersionRange parseVersionRange(final String range) throws InvalidVersionSpecificationException {
68          return new GenericVersionRange(range);
69      }
70  
71      @Override
72      public GenericVersionConstraint parseVersionConstraint(final String constraint)
73              throws InvalidVersionSpecificationException {
74          String process = requireNonNull(constraint, "constraint cannot be null");
75  
76          Collection<GenericVersionRange> ranges = new ArrayList<>();
77  
78          while (process.startsWith("[") || process.startsWith("(")) {
79              int index1 = process.indexOf(')');
80              int index2 = process.indexOf(']');
81  
82              int index = index2;
83              if (index2 < 0 || (index1 >= 0 && index1 < index2)) {
84                  index = index1;
85              }
86  
87              if (index < 0) {
88                  throw new InvalidVersionSpecificationException(constraint, "Unbounded version range " + constraint);
89              }
90  
91              GenericVersionRange range = parseVersionRange(process.substring(0, index + 1));
92              ranges.add(range);
93  
94              process = process.substring(index + 1).trim();
95  
96              if (process.startsWith(",")) {
97                  process = process.substring(1).trim();
98              }
99          }
100 
101         if (process.length() > 0 && !ranges.isEmpty()) {
102             throw new InvalidVersionSpecificationException(
103                     constraint, "Invalid version range " + constraint + ", expected [ or ( but got " + process);
104         }
105 
106         GenericVersionConstraint result;
107         if (ranges.isEmpty()) {
108             result = new GenericVersionConstraint(parseVersion(constraint));
109         } else {
110             result = new GenericVersionConstraint(UnionVersionRange.from(ranges));
111         }
112 
113         return result;
114     }
115 
116     @Override
117     public boolean equals(final Object obj) {
118         if (this == obj) {
119             return true;
120         }
121 
122         return obj != null && getClass().equals(obj.getClass());
123     }
124 
125     @Override
126     public int hashCode() {
127         return getClass().hashCode();
128     }
129 
130     // CHECKSTYLE_OFF: LineLength
131     /**
132      * A handy main method that behaves similarly like maven-artifact ComparableVersion is, to make possible test
133      * and possibly compare differences between the two.
134      * <p>
135      * To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue
136      * <pre>java -cp ${maven.repo.local}/org/apache/maven/resolver/maven-resolver-api/${resolver.version}/maven-resolver-api-${resolver.version}.jar:${maven.repo.local}/org/apache/maven/resolver/maven-resolver-util/${resolver.version}/maven-resolver-util-${resolver.version}.jar org.eclipse.aether.util.version.GenericVersionScheme "1.2.7" "1.2-SNAPSHOT"</pre>
137      * command to command line, output is very similar to that of ComparableVersion on purpose.
138      */
139     // CHECKSTYLE_ON: LineLength
140     public static void main(String... args) {
141         System.out.println("Display parameters as parsed by Maven Resolver (in canonical form and as a list of tokens)"
142                 + " and comparison result:");
143         if (args.length == 0) {
144             return;
145         }
146 
147         GenericVersion prev = null;
148         int i = 1;
149         for (String version : args) {
150             GenericVersion c = new GenericVersion(version);
151 
152             if (prev != null) {
153                 int compare = prev.compareTo(c);
154                 System.out.println(
155                         "   " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version);
156             }
157 
158             System.out.println((i++) + ". " + version + " -> " + c.asString() + "; tokens: " + c.asItems());
159 
160             prev = c;
161         }
162     }
163 }