001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.version; 020 021import java.nio.charset.StandardCharsets; 022 023import org.eclipse.aether.util.concurrency.ConcurrentWeakCache; 024import org.eclipse.aether.version.InvalidVersionSpecificationException; 025 026/** 027 * A version scheme using a generic version syntax and common sense sorting. 028 * <p> 029 * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments. 030 * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the 031 * version segments. Delimiters are treated as equivalent. 032 * </p> 033 * <p> 034 * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and 035 * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" < 036 * "beta" = "b" < "milestone" = "m" < "cr" = "rc" < "snapshot" < "final" = "ga" < "sp". All of those 037 * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0. 038 * </p> 039 * <p> 040 * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to 041 * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in 042 * 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 043 * for "[M.N.min, M.N.max]". 044 * </p> 045 * <p> 046 * Numbers and strings are considered incomparable against each other. Where version segments of different kind would 047 * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments, 048 * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" < "1.0.1-ga" = "1.0.1". 049 * </p> 050 */ 051public class GenericVersionScheme extends VersionSchemeSupport { 052 053 // Concurrent cache with weak keys and weak values: lock-free reads (volatile read, no lock 054 // acquisition via ConcurrentHashMap), lock-striped writes, zero allocation on get() via 055 // ThreadLocal lookup key, weak references allow GC under memory pressure. 056 private final ConcurrentWeakCache<String, GenericVersion> versionCache = new ConcurrentWeakCache<>(); 057 058 @Override 059 public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException { 060 GenericVersion v = versionCache.get(version); 061 if (v == null) { 062 v = versionCache.putIfAbsent(version, new GenericVersion(version)); 063 } 064 return v; 065 } 066 067 /** 068 * A handy main method that behaves similarly like maven-artifact ComparableVersion is, to make possible test 069 * and possibly compare differences between the two. 070 * <p> 071 * To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue 072 * <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> 073 * command to command line, output is very similar to that of ComparableVersion on purpose. 074 */ 075 public static void main(String... args) { 076 System.out.println( 077 "Display parameters as parsed by Maven Resolver 'generic' scheme (in canonical form and as a list of tokens)" 078 + " and comparison result:"); 079 if (args.length == 0) { 080 return; 081 } 082 083 GenericVersionScheme scheme = new GenericVersionScheme(); 084 GenericVersion prev = null; 085 int i = 1; 086 for (String version : args) { 087 if (!StandardCharsets.US_ASCII.newEncoder().canEncode(version)) { 088 System.out.println("WW Use of non-ASCII characters for version strings is not recommended."); 089 } 090 try { 091 GenericVersion c = scheme.parseVersion(version); 092 093 if (prev != null) { 094 int compare = prev.compareTo(c); 095 System.out.println( 096 " " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version); 097 } 098 099 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}