JavaVersion { public static JavaVersion parse(String version) { Matcher matcher = LEGACY_PATTERN.matcher(version); if (matcher.matches()) { int major = Integer.parseInt(matcher.group("MAJOR")); int minor = Optional.ofNullable(matcher.group("MINOR")) .map(Integer::parseInt) .orElse(0); String update = matcher.group("UPDATE"); if (update == null) { return new JavaVersion(major, minor); } return new JavaVersion(major, minor, OptionalInt.of(Integer.parseInt(update))); } matcher = PATTERN.matcher(version); if (matcher.matches()) { int major = Integer.parseInt(matcher.group("MAJOR")); int minor = Optional.ofNullable(matcher.group("MINOR")) .map(Integer::parseInt) .orElse(0); return new JavaVersion(major, minor); } throw new IllegalArgumentException(format("Cannot parse version %s", version)); } JavaVersion(int major, int minor, OptionalInt update);  JavaVersion(int major, int minor); static JavaVersion current(); static JavaVersion parse(String version); int getMajor(); int getMinor(); OptionalInt getUpdate(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testParseLegacy() { assertEquals(JavaVersion.parse("1.8"), new JavaVersion(8, 0)); assertEquals(JavaVersion.parse("1.8.0"), new JavaVersion(8, 0)); assertEquals(JavaVersion.parse("1.8.0_5"), new JavaVersion(8, 0, OptionalInt.of(5))); assertEquals(JavaVersion.parse("1.8.0_20"), new JavaVersion(8, 0, OptionalInt.of(20))); assertEquals(JavaVersion.parse("1.8.1_25"), new JavaVersion(8, 1, OptionalInt.of(25))); assertEquals(JavaVersion.parse("1.8.0_60-ea"), new JavaVersion(8, 0, OptionalInt.of(60))); assertEquals(JavaVersion.parse("1.8.0_131-foo-bar-baz-qux"), new JavaVersion(8, 0, OptionalInt.of(131))); }
@Test public void testParseNew() { assertEquals(JavaVersion.parse("9-ea+19"), new JavaVersion(9, 0)); assertEquals(JavaVersion.parse("9+100"), new JavaVersion(9, 0)); assertEquals(JavaVersion.parse("9.0.1+20"), new JavaVersion(9, 0)); assertEquals(JavaVersion.parse("9.1.1+20"), new JavaVersion(9, 1)); }