@Test (expected=NumberFormatException.class) public void negativeUnitOnlyDataRate() { Utils.parseDataRate("kB/s"); }
public static long parseDataRate(String dataRateValue) { int length = dataRateValue.length(); if (length < 1) { throw new NumberFormatException("Illegal dataRate value, empty string not allowed"); } char last = dataRateValue.charAt(length - 1); long multiplier = 1; String numberPart = dataRateValue; if (!Character.isDigit(last)) { for (int i = 0; i < PERMITTED_DATA_RATE_UNITS.length; i++) { if (dataRateValue.endsWith(PERMITTED_DATA_RATE_UNITS[i])) { multiplier = DATA_RATE_MULTIPLIERS[i]; int unitLength = PERMITTED_DATA_RATE_UNITS[i].length(); if (length < (unitLength + 1)) { throw new NumberFormatException("Invalid dataRate value \"" + dataRateValue + "\", number part missing"); } numberPart = dataRateValue.substring(0, length - unitLength); break; } } } final long result = Long.parseLong(numberPart) * multiplier; if (result <= 0) { throw new NumberFormatException("Illegal dataRate value \"" + dataRateValue + "\", must be a positive number or overflow occurred"); } return result; }
Utils { public static long parseDataRate(String dataRateValue) { int length = dataRateValue.length(); if (length < 1) { throw new NumberFormatException("Illegal dataRate value, empty string not allowed"); } char last = dataRateValue.charAt(length - 1); long multiplier = 1; String numberPart = dataRateValue; if (!Character.isDigit(last)) { for (int i = 0; i < PERMITTED_DATA_RATE_UNITS.length; i++) { if (dataRateValue.endsWith(PERMITTED_DATA_RATE_UNITS[i])) { multiplier = DATA_RATE_MULTIPLIERS[i]; int unitLength = PERMITTED_DATA_RATE_UNITS[i].length(); if (length < (unitLength + 1)) { throw new NumberFormatException("Invalid dataRate value \"" + dataRateValue + "\", number part missing"); } numberPart = dataRateValue.substring(0, length - unitLength); break; } } } final long result = Long.parseLong(numberPart) * multiplier; if (result <= 0) { throw new NumberFormatException("Illegal dataRate value \"" + dataRateValue + "\", must be a positive number or overflow occurred"); } return result; } }
Utils { public static long parseDataRate(String dataRateValue) { int length = dataRateValue.length(); if (length < 1) { throw new NumberFormatException("Illegal dataRate value, empty string not allowed"); } char last = dataRateValue.charAt(length - 1); long multiplier = 1; String numberPart = dataRateValue; if (!Character.isDigit(last)) { for (int i = 0; i < PERMITTED_DATA_RATE_UNITS.length; i++) { if (dataRateValue.endsWith(PERMITTED_DATA_RATE_UNITS[i])) { multiplier = DATA_RATE_MULTIPLIERS[i]; int unitLength = PERMITTED_DATA_RATE_UNITS[i].length(); if (length < (unitLength + 1)) { throw new NumberFormatException("Invalid dataRate value \"" + dataRateValue + "\", number part missing"); } numberPart = dataRateValue.substring(0, length - unitLength); break; } } } final long result = Long.parseLong(numberPart) * multiplier; if (result <= 0) { throw new NumberFormatException("Illegal dataRate value \"" + dataRateValue + "\", must be a positive number or overflow occurred"); } return result; } private  Utils(); }
Utils { public static long parseDataRate(String dataRateValue) { int length = dataRateValue.length(); if (length < 1) { throw new NumberFormatException("Illegal dataRate value, empty string not allowed"); } char last = dataRateValue.charAt(length - 1); long multiplier = 1; String numberPart = dataRateValue; if (!Character.isDigit(last)) { for (int i = 0; i < PERMITTED_DATA_RATE_UNITS.length; i++) { if (dataRateValue.endsWith(PERMITTED_DATA_RATE_UNITS[i])) { multiplier = DATA_RATE_MULTIPLIERS[i]; int unitLength = PERMITTED_DATA_RATE_UNITS[i].length(); if (length < (unitLength + 1)) { throw new NumberFormatException("Invalid dataRate value \"" + dataRateValue + "\", number part missing"); } numberPart = dataRateValue.substring(0, length - unitLength); break; } } } final long result = Long.parseLong(numberPart) * multiplier; if (result <= 0) { throw new NumberFormatException("Illegal dataRate value \"" + dataRateValue + "\", must be a positive number or overflow occurred"); } return result; } private  Utils(); static String join(String[] array, String separator); static String fill(char c, int size); static String toHex(byte[] data); static byte[] fromHex(String hex); static void encodeBoolean(boolean b, ByteBuffer buf); static void encodeInt(int i, ByteBuffer buf); static void encodeLong(long i, ByteBuffer buf); static String asString(Map<ByteBuffer, ByteBuffer> headers); static String asString(ByteBuffer buf); static byte[] asByteArray(ByteBuffer buf); @Deprecated // Use asString now it's been fixed to use UTF-8 CharSet static String asStringUTF8(ByteBuffer buf); static ByteBuffer asByteBuffer(String s); static boolean isASCII(String s); static ByteBuffer asByteBuffer(byte[] bytes); static int getInt(ByteBuffer buf, int index); static long getLong(ByteBuffer buf, int index); static short getShort(ByteBuffer buf, int index); static String initCaps(String in); static void putByteBuffer(ByteBuffer source, ByteBuffer target); static Class<?> loadClass(String className); static int loadStaticInt(String fullyQualifiedFieldName); static boolean parseBoolean(String valueName, String value, boolean defaultValue); static long parsePositiveInteger(String valueName, String value, long defaultValue); static long parseTimeInterval(String timeIntervalValue, TimeUnit outputUnit); static long parseTimeInterval(String timeIntervalValue, TimeUnit outputUnit, long defaultValue); static long parseTimeInterval(String timeIntervalValue, TimeUnit outputUnit, String defaultValue); static int parseDataSize(String dataSizeValue); static long parseDataRate(String dataRateValue); static boolean sameOrEquals(Object this_, Object that); static boolean sameOrEquals(Map<K, V> this_, Map<K, V> that); static boolean sameOrEquals(Collection<T> this_, Collection<T> that); static String randomHexString(int len); static byte[] randomHexBytes(int len); static int randomInt(); @Deprecated static void log(Logger logger, LogLevel eventLevel, String message, Object param); static void inject(Object target,
                                  Class<T> injectableType,
                                  T injectableInstance); static void injectAll(Object target, Map<Class<?>, Object> injectables); static String join(Object[] data, String separator); static String asCommaSeparatedString(final Collection<String> strings); static String asSeparatedString(final Collection<String> strings, final String separator); static Map<String, Object> rewriteKeys(final Map<String, Object> options, String prefix, String newPrefix); static String getHostStringWithoutNameLookup(InetSocketAddress inetSocketAddress); static String stackTrace(Throwable cause); static String[] removeStringArrayElement(String[] option, String remove); }
