/** * Checks if this version is greater than the other version. * * @param other the other version to compare to * @return {@code true} if this version is greater than the other version * or {@code false} otherwise * @see #compareTo(Version other) */ public boolean greaterThan(Version other) { return compareTo(other) > 0; }
private static Item parseItem(boolean isDigit, String buf) { return isDigit ? new IntegerItem(buf) : new StringItem(buf, false); }
/** * Creates a new instance of {@code Version} as a * result of parsing the specified version string. * * @param version the version string to parse * @return a new instance of the {@code Version} class */ public Version(String version) { parseVersion(version); }
/** * Check lib version satisfies lib version requirements * * @param versions e.g. <code>{"junit:junit": "4.12", "com.android.support:support-v4": "25.3.1"}</code> * @param requirements e.g. <code>{"junit:junit": "4.12", "com.android.support:support-v4": ">=25"}</code> * @return verify result * @see Result */ public static Result satisfies(Map<String, String> versions, Map<String, String> requirements) { for (Map.Entry<String, String> entry : requirements.entrySet()) { final String lib = entry.getKey(); final String requirement = entry.getValue(); final String version = versions.get(lib); if (version == null) { // lib not exist return Result.createFailResult(lib, String.format(Locale.ENGLISH, "required lib [%s] not exist", lib)); } if (!new Version(version).satisfies(requirement)) { // lib version requirement not satisfied return Result.createFailResult(lib, String.format(Locale.ENGLISH, "required lib [%s:%s] not satisfies [%s:%s]", lib, version, lib, requirement)); } } return Result.createSuccessResult(); }
public int compareTo(Item item) { if (item == null) { if (size() == 0) { return 0; // 1-0 = 1- (normalize) = 1 } Item first = get(0); return first.compareTo(null); } switch (item.getType()) { case INTEGER_ITEM: return -1; // 1-1 < 1.0.x case STRING_ITEM: return 1; // 1-1 > 1-sp case LIST_ITEM: Iterator<Item> left = iterator(); Iterator<Item> right = ((ListItem) item).iterator(); while (left.hasNext() || right.hasNext()) { Item l = left.hasNext() ? left.next() : null; Item r = right.hasNext() ? right.next() : null; // if this is shorter, then invert the compare and mul with -1 int result = l == null ? -1 * r.compareTo(l) : l.compareTo(r); if (result != 0) { return result; } } return 0; default: throw new RuntimeException("invalid item: " + item.getClass()); } }
void normalize() { for (ListIterator<Item> iterator = listIterator(size()); iterator.hasPrevious(); ) { Item item = iterator.previous(); if (item.isNull()) { iterator.remove(); // remove null trailing items: 0, "", empty list } else { break; } } }
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 INTEGER_ITEM: return -1; // 1.any < 1.1 ? case STRING_ITEM: return comparableQualifier(value).compareTo(comparableQualifier(((StringItem) item).value)); case LIST_ITEM: return -1; // 1.any < 1-1 default: throw new RuntimeException("invalid item: " + item.getClass()); } }
@Test public void testCreateVersion() throws Exception { // org.infinispan:infinispan-directory-provider:9.2.0.Alpha2 Version version = new Version("9.2.0.Alpha2"); // junit:junit:4.12 version = new Version("4.12"); // org.json:json:20171018 version = new Version("20171018"); // io.reactivex.rxjava2:rxjava:2.1.6 version = new Version("2.1.6"); // io.reactivex.rxjava2:rxjava:2.1.6-SNAPSHOT version = new Version("2.1.6-SNAPSHOT"); }
/** * Checks if the current version equals the parsed version. * * @param version the version to compare to, the left-hand * operand of the "equal" operator * @return {@code true} if the version equals the * parsed version or {@code false} otherwise */ @Override public boolean interpret(Version version) { return version.equals(mParsedVersion); } }
/** * Create success result; lib, message would be <code>null</code> * @return success result */ public static Result createSuccessResult() { return new Result(true, null, null); }
public int compareTo(Version o) { return items.compareTo(o.items); }
public boolean isNull() { return (size() == 0); }
public boolean isNull() { return (comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX) == 0); }
public String toString() { StringBuilder buffer = new StringBuilder("("); for (Iterator<Item> iter = iterator(); iter.hasNext(); ) { buffer.append(iter.next()); if (iter.hasNext()) { buffer.append(','); } } buffer.append(')'); return buffer.toString(); } }
public int compareTo(Item item) { if (item == null) { return BigInteger_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1 } switch (item.getType()) { case INTEGER_ITEM: return value.compareTo(((IntegerItem) item).value); case STRING_ITEM: return 1; // 1.1 > 1-sp case LIST_ITEM: return 1; // 1.1 > 1-1 default: throw new RuntimeException("invalid item: " + item.getClass()); } }
/** * Checks if this version is less than or equal to the other version. * * @param other the other version to compare to * @return {@code true} if this version is less than or equal * to the other version or {@code false} otherwise * @see #compareTo(Version other) */ public boolean lessThanOrEqualTo(Version other) { return compareTo(other) <= 0; }
/** * Create fail result * @param lib which lib requirement not satisfied * @param message detail fail message * @return fail result */ public static Result createFailResult(String lib, String message) { return new Result(false, lib, message); }
/** * Checks if this version is greater than or equal to the other version. * * @param other the other version to compare to * @return {@code true} if this version is greater than or equal * to the other version or {@code false} otherwise * @see #compareTo(Version other) */ public boolean greaterThanOrEqualTo(Version other) { return compareTo(other) >= 0; }
/** * Checks if this version is less than the other version. * * @param other the other version to compare to * @return {@code true} if this version is less than the other version * or {@code false} otherwise * @see #compareTo(Version other) */ public boolean lessThan(Version other) { return compareTo(other) < 0; }
/** * Checks if the current version is greater * than or equal to the parsed version. * * @param version the version to compare to, the left-hand operand * of the "greater than or equal to" operator * @return {@code true} if the version is greater than or equal * to the parsed version or {@code false} otherwise */ @Override public boolean interpret(Version version) { return version.compareTo(mParsedVersion) >= 0; } }