VersionNumberSanitizer { public static Optional<Integer> sanitize(String version) { if (version == null) { return Optional.absent(); } String stripped = version.replaceAll("\\D", ""); if (StringUtils.isEmpty(stripped)) { return Optional.absent(); } try { return Optional.of(Integer.valueOf(StringUtils.rightPad(stripped, MAX_DIGITS_VERSION_NO, '0'))); } catch (NumberFormatException nfe) { return Optional.absent(); } }  static Optional<Integer> sanitize(String version); static final int MAX_DIGITS_VERSION_NO; }
@Test public void should_convert_1_0_0_to_1000() { assertEquals(1000, VersionNumberSanitizer.sanitize("1.0.0").get().intValue()); }
@Test public void should_discard_any_non_numbers() { assertEquals(1000, VersionNumberSanitizer.sanitize("1.0.0XX").get().intValue()); }
@Test public void should_discard_SNAPSHOT() { assertEquals(1000, VersionNumberSanitizer.sanitize("1.0.0-SNAPSHOT").get().intValue()); }
@Test public void should_append_0_for_short_version_numbers() { assertEquals(1300, VersionNumberSanitizer.sanitize("1.3").get().intValue()); }
@Test public void should_convert_1_2_2_21_to_1221() { assertEquals(1221, VersionNumberSanitizer.sanitize("1.2.2.1").get().intValue()); }
@Test public void should_not_trip_on_invalid_numbers() { assertFalse(VersionNumberSanitizer.sanitize("xxx").isPresent()); }
@Test public void should_handle_nulls() { assertFalse(VersionNumberSanitizer.sanitize(null).isPresent()); }