Utils { public static long parseDataRate(String dataRateValue) { int length = dataRateValue.length(); if (length < 1) { throw new NumberFormatException("Illegal dataRate value, empty string not allowed"); } char last = dataRateValue.charAt(length - 1); long multiplier = 1; String numberPart = dataRateValue; if (!Character.isDigit(last)) { for (int i = 0; i < PERMITTED_DATA_RATE_UNITS.length; i++) { if (dataRateValue.endsWith(PERMITTED_DATA_RATE_UNITS[i])) { multiplier = DATA_RATE_MULTIPLIERS[i]; int unitLength = PERMITTED_DATA_RATE_UNITS[i].length(); if (length < (unitLength + 1)) { throw new NumberFormatException("Invalid dataRate value \"" + dataRateValue + "\", number part missing"); } numberPart = dataRateValue.substring(0, length - unitLength); break; } } } final long result = Long.parseLong(numberPart) * multiplier; if (result <= 0) { throw new NumberFormatException("Illegal dataRate value \"" + dataRateValue + "\", must be a positive number or overflow occurred"); } return result; } private  Utils(); static String join(String[] array, String separator); static String fill(char c, int size); static String toHex(byte[] data); static byte[] fromHex(String hex); static void encodeBoolean(boolean b, ByteBuffer buf); static void encodeInt(int i, ByteBuffer buf); static void encodeLong(long i, ByteBuffer buf); static String asString(Map<ByteBuffer, ByteBuffer> headers); static String asString(ByteBuffer buf); static byte[] asByteArray(ByteBuffer buf); @Deprecated // Use asString now it's been fixed to use UTF-8 CharSet static String asStringUTF8(ByteBuffer buf); static ByteBuffer asByteBuffer(String s); static boolean isASCII(String s); static ByteBuffer asByteBuffer(byte[] bytes); static int getInt(ByteBuffer buf, int index); static long getLong(ByteBuffer buf, int index); static short getShort(ByteBuffer buf, int index); static String initCaps(String in); static void putByteBuffer(ByteBuffer source, ByteBuffer target); static Class<?> loadClass(String className); static int loadStaticInt(String fullyQualifiedFieldName); static boolean parseBoolean(String valueName, String value, boolean defaultValue); static long parsePositiveInteger(String valueName, String value, long defaultValue); static long parseTimeInterval(String timeIntervalValue, TimeUnit outputUnit); static long parseTimeInterval(String timeIntervalValue, TimeUnit outputUnit, long defaultValue); static long parseTimeInterval(String timeIntervalValue, TimeUnit outputUnit, String defaultValue); static int parseDataSize(String dataSizeValue); static long parseDataRate(String dataRateValue); static boolean sameOrEquals(Object this_, Object that); static boolean sameOrEquals(Map<K, V> this_, Map<K, V> that); static boolean sameOrEquals(Collection<T> this_, Collection<T> that); static String randomHexString(int len); static byte[] randomHexBytes(int len); static int randomInt(); @Deprecated static void log(Logger logger, LogLevel eventLevel, String message, Object param); static void inject(Object target,
                                  Class<T> injectableType,
                                  T injectableInstance); static void injectAll(Object target, Map<Class<?>, Object> injectables); static String join(Object[] data, String separator); static String asCommaSeparatedString(final Collection<String> strings); static String asSeparatedString(final Collection<String> strings, final String separator); static Map<String, Object> rewriteKeys(final Map<String, Object> options, String prefix, String newPrefix); static String getHostStringWithoutNameLookup(InetSocketAddress inetSocketAddress); static String stackTrace(Throwable cause); static String[] removeStringArrayElement(String[] option, String remove); static final Charset UTF_8; static final Charset US_ASCII; static final Set<String> SECONDS_UNITS; static final Set<String> MILLISECONDS_UNITS; static final Set<String> MINUTES_UNITS; static final Set<String> HOURS_UNITS; static int ENCODED_BOOLEAN_CAPACITY; static int ENCODED_INT_CAPACITY; static int ENCODED_LONG_CAPACITY; }