@Test public void parseTest() { assertEquals(0L, BytesParser.parse("0")); assertEquals(17L, BytesParser.parse("17")); assertEquals(4L << 10, BytesParser.parse("4k")); assertEquals(4L << 10, BytesParser.parse("4K")); assertEquals(16L << 20, BytesParser.parse("16m")); assertEquals(16L << 20, BytesParser.parse("16M")); assertEquals(5L << 30, BytesParser.parse("5g")); assertEquals(5L << 30, BytesParser.parse("5G")); assertEquals(764L << 40, BytesParser.parse("764t")); assertEquals(764L << 40, BytesParser.parse("764T")); assertEquals(2048L << 50, BytesParser.parse("2048p")); assertEquals(2048L << 50, BytesParser.parse("2048P")); assertEquals(3L << 60, BytesParser.parse("3e")); assertEquals(3L << 60, BytesParser.parse("3E")); }
public static long parse(String s) { final char lastChar; try { lastChar = s.charAt(s.length() - 1); } catch (IndexOutOfBoundsException ex) { throw new IllegalArgumentException("\"\" is not a valid size"); } final char lastCharLower = Character.toLowerCase(lastChar); final char lastCharUpper = Character.toUpperCase(lastChar); final Long mul = SIZE_MAP.getOrDefault(lastCharUpper, SIZE_MAP.get(lastCharLower)); if (mul != null) s = s.substring(0, s.length() - 1); final long v; try { v = Long.parseLong(s); } catch (NumberFormatException ex) { throw new IllegalArgumentException("\"" + s + "\" is not a number"); } return v * (mul == null ? 1L : mul.longValue()); }
BytesParser { public static long parse(String s) { final char lastChar; try { lastChar = s.charAt(s.length() - 1); } catch (IndexOutOfBoundsException ex) { throw new IllegalArgumentException("\"\" is not a valid size"); } final char lastCharLower = Character.toLowerCase(lastChar); final char lastCharUpper = Character.toUpperCase(lastChar); final Long mul = SIZE_MAP.getOrDefault(lastCharUpper, SIZE_MAP.get(lastCharLower)); if (mul != null) s = s.substring(0, s.length() - 1); final long v; try { v = Long.parseLong(s); } catch (NumberFormatException ex) { throw new IllegalArgumentException("\"" + s + "\" is not a number"); } return v * (mul == null ? 1L : mul.longValue()); } }
BytesParser { public static long parse(String s) { final char lastChar; try { lastChar = s.charAt(s.length() - 1); } catch (IndexOutOfBoundsException ex) { throw new IllegalArgumentException("\"\" is not a valid size"); } final char lastCharLower = Character.toLowerCase(lastChar); final char lastCharUpper = Character.toUpperCase(lastChar); final Long mul = SIZE_MAP.getOrDefault(lastCharUpper, SIZE_MAP.get(lastCharLower)); if (mul != null) s = s.substring(0, s.length() - 1); final long v; try { v = Long.parseLong(s); } catch (NumberFormatException ex) { throw new IllegalArgumentException("\"" + s + "\" is not a number"); } return v * (mul == null ? 1L : mul.longValue()); }  }
BytesParser { public static long parse(String s) { final char lastChar; try { lastChar = s.charAt(s.length() - 1); } catch (IndexOutOfBoundsException ex) { throw new IllegalArgumentException("\"\" is not a valid size"); } final char lastCharLower = Character.toLowerCase(lastChar); final char lastCharUpper = Character.toUpperCase(lastChar); final Long mul = SIZE_MAP.getOrDefault(lastCharUpper, SIZE_MAP.get(lastCharLower)); if (mul != null) s = s.substring(0, s.length() - 1); final long v; try { v = Long.parseLong(s); } catch (NumberFormatException ex) { throw new IllegalArgumentException("\"" + s + "\" is not a number"); } return v * (mul == null ? 1L : mul.longValue()); }  static long parse(String s); static String render(long v); }
BytesParser { public static long parse(String s) { final char lastChar; try { lastChar = s.charAt(s.length() - 1); } catch (IndexOutOfBoundsException ex) { throw new IllegalArgumentException("\"\" is not a valid size"); } final char lastCharLower = Character.toLowerCase(lastChar); final char lastCharUpper = Character.toUpperCase(lastChar); final Long mul = SIZE_MAP.getOrDefault(lastCharUpper, SIZE_MAP.get(lastCharLower)); if (mul != null) s = s.substring(0, s.length() - 1); final long v; try { v = Long.parseLong(s); } catch (NumberFormatException ex) { throw new IllegalArgumentException("\"" + s + "\" is not a number"); } return v * (mul == null ? 1L : mul.longValue()); }  static long parse(String s); static String render(long v); static final Map<Character, Long> SIZE_MAP; }