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.nio.charset.StandardCharsets;
22  
23  import org.eclipse.aether.util.concurrency.ConcurrentWeakCache;
24  import org.eclipse.aether.version.InvalidVersionSpecificationException;
25  
26  /**
27   * A version scheme using a generic version syntax and common sense sorting.
28   * <p>
29   * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
30   * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
31   * version segments. Delimiters are treated as equivalent.
32   * </p>
33   * <p>
34   * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
35   * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" &lt;
36   * "beta" = "b" &lt; "milestone" = "m" &lt; "cr" = "rc" &lt; "snapshot" &lt; "final" = "ga" &lt; "sp". All of those
37   * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
38   * </p>
39   * <p>
40   * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
41   * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
42   * 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
43   * for "[M.N.min, M.N.max]".
44   * </p>
45   * <p>
46   * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
47   * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
48   * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" &lt; "1.0.1-ga" = "1.0.1".
49   * </p>
50   */
51  public class GenericVersionScheme extends VersionSchemeSupport {
52  
53      // Concurrent cache with weak keys and weak values: lock-free reads (volatile read, no lock
54      // acquisition via ConcurrentHashMap), lock-striped writes, zero allocation on get() via
55      // ThreadLocal lookup key, weak references allow GC under memory pressure.
56      private final ConcurrentWeakCache<String, GenericVersion> versionCache = new ConcurrentWeakCache<>();
57  
58      @Override
59      public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException {
60          GenericVersion v = versionCache.get(version);
61          if (v == null) {
62              v = versionCache.putIfAbsent(version, new GenericVersion(version));
63          }
64          return v;
65      }
66  
67      /**
68       * A handy main method that behaves similarly like maven-artifact ComparableVersion is, to make possible test
69       * and possibly compare differences between the two.
70       * <p>
71       * To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue
72       * <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>
73       * command to command line, output is very similar to that of ComparableVersion on purpose.
74       */
75      public static void main(String... args) {
76          System.out.println(
77                  "Display parameters as parsed by Maven Resolver 'generic' scheme (in canonical form and as a list of tokens)"
78                          + " and comparison result:");
79          if (args.length == 0) {
80              return;
81          }
82  
83          GenericVersionScheme scheme = new GenericVersionScheme();
84          GenericVersion prev = null;
85          int i = 1;
86          for (String version : args) {
87              if (!StandardCharsets.US_ASCII.newEncoder().canEncode(version)) {
88                  System.out.println("WW Use of non-ASCII characters for version strings is not recommended.");
89              }
90              try {
91                  GenericVersion c = scheme.parseVersion(version);
92  
93                  if (prev != null) {
94                      int compare = prev.compareTo(c);
95                      System.out.println(
96                              "   " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version);
97                  }
98  
99                  System.out.println((i++) + ". " + version + " -> " + c.asString() + "; tokens: " + c.asItems());
100 
101                 prev = c;
102             } catch (InvalidVersionSpecificationException e) {
103                 System.err.println("Invalid version: " + version + " - " + e.getMessage());
104             }
105         }
106     }
107 }