![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.Version Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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.juneau;
import static org.apache.juneau.common.internal.StringUtils.*;
import static org.apache.juneau.internal.CollectionUtils.*;
import java.util.*;
/**
* Represents a version string such as "1.2" or "1.2.3"
*
*
* Used to compare version numbers.
*
*
See Also:
*
*/
public class Version implements Comparable {
private int[] parts;
/**
* Static creator.
*
* @param value
* A string of the form "#.#..." where there can be any number of parts.
*
Valid values:
*
* "1.2"
* "1.2.3"
* "0.1"
* ".1"
*
*
Can be null .
* @return A new header bean, or null if the value is null .
*/
public static Version of(String value) {
if (value == null)
return null;
return new Version(value);
}
/**
* Constructor
*
* @param value
* A string of the form "#.#..." where there can be any number of parts.
*
Valid values:
*
* "1.2"
* "1.2.3"
* "0.1"
* ".1"
*
* Any parts that are not numeric are interpreted as {@link Integer#MAX_VALUE}
*/
public Version(String value) {
if (isEmpty(value))
value = "0";
String[] sParts = split(value, '.');
parts = new int[sParts.length];
for (int i = 0; i < sParts.length; i++) {
try {
parts[i] = sParts[i].isEmpty() ? 0 : Integer.parseInt(sParts[i]);
} catch (NumberFormatException e) {
parts[i] = Integer.MAX_VALUE;
}
}
}
/**
* Returns the version part at the specified zero-indexed value.
*
* @param index The index of the version part.
* @return The version part, never null .
*/
public Optional getPart(int index) {
if (index < 0 || parts.length <= index)
return empty();
return optional(parts[index]);
}
/**
* Returns the major version part (i.e. part at index 0).
*
* @return The version part, never null .
*/
public Optional getMajor() {
return getPart(0);
}
/**
* Returns the minor version part (i.e. part at index 1).
*
* @return The version part, never null .
*/
public Optional getMinor() {
return getPart(1);
}
/**
* Returns the maintenance version part (i.e. part at index 2).
*
* @return The version part, never null .
*/
public Optional getMaintenance() {
return getPart(2);
}
/**
* Returns true if the specified version is at least this version.
*
* Example:
*
* assertTrue (Version.of ("1.2" ).isAtLeast(Version.of ("1" )));
* assertFalse (Version.of ("1.2" ).isAtLeast(Version.of ("2" )));
* assertTrue (Version.of ("1.2" ).isAtLeast(Version.of ("1.2.3" )));
* assertFalse (Version.of ("1.2.0" ).isAtLeast(Version.of ("1.2.3" )));
*
*
* @param v The version to compare to.
* @return true if the specified version is at least this version.
*/
public boolean isAtLeast(Version v) {
return isAtLeast(v, false);
}
/**
* Returns true if the specified version is at least this version.
*
* Example:
*
* assertTrue (Version.of ("1.2.3" ).isAtLeast(Version.of ("1.2.3" , false )));
* assertFalse (Version.of ("1.2.3" ).isAtLeast(Version.of ("1.2.3" , true )));
*
*
* @param v The version to compare to.
* @param exclusive Match down-to-version but not including.
* @return true if the specified version is at least this version.
*/
public boolean isAtLeast(Version v, boolean exclusive) {
for (int i = 0; i < Math.min(parts.length, v.parts.length); i++) {
int c = v.parts[i] - parts[i];
if (c > 0)
return false;
else if (c < 0)
return true;
}
for (int i = parts.length; i < v.parts.length; i++)
if (v.parts[i] != 0)
return false;
return ! exclusive;
}
/**
* Returns true if the specified version is at most this version.
*
* Example:
*
* assertFalse (Version.of ("1.2.3" ).isAtMost(Version.of ("1" )));
* assertTrue (Version.of ("1.2.3" ).isAtMost(Version.of ("2" )));
* assertTrue (Version.of ("1.2.3" ).isAtMost(Version.of ("1.2" )));
* assertFalse (Version.of ("1.2.3" ).isAtMost(Version.of ("1.2.0" )));
*
*
* @param v The version to compare to.
* @return true if the specified version is at most this version.
*/
public boolean isAtMost(Version v) {
return isAtMost(v, false);
}
/**
* Returns true if the specified version is at most this version.
*
* Example:
*
* assertTrue (Version.of ("1.2.3" ).isAtMost(Version.of ("1.2.3" , false )));
* assertFalse (Version.of ("1.2.3" ).isAtMost(Version.of ("1.2.3" , true )));
*
*
* @param v The version to compare to.
* @param exclusive Match up-to-version but not including.
* @return true if the specified version is at most this version.
*/
public boolean isAtMost(Version v, boolean exclusive) {
for (int i = 0; i < Math.min(parts.length, v.parts.length); i++) {
int c = parts[i] - v.parts[i];
if (c > 0)
return false;
else if (c < 0)
return true;
}
for (int i = parts.length; i < v.parts.length; i++)
if (v.parts[i] > 0)
return true;
return ! exclusive;
}
/**
* Returns true if the specified version is equal to this version.
*
* Example:
*
* assertTrue (Version.of ("1.2.3" ).isEqualsTo(Version.of ("1.2.3" )));
* assertTrue (Version.of ("1.2.3" ).isEqualsTo(Version.of ("1.2" )));
*
*
* @param v The version to compare to.
* @return true if the specified version is equal to this version.
*/
public boolean equals(Version v) {
for (int i = 0; i < Math.min(parts.length, v.parts.length); i++)
if (v.parts[i] - parts[i] != 0)
return false;
return true;
}
@Override /* Object */
public boolean equals(Object o) {
return o instanceof Version && equals((Version)o);
}
@Override /* Object */
public String toString() {
return join(parts, '.');
}
@Override
public int compareTo(Version v) {
for (int i = 0; i < Math.min(parts.length, v.parts.length); i++) {
int c = parts[i] - v.parts[i];
if (c != 0)
return c;
}
return parts.length - v.parts.length;
}
}