@Test public void testParseDurationInvalid() { try { TimeUtils.parseDuration(null); fail("exception expected"); } catch (NullPointerException ignored) { } try { TimeUtils.parseDuration(""); fail("exception expected"); } catch (IllegalArgumentException ignored) { } try { TimeUtils.parseDuration(" "); fail("exception expected"); } catch (IllegalArgumentException ignored) { } try { TimeUtils.parseDuration("foobar or fubar or foo bazz"); fail("exception expected"); } catch (IllegalArgumentException ignored) { } try { TimeUtils.parseDuration("16 gjah"); fail("exception expected"); } catch (IllegalArgumentException ignored) { } try { TimeUtils.parseDuration("16 16 17 18 ms"); fail("exception expected"); } catch (IllegalArgumentException ignored) { } try { TimeUtils.parseDuration("-100 ms"); fail("exception expected"); } catch (IllegalArgumentException ignored) { } }
public static Duration parseDuration(String text) { checkNotNull(text, "text"); final String trimmed = text.trim(); checkArgument(!trimmed.isEmpty(), "argument is an empty- or whitespace-only string"); final int len = trimmed.length(); int pos = 0; char current; while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') { pos++; } final String number = trimmed.substring(0, pos); final String unit = trimmed.substring(pos).trim().toLowerCase(Locale.US); if (number.isEmpty()) { throw new NumberFormatException("text does not start with a number"); } final long value; try { value = Long.parseLong(number); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value '" + number + "' cannot be re represented as 64bit number (numeric overflow)."); } final long multiplier; if (unit.isEmpty()) { multiplier = 1L; } else { if (matchTimeUnit(unit, TimeUnit.MILLISECONDS)) { multiplier = 1L; } else if (matchTimeUnit(unit, TimeUnit.SECONDS)) { multiplier = 1000L; } else if (matchTimeUnit(unit, TimeUnit.MINUTES)) { multiplier = 1000L * 60L; } else if (matchTimeUnit(unit, TimeUnit.HOURS)) { multiplier = 1000L * 60L * 60L; } else { throw new IllegalArgumentException("Time interval unit '" + unit + "' does not match any of the recognized units: " + TimeUnit.getAllUnits()); } } final long result = value * multiplier; if (result / multiplier != value) { throw new IllegalArgumentException("The value '" + text + "' cannot be re represented as 64bit number of bytes (numeric overflow)."); } return Duration.ofMillis(result); }
TimeUtils { public static Duration parseDuration(String text) { checkNotNull(text, "text"); final String trimmed = text.trim(); checkArgument(!trimmed.isEmpty(), "argument is an empty- or whitespace-only string"); final int len = trimmed.length(); int pos = 0; char current; while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') { pos++; } final String number = trimmed.substring(0, pos); final String unit = trimmed.substring(pos).trim().toLowerCase(Locale.US); if (number.isEmpty()) { throw new NumberFormatException("text does not start with a number"); } final long value; try { value = Long.parseLong(number); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value '" + number + "' cannot be re represented as 64bit number (numeric overflow)."); } final long multiplier; if (unit.isEmpty()) { multiplier = 1L; } else { if (matchTimeUnit(unit, TimeUnit.MILLISECONDS)) { multiplier = 1L; } else if (matchTimeUnit(unit, TimeUnit.SECONDS)) { multiplier = 1000L; } else if (matchTimeUnit(unit, TimeUnit.MINUTES)) { multiplier = 1000L * 60L; } else if (matchTimeUnit(unit, TimeUnit.HOURS)) { multiplier = 1000L * 60L * 60L; } else { throw new IllegalArgumentException("Time interval unit '" + unit + "' does not match any of the recognized units: " + TimeUnit.getAllUnits()); } } final long result = value * multiplier; if (result / multiplier != value) { throw new IllegalArgumentException("The value '" + text + "' cannot be re represented as 64bit number of bytes (numeric overflow)."); } return Duration.ofMillis(result); } }
TimeUtils { public static Duration parseDuration(String text) { checkNotNull(text, "text"); final String trimmed = text.trim(); checkArgument(!trimmed.isEmpty(), "argument is an empty- or whitespace-only string"); final int len = trimmed.length(); int pos = 0; char current; while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') { pos++; } final String number = trimmed.substring(0, pos); final String unit = trimmed.substring(pos).trim().toLowerCase(Locale.US); if (number.isEmpty()) { throw new NumberFormatException("text does not start with a number"); } final long value; try { value = Long.parseLong(number); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value '" + number + "' cannot be re represented as 64bit number (numeric overflow)."); } final long multiplier; if (unit.isEmpty()) { multiplier = 1L; } else { if (matchTimeUnit(unit, TimeUnit.MILLISECONDS)) { multiplier = 1L; } else if (matchTimeUnit(unit, TimeUnit.SECONDS)) { multiplier = 1000L; } else if (matchTimeUnit(unit, TimeUnit.MINUTES)) { multiplier = 1000L * 60L; } else if (matchTimeUnit(unit, TimeUnit.HOURS)) { multiplier = 1000L * 60L * 60L; } else { throw new IllegalArgumentException("Time interval unit '" + unit + "' does not match any of the recognized units: " + TimeUnit.getAllUnits()); } } final long result = value * multiplier; if (result / multiplier != value) { throw new IllegalArgumentException("The value '" + text + "' cannot be re represented as 64bit number of bytes (numeric overflow)."); } return Duration.ofMillis(result); }  }
TimeUtils { public static Duration parseDuration(String text) { checkNotNull(text, "text"); final String trimmed = text.trim(); checkArgument(!trimmed.isEmpty(), "argument is an empty- or whitespace-only string"); final int len = trimmed.length(); int pos = 0; char current; while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') { pos++; } final String number = trimmed.substring(0, pos); final String unit = trimmed.substring(pos).trim().toLowerCase(Locale.US); if (number.isEmpty()) { throw new NumberFormatException("text does not start with a number"); } final long value; try { value = Long.parseLong(number); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value '" + number + "' cannot be re represented as 64bit number (numeric overflow)."); } final long multiplier; if (unit.isEmpty()) { multiplier = 1L; } else { if (matchTimeUnit(unit, TimeUnit.MILLISECONDS)) { multiplier = 1L; } else if (matchTimeUnit(unit, TimeUnit.SECONDS)) { multiplier = 1000L; } else if (matchTimeUnit(unit, TimeUnit.MINUTES)) { multiplier = 1000L * 60L; } else if (matchTimeUnit(unit, TimeUnit.HOURS)) { multiplier = 1000L * 60L * 60L; } else { throw new IllegalArgumentException("Time interval unit '" + unit + "' does not match any of the recognized units: " + TimeUnit.getAllUnits()); } } final long result = value * multiplier; if (result / multiplier != value) { throw new IllegalArgumentException("The value '" + text + "' cannot be re represented as 64bit number of bytes (numeric overflow)."); } return Duration.ofMillis(result); }  static Duration parseDuration(String text); }
TimeUtils { public static Duration parseDuration(String text) { checkNotNull(text, "text"); final String trimmed = text.trim(); checkArgument(!trimmed.isEmpty(), "argument is an empty- or whitespace-only string"); final int len = trimmed.length(); int pos = 0; char current; while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') { pos++; } final String number = trimmed.substring(0, pos); final String unit = trimmed.substring(pos).trim().toLowerCase(Locale.US); if (number.isEmpty()) { throw new NumberFormatException("text does not start with a number"); } final long value; try { value = Long.parseLong(number); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value '" + number + "' cannot be re represented as 64bit number (numeric overflow)."); } final long multiplier; if (unit.isEmpty()) { multiplier = 1L; } else { if (matchTimeUnit(unit, TimeUnit.MILLISECONDS)) { multiplier = 1L; } else if (matchTimeUnit(unit, TimeUnit.SECONDS)) { multiplier = 1000L; } else if (matchTimeUnit(unit, TimeUnit.MINUTES)) { multiplier = 1000L * 60L; } else if (matchTimeUnit(unit, TimeUnit.HOURS)) { multiplier = 1000L * 60L * 60L; } else { throw new IllegalArgumentException("Time interval unit '" + unit + "' does not match any of the recognized units: " + TimeUnit.getAllUnits()); } } final long result = value * multiplier; if (result / multiplier != value) { throw new IllegalArgumentException("The value '" + text + "' cannot be re represented as 64bit number of bytes (numeric overflow)."); } return Duration.ofMillis(result); }  static Duration parseDuration(String text);  }