com.insidecoding.updatr.model.Version Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of updatr Show documentation
Show all versions of updatr Show documentation
Checks for the latest version of programs and automatically updates them
package com.insidecoding.updatr.model;
/**
* Abstracts the representation of a (software) version in format X.X.X.X...
*
* @author ludovicianul
*
*/
public final class Version implements Comparable {
private Integer[] items;
private Version(String[] itms) {
if (itms == null) {
throw new IllegalArgumentException("must provide non-null items array");
}
if (itms.length == 0) {
throw new IllegalArgumentException("At least one component required");
}
this.items = new Integer[itms.length];
for (int i = 0; i < itms.length; i++) {
this.items[i] = Integer.valueOf(itms[i]);
if (this.items[i] < 0) {
throw new IllegalArgumentException("must only use positive numbers; offending value "
+ itms[i]);
}
}
}
/**
* It creates a version object from a given String representation.
*
* @param versionSpec
* version in format X.X.X.
* @return a Version representation of the given String.
*/
public static Version fromString(String versionSpec) {
return new Version(parse(versionSpec));
}
@Override
public int compareTo(Version otheVersion) {
if (this == otheVersion) {
return 0;
}
int result = 0;
int length = Math.min(items.length, otheVersion.items.length);
int idx = 0;
while (result == 0 && idx < length) {
int current = items[idx].compareTo(otheVersion.items[idx]);
if (current != 0) {
result = current;
}
idx++;
}
if (result == 0) {
if (idx < items.length) {
result = items[idx];
} else if (idx < otheVersion.items.length) {
result = -otheVersion.items[idx];
}
}
return result;
}
private static String[] parse(String versionStr) {
if (versionStr == null) {
return null;
}
final String[] items = versionStr.replaceAll("[^\\d.]", "").split("\\.");
for (int i = 0; i < items.length; i++) {
items[i] = items[i].trim();
}
return items;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(items[0].toString());
if (items.length >= 1) {
for (int i = 1; i < items.length; i++) {
builder.append('.').append(items[i]);
}
}
return builder.toString();
}
}