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 org.eclipse.aether.version.InvalidVersionSpecificationException;
022
023/**
024 * A version scheme using a generic version syntax and common sense sorting.
025 * <p>
026 * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
027 * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
028 * version segments. Delimiters are treated as equivalent.
029 * </p>
030 * <p>
031 * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
032 * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" &lt;
033 * "beta" = "b" &lt; "milestone" = "m" &lt; "cr" = "rc" &lt; "snapshot" &lt; "final" = "ga" &lt; "sp". All of those
034 * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
035 * </p>
036 * <p>
037 * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
038 * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
039 * 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
040 * for "[M.N.min, M.N.max]".
041 * </p>
042 * <p>
043 * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
044 * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
045 * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" &lt; "1.0.1-ga" = "1.0.1".
046 * </p>
047 */
048public class GenericVersionScheme extends VersionSchemeSupport {
049    @Override
050    public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException {
051        return new GenericVersion(version);
052    }
053
054    /**
055     * A handy main method that behaves similarly like maven-artifact ComparableVersion is, to make possible test
056     * and possibly compare differences between the two.
057     * <p>
058     * To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue
059     * <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>
060     * command to command line, output is very similar to that of ComparableVersion on purpose.
061     */
062    public static void main(String... args) {
063        System.out.println(
064                "Display parameters as parsed by Maven Resolver 'generic' scheme (in canonical form and as a list of tokens)"
065                        + " and comparison result:");
066        if (args.length == 0) {
067            return;
068        }
069
070        GenericVersion prev = null;
071        int i = 1;
072        for (String version : args) {
073            GenericVersion c = new GenericVersion(version);
074
075            if (prev != null) {
076                int compare = prev.compareTo(c);
077                System.out.println(
078                        "   " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version);
079            }
080
081            System.out.println((i++) + ". " + version + " -> " + c.asString() + "; tokens: " + c.asItems());
082
083            prev = c;
084        }
085    }
086}