/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.maven.artifact.versioning; import java.math.BigInteger; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Deque; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Objects; import java.util.Properties; /** *
* Generic implementation of version comparison. *
** Features: *
-
' (hyphen) and '.
' (dot) separators,1.0alpha1 => [1, [alpha, 1]]
alpha
or a
beta
or b
milestone
or m
rc
or cr
snapshot
(the empty string)
or ga
or final
sp
* This method takes into account the ordering of known qualifiers then unknown qualifiers with lexical * ordering. *
*
* @param qualifier
* @return an equivalent value that can be used with lexical comparison
*/
public static String comparableQualifier(String qualifier) {
if (RELEASE_QUALIFIERS.contains(qualifier)) {
return String.valueOf(QUALIFIERS.indexOf(""));
}
int i = QUALIFIERS.indexOf(qualifier);
// Just returning an Integer with the index here is faster, but requires a lot of if/then/else to check for
// -1
// or QUALIFIERS.size and then resort to lexical ordering. Most comparisons are decided by the first
// character,
// so this is still fast. If more characters are needed then it requires a lexical sort anyway.
return i == -1 ? (QUALIFIERS.size() + "-" + qualifier) : String.valueOf(i);
}
@Override
public int compareTo(Item item) {
if (item == null) {
// 1-rc < 1, 1-ga > 1
return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX);
}
switch (item.getType()) {
case INT_ITEM:
case LONG_ITEM:
case BIGINTEGER_ITEM:
return -1; // 1.any < 1.1 ?
case STRING_ITEM:
return comparableQualifier(value).compareTo(comparableQualifier(((StringItem) item).value));
case COMBINATION_ITEM:
int result = this.compareTo(((CombinationItem) item).getStringPart());
if (result == 0) {
return -1;
}
return result;
case LIST_ITEM:
return -1; // 1.any < 1-1
default:
throw new IllegalStateException("invalid item: " + item.getClass());
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StringItem that = (StringItem) o;
return value.equals(that.value);
}
@Override
public int hashCode() {
return value.hashCode();
}
public String toString() {
return value;
}
}
/**
* Represents a combination in the version item list.
* It is usually a combination of a string and a number, with the string first and the number second.
*/
private static class CombinationItem implements Item {
StringItem stringPart;
Item digitPart;
CombinationItem(String value) {
int index = 0;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (Character.isDigit(c)) {
index = i;
break;
}
}
stringPart = new StringItem(value.substring(0, index), true);
digitPart = parseItem(true, value.substring(index));
}
@Override
public int compareTo(Item item) {
if (item == null) {
// 1-rc1 < 1, 1-ga1 > 1
return stringPart.compareTo(item);
}
int result = 0;
switch (item.getType()) {
case INT_ITEM:
case LONG_ITEM:
case BIGINTEGER_ITEM:
return -1;
case STRING_ITEM:
result = stringPart.compareTo(item);
if (result == 0) {
// X1 > X
return 1;
}
return result;
case LIST_ITEM:
return -1;
case COMBINATION_ITEM:
result = stringPart.compareTo(((CombinationItem) item).getStringPart());
if (result == 0) {
return digitPart.compareTo(((CombinationItem) item).getDigitPart());
}
return result;
default:
return 0;
}
}
public StringItem getStringPart() {
return stringPart;
}
public Item getDigitPart() {
return digitPart;
}
@Override
public int getType() {
return COMBINATION_ITEM;
}
@Override
public boolean isNull() {
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CombinationItem that = (CombinationItem) o;
return Objects.equals(stringPart, that.stringPart) && Objects.equals(digitPart, that.digitPart);
}
@Override
public int hashCode() {
return Objects.hash(stringPart, digitPart);
}
@Override
public String toString() {
return stringPart.toString() + digitPart.toString();
}
}
/**
* Represents a version list item. This class is used both for the global item list and for sub-lists (which start
* with '-(number)' in the version specification).
*/
private static class ListItem extends ArrayList
* To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue
* java -jar ${maven.repo.local}/org/apache/maven/maven-artifact/${maven.version}/maven-artifact-${maven.version}.jar "1.2.7" "1.2-SNAPSHOT"
* command to command line. Result of given command will be something like this:
*
* Display parameters as parsed by Maven (in canonical form) and comparison result:
* 1. 1.2.7 == 1.2.7
* 1.2.7 > 1.2-SNAPSHOT
* 2. 1.2-SNAPSHOT == 1.2-snapshot
*
*
* @param args the version strings to parse and compare. You can pass arbitrary number of version strings and always
* two adjacent will be compared.
*/
// CHECKSTYLE_ON: LineLength
public static void main(String... args) {
System.out.println("Display parameters as parsed by Maven (in canonical form and as a list of tokens) and"
+ " comparison result:");
if (args.length == 0) {
return;
}
ComparableVersion prev = null;
int i = 1;
for (String version : args) {
ComparableVersion c = new ComparableVersion(version);
if (prev != null) {
int compare = prev.compareTo(c);
System.out.println(" " + prev.toString() + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">"))
+ ' ' + version);
}
System.out.println(
(i++) + ". " + version + " -> " + c.getCanonical() + "; tokens: " + c.items.toListString());
prev = c;
}
}
}