/** * Return a new {@link Version} initialized to the value * represented by the specified {@link String} * * @param version the string to be parsed * * @return the version represented by the string value * * @throws NumberFormatException if the string does * not contain a valid version */ public static Version parse(String version) { int indexFirstDot = version.indexOf('.'); int indexSecondDot = version.indexOf('.', indexFirstDot + 1); if (indexFirstDot == -1 || indexSecondDot == -1) { throw new NumberFormatException("Invalid version format '" + version + "', expected two dots!"); } int indexFirstDash = version.indexOf('-'); int versionEnd; if (indexFirstDash == -1) { versionEnd = version.length(); } else { versionEnd = indexFirstDash; } boolean snapshot = version.endsWith(SNAPSHOT_MARKER); return new Version(Integer.parseInt(version.substring(0, indexFirstDot)), Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)), Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot); }
@Test public void testParse() { Version referenceVersion = Version.currentVersion(); Assert.assertEquals(referenceVersion, Version.parse(referenceVersion.toString())); Assert.assertEquals(new Version(1,5,2, false), Version.parse("1.5.2-incubating")); Assert.assertEquals(new Version(1,5,2, false), Version.parse("1.5.2")); }
/** * Return a new {@link Version} initialized to the value * represented by the specified {@link String} * * @param version the string to be parsed * * @return the version represented by the string value * * @throws NumberFormatException if the string does * not contain a valid version */ public static Version parse(String version) { int indexFirstDot = version.indexOf('.'); int indexSecondDot = version.indexOf('.', indexFirstDot + 1); if (indexFirstDot == -1 || indexSecondDot == -1) { throw new NumberFormatException("Invalid version format '" + version + "', expected two dots!"); } int indexFirstDash = version.indexOf('-'); int versionEnd; if (indexFirstDash == -1) { versionEnd = version.length(); } else { versionEnd = indexFirstDash; } boolean snapshot = version.endsWith(SNAPSHOT_MARKER); return new Version(Integer.parseInt(version.substring(0, indexFirstDot)), Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)), Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot); }
/** * Return a new {@link Version} initialized to the value * represented by the specified {@link String} * * @param version the string to be parsed * * @return the version represented by the string value * * @throws NumberFormatException if the string does * not contain a valid version */ public static Version parse(String version) { int indexFirstDot = version.indexOf('.'); int indexSecondDot = version.indexOf('.', indexFirstDot + 1); if (indexFirstDot == -1 || indexSecondDot == -1) { throw new NumberFormatException("Invalid version format '" + version + "', expected two dots!"); } int indexFirstDash = version.indexOf('-'); int versionEnd; if (indexFirstDash == -1) { versionEnd = version.length(); } else { versionEnd = indexFirstDash; } boolean snapshot = version.endsWith(SNAPSHOT_MARKER); return new Version(Integer.parseInt(version.substring(0, indexFirstDot)), Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)), Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot); }