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 org.eclipse.aether.version.InvalidVersionSpecificationException;
22  
23  /**
24   * A version scheme using a generic version syntax and common sense sorting.
25   * <p>
26   * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
27   * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
28   * version segments. Delimiters are treated as equivalent.
29   * </p>
30   * <p>
31   * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
32   * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" &lt;
33   * "beta" = "b" &lt; "milestone" = "m" &lt; "cr" = "rc" &lt; "snapshot" &lt; "final" = "ga" &lt; "sp". All of those
34   * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
35   * </p>
36   * <p>
37   * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
38   * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
39   * 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
40   * for "[M.N.min, M.N.max]".
41   * </p>
42   * <p>
43   * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
44   * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
45   * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" &lt; "1.0.1-ga" = "1.0.1".
46   * </p>
47   */
48  public final class GenericVersionScheme extends VersionSchemeSupport {
49      @Override
50      public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException {
51          return new GenericVersion(version);
52      }
53  
54      /**
55       * A handy main method that behaves similarly like maven-artifact ComparableVersion is, to make possible test
56       * and possibly compare differences between the two.
57       * <p>
58       * To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue
59       * <pre>jbang --main=org.eclipse.aether.util.version.GenericVersionScheme org.apache.maven.resolver:maven-resolver-util:1.9.18 "1.2.7" "1.2-SNAPSHOT"</pre>
60       * command to command line, output is very similar to that of ComparableVersion on purpose.
61       */
62      public static void main(String... args) {
63          System.out.println(
64                  "Display parameters as parsed by Maven Resolver 'generic' scheme (in canonical form and as a list of tokens)"
65                          + " and comparison result:");
66          if (args.length == 0) {
67              return;
68          }
69  
70          GenericVersion prev = null;
71          int i = 1;
72          for (String version : args) {
73              GenericVersion c = new GenericVersion(version);
74  
75              if (prev != null) {
76                  int compare = prev.compareTo(c);
77                  System.out.println(
78                          "   " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version);
79              }
80  
81              System.out.println((i++) + ". " + version + " -> " + c.asString() + "; tokens: " + c.asItems());
82  
83              prev = c;
84          }
85      }
86  }