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.util.ArrayList; 022import java.util.Collection; 023 024import org.eclipse.aether.version.InvalidVersionSpecificationException; 025import org.eclipse.aether.version.VersionScheme; 026 027import static java.util.Objects.requireNonNull; 028 029/** 030 * A version scheme using a generic version syntax and common sense sorting. 031 * <p> 032 * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments. 033 * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the 034 * version segments. Delimiters are treated as equivalent. 035 * </p> 036 * <p> 037 * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and 038 * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" < 039 * "beta" = "b" < "milestone" = "m" < "cr" = "rc" < "snapshot" < "final" = "ga" < "sp". All of those 040 * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0. 041 * </p> 042 * <p> 043 * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to 044 * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in 045 * 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 046 * for "[M.N.min, M.N.max]". 047 * </p> 048 * <p> 049 * Numbers and strings are considered incomparable against each other. Where version segments of different kind would 050 * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments, 051 * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" < "1.0.1-ga" = "1.0.1". 052 * </p> 053 */ 054public final class GenericVersionScheme implements VersionScheme { 055 056 /** 057 * Creates a new instance of the version scheme for parsing versions. 058 */ 059 public GenericVersionScheme() {} 060 061 @Override 062 public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException { 063 return new GenericVersion(version); 064 } 065 066 @Override 067 public GenericVersionRange parseVersionRange(final String range) throws InvalidVersionSpecificationException { 068 return new GenericVersionRange(range); 069 } 070 071 @Override 072 public GenericVersionConstraint parseVersionConstraint(final String constraint) 073 throws InvalidVersionSpecificationException { 074 String process = requireNonNull(constraint, "constraint cannot be null"); 075 076 Collection<GenericVersionRange> ranges = new ArrayList<>(); 077 078 while (process.startsWith("[") || process.startsWith("(")) { 079 int index1 = process.indexOf(')'); 080 int index2 = process.indexOf(']'); 081 082 int index = index2; 083 if (index2 < 0 || (index1 >= 0 && index1 < index2)) { 084 index = index1; 085 } 086 087 if (index < 0) { 088 throw new InvalidVersionSpecificationException(constraint, "Unbounded version range " + constraint); 089 } 090 091 GenericVersionRange range = parseVersionRange(process.substring(0, index + 1)); 092 ranges.add(range); 093 094 process = process.substring(index + 1).trim(); 095 096 if (process.startsWith(",")) { 097 process = process.substring(1).trim(); 098 } 099 } 100 101 if (!process.isEmpty() && !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}