@Test public void shouldThrowWhenParseTimeHasBadTwoLetterSuffix() { assertThrows(NumberFormatException.class, () -> parseDuration("", "1zs")); }
public static long parseDuration(final String propertyName, final String propertyValue) { final char lastCharacter = propertyValue.charAt(propertyValue.length() - 1); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } if (lastCharacter != 's' && lastCharacter != 'S') { throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } final char secondLastCharacter = propertyValue.charAt(propertyValue.length() - 2); if (Character.isDigit(secondLastCharacter)) { final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 1); return TimeUnit.SECONDS.toNanos(value); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 2); switch (secondLastCharacter) { case 'n': case 'N': return value; case 'u': case 'U': return TimeUnit.MICROSECONDS.toNanos(value); case 'm': case 'M': return TimeUnit.MILLISECONDS.toNanos(value); default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } }
SystemUtil { public static long parseDuration(final String propertyName, final String propertyValue) { final char lastCharacter = propertyValue.charAt(propertyValue.length() - 1); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } if (lastCharacter != 's' && lastCharacter != 'S') { throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } final char secondLastCharacter = propertyValue.charAt(propertyValue.length() - 2); if (Character.isDigit(secondLastCharacter)) { final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 1); return TimeUnit.SECONDS.toNanos(value); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 2); switch (secondLastCharacter) { case 'n': case 'N': return value; case 'u': case 'U': return TimeUnit.MICROSECONDS.toNanos(value); case 'm': case 'M': return TimeUnit.MILLISECONDS.toNanos(value); default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } } }
SystemUtil { public static long parseDuration(final String propertyName, final String propertyValue) { final char lastCharacter = propertyValue.charAt(propertyValue.length() - 1); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } if (lastCharacter != 's' && lastCharacter != 'S') { throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } final char secondLastCharacter = propertyValue.charAt(propertyValue.length() - 2); if (Character.isDigit(secondLastCharacter)) { final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 1); return TimeUnit.SECONDS.toNanos(value); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 2); switch (secondLastCharacter) { case 'n': case 'N': return value; case 'u': case 'U': return TimeUnit.MICROSECONDS.toNanos(value); case 'm': case 'M': return TimeUnit.MILLISECONDS.toNanos(value); default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } } private  SystemUtil(); }
SystemUtil { public static long parseDuration(final String propertyName, final String propertyValue) { final char lastCharacter = propertyValue.charAt(propertyValue.length() - 1); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } if (lastCharacter != 's' && lastCharacter != 'S') { throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } final char secondLastCharacter = propertyValue.charAt(propertyValue.length() - 2); if (Character.isDigit(secondLastCharacter)) { final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 1); return TimeUnit.SECONDS.toNanos(value); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 2); switch (secondLastCharacter) { case 'n': case 'N': return value; case 'u': case 'U': return TimeUnit.MICROSECONDS.toNanos(value); case 'm': case 'M': return TimeUnit.MILLISECONDS.toNanos(value); default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } } private  SystemUtil(); static String osName(); static long getPid(); static boolean isWindows(); static boolean isLinux(); static boolean isDebuggerAttached(); static String tmpDirName(); static String threadDump(); static void loadPropertiesFile(final String filenameOrUrl); static void loadPropertiesFiles(final String[] filenamesOrUrls); static int getSizeAsInt(final String propertyName, final int defaultValue); static long getSizeAsLong(final String propertyName, final long defaultValue); static long parseSize(final String propertyName, final String propertyValue); static long getDurationInNanos(final String propertyName, final long defaultValue); static long parseDuration(final String propertyName, final String propertyValue); }
SystemUtil { public static long parseDuration(final String propertyName, final String propertyValue) { final char lastCharacter = propertyValue.charAt(propertyValue.length() - 1); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } if (lastCharacter != 's' && lastCharacter != 'S') { throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } final char secondLastCharacter = propertyValue.charAt(propertyValue.length() - 2); if (Character.isDigit(secondLastCharacter)) { final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 1); return TimeUnit.SECONDS.toNanos(value); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, propertyValue.length() - 2); switch (secondLastCharacter) { case 'n': case 'N': return value; case 'u': case 'U': return TimeUnit.MICROSECONDS.toNanos(value); case 'm': case 'M': return TimeUnit.MILLISECONDS.toNanos(value); default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: s, ms, us, or ns."); } } private  SystemUtil(); static String osName(); static long getPid(); static boolean isWindows(); static boolean isLinux(); static boolean isDebuggerAttached(); static String tmpDirName(); static String threadDump(); static void loadPropertiesFile(final String filenameOrUrl); static void loadPropertiesFiles(final String[] filenamesOrUrls); static int getSizeAsInt(final String propertyName, final int defaultValue); static long getSizeAsLong(final String propertyName, final long defaultValue); static long parseSize(final String propertyName, final String propertyValue); static long getDurationInNanos(final String propertyName, final long defaultValue); static long parseDuration(final String propertyName, final String propertyValue); static final long PID_NOT_FOUND; }