Duration implements Comparable<Duration> { @JsonCreator public static Duration valueOf(String duration) throws IllegalArgumentException { requireNonNull(duration, "duration is null"); Preconditions.checkArgument(!duration.isEmpty(), "duration is empty"); Matcher matcher = PATTERN.matcher(duration); if (!matcher.matches()) { throw new IllegalArgumentException("duration is not a valid data duration string: " + duration); } double value = Double.parseDouble(matcher.group(1)); String unitString = matcher.group(2); TimeUnit timeUnit = valueOfTimeUnit(unitString); return new Duration(value, timeUnit); } Duration(double value, TimeUnit unit); static Duration nanosSince(long start); static Duration succinctNanos(long nanos); static Duration succinctDuration(double value, TimeUnit unit); long toMillis(); double getValue(); TimeUnit getUnit(); double getValue(TimeUnit timeUnit); long roundTo(TimeUnit timeUnit); Duration convertToMostSuccinctTimeUnit(); @JsonValue @Override String toString(); String toString(TimeUnit timeUnit); @JsonCreator static Duration valueOf(String duration); @Override int compareTo(Duration o); @Override boolean equals(Object o); @Override int hashCode(); static TimeUnit valueOfTimeUnit(String timeUnitString); static String timeUnitToString(TimeUnit timeUnit);  }
@Test(dataProvider = "parseableValues") public void testValueOf(String string, double expectedValue, TimeUnit expectedUnit) { Duration duration = Duration.valueOf(string); assertEquals(duration.getUnit(), expectedUnit); assertEquals(duration.getValue(), expectedValue); }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "duration is null") public void testValueOfRejectsNull() { Duration.valueOf(null); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "duration is empty") public void testValueOfRejectsEmptyString() { Duration.valueOf(""); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unknown time unit: kg") public void testValueOfRejectsInvalidUnit() { Duration.valueOf("1.234 kg"); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "duration is not a valid.*") public void testValueOfRejectsInvalidNumber() { Duration.valueOf("1.2x4 s"); }