@Test public void testString2Size() { assertEquals(-1, TypeUtils.string2Size(null, -1)); assertEquals(-1, TypeUtils.string2Size("", -1)); assertEquals(0, TypeUtils.string2Size("0", -1)); assertEquals(0, TypeUtils.string2Size("0K", -1)); assertEquals(0, TypeUtils.string2Size("0Mb", -1)); assertEquals(0, TypeUtils.string2Size("0G", -1)); assertEquals(0, TypeUtils.string2Size("0T", -1)); assertEquals(0, TypeUtils.string2Size("0P", -1)); assertEquals(12345, TypeUtils.string2Size("12345", -1)); assertEquals(TypeUtils.KB, TypeUtils.string2Size("1K", -1)); assertEquals(TypeUtils.MB, TypeUtils.string2Size("1M", -1)); assertEquals(TypeUtils.GB, TypeUtils.string2Size("1GB", -1)); assertEquals(TypeUtils.TB, TypeUtils.string2Size("1T", -1)); assertEquals(TypeUtils.PB, TypeUtils.string2Size("1Pb", -1)); assertEquals(TypeUtils.KB, TypeUtils.string2Size("1 K", -1)); assertEquals(TypeUtils.MB, TypeUtils.string2Size("1 M", -1)); assertEquals(TypeUtils.GB, TypeUtils.string2Size("1 G", -1)); assertEquals(TypeUtils.TB, TypeUtils.string2Size("1 T", -1)); assertEquals(TypeUtils.PB, TypeUtils.string2Size("1 P", -1)); assertEquals(TypeUtils.KB, TypeUtils.string2Size("1kb", -1)); assertEquals(TypeUtils.MB, TypeUtils.string2Size("1m", -1)); assertEquals(TypeUtils.GB, TypeUtils.string2Size("1gB", -1)); assertEquals(TypeUtils.TB, TypeUtils.string2Size("1t", -1)); assertEquals(TypeUtils.PB, TypeUtils.string2Size("1p", -1)); assertEquals(0.5 * TypeUtils.KB, TypeUtils.string2Size("0.5k", -1), 3); assertEquals(1.7 * TypeUtils.KB, TypeUtils.string2Size("1.7k", -1), 3); assertEquals(6.843 * TypeUtils.KB, TypeUtils.string2Size("6.843k", -1), 3); }
public static long string2Size(String size, long defaultValue) { if (StringUtils.isBlank(size)) { return defaultValue; } final Matcher matcher = SPACE_PATTERN.matcher(size); if (matcher.matches()) { try { final double value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(matcher.group(1)).doubleValue(); final String units = matcher.group(3); if (units.isEmpty()) { return (long) value; } else if (units.equalsIgnoreCase("K")) { return (long) (value * KB); } else if (units.equalsIgnoreCase("M")) { return (long) (value * MB); } else if (units.equalsIgnoreCase("G")) { return (long) (value * GB); } else if (units.equalsIgnoreCase("T")) { return (long) (value * TB); } else if (units.equalsIgnoreCase("P")) { return (long) (value * PB); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, default: %d.", size, defaultValue)); } } } return defaultValue; }
TypeUtils { public static long string2Size(String size, long defaultValue) { if (StringUtils.isBlank(size)) { return defaultValue; } final Matcher matcher = SPACE_PATTERN.matcher(size); if (matcher.matches()) { try { final double value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(matcher.group(1)).doubleValue(); final String units = matcher.group(3); if (units.isEmpty()) { return (long) value; } else if (units.equalsIgnoreCase("K")) { return (long) (value * KB); } else if (units.equalsIgnoreCase("M")) { return (long) (value * MB); } else if (units.equalsIgnoreCase("G")) { return (long) (value * GB); } else if (units.equalsIgnoreCase("T")) { return (long) (value * TB); } else if (units.equalsIgnoreCase("P")) { return (long) (value * PB); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, default: %d.", size, defaultValue)); } } } return defaultValue; } }
TypeUtils { public static long string2Size(String size, long defaultValue) { if (StringUtils.isBlank(size)) { return defaultValue; } final Matcher matcher = SPACE_PATTERN.matcher(size); if (matcher.matches()) { try { final double value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(matcher.group(1)).doubleValue(); final String units = matcher.group(3); if (units.isEmpty()) { return (long) value; } else if (units.equalsIgnoreCase("K")) { return (long) (value * KB); } else if (units.equalsIgnoreCase("M")) { return (long) (value * MB); } else if (units.equalsIgnoreCase("G")) { return (long) (value * GB); } else if (units.equalsIgnoreCase("T")) { return (long) (value * TB); } else if (units.equalsIgnoreCase("P")) { return (long) (value * PB); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, default: %d.", size, defaultValue)); } } } return defaultValue; } private  TypeUtils(); }
TypeUtils { public static long string2Size(String size, long defaultValue) { if (StringUtils.isBlank(size)) { return defaultValue; } final Matcher matcher = SPACE_PATTERN.matcher(size); if (matcher.matches()) { try { final double value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(matcher.group(1)).doubleValue(); final String units = matcher.group(3); if (units.isEmpty()) { return (long) value; } else if (units.equalsIgnoreCase("K")) { return (long) (value * KB); } else if (units.equalsIgnoreCase("M")) { return (long) (value * MB); } else if (units.equalsIgnoreCase("G")) { return (long) (value * GB); } else if (units.equalsIgnoreCase("T")) { return (long) (value * TB); } else if (units.equalsIgnoreCase("P")) { return (long) (value * PB); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, default: %d.", size, defaultValue)); } } } return defaultValue; } private  TypeUtils(); static int byteArray2Int(byte[] bytes); static long byteArray2Long(byte[] bytes); static byte[] int2ByteArray(int i); static byte[] long2ByteArray(long l); static String byteArray2Ipv4(byte[] bytes); static String byteArray2Ipv6(byte[] bytes); static String byteArray2Ip(byte[] bytes); static List<String> csv2List(String line); static byte[] Ip2byteArrayV4(String ipv4); static byte[] Ip2byteArrayV6(String ipv6); static byte[] Ip2byteArray(String ip); static String byteArray2HexString(byte[] byteArray); static String intArray2HexString(int[] intArray); static String longArray2HexString(int[] longArray); static String byteArray2Base64(byte[] value); static byte[] base642ByteArray(String base64Str); static String byte2HexString(byte b); static byte[] hexString2ByteArray(String input); static boolean string2Boolean(String str, boolean defaultValue); static int string2Int(String str, Radix radix, int defaultValue); static long string2Long(String str, Radix radix, long defaultValue); static float string2Float(String str, float defaultValue); static double string2Double(String str, double defaultValue); static long string2Size(String size, long defaultValue); static long string2TimePeriod(String timePeriod, long defaultValue); static boolean equals(T t1, T t2); static boolean maybeUpdate(T tar, T src); }
TypeUtils { public static long string2Size(String size, long defaultValue) { if (StringUtils.isBlank(size)) { return defaultValue; } final Matcher matcher = SPACE_PATTERN.matcher(size); if (matcher.matches()) { try { final double value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(matcher.group(1)).doubleValue(); final String units = matcher.group(3); if (units.isEmpty()) { return (long) value; } else if (units.equalsIgnoreCase("K")) { return (long) (value * KB); } else if (units.equalsIgnoreCase("M")) { return (long) (value * MB); } else if (units.equalsIgnoreCase("G")) { return (long) (value * GB); } else if (units.equalsIgnoreCase("T")) { return (long) (value * TB); } else if (units.equalsIgnoreCase("P")) { return (long) (value * PB); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, default: %d.", size, defaultValue)); } } } return defaultValue; } private  TypeUtils(); static int byteArray2Int(byte[] bytes); static long byteArray2Long(byte[] bytes); static byte[] int2ByteArray(int i); static byte[] long2ByteArray(long l); static String byteArray2Ipv4(byte[] bytes); static String byteArray2Ipv6(byte[] bytes); static String byteArray2Ip(byte[] bytes); static List<String> csv2List(String line); static byte[] Ip2byteArrayV4(String ipv4); static byte[] Ip2byteArrayV6(String ipv6); static byte[] Ip2byteArray(String ip); static String byteArray2HexString(byte[] byteArray); static String intArray2HexString(int[] intArray); static String longArray2HexString(int[] longArray); static String byteArray2Base64(byte[] value); static byte[] base642ByteArray(String base64Str); static String byte2HexString(byte b); static byte[] hexString2ByteArray(String input); static boolean string2Boolean(String str, boolean defaultValue); static int string2Int(String str, Radix radix, int defaultValue); static long string2Long(String str, Radix radix, long defaultValue); static float string2Float(String str, float defaultValue); static double string2Double(String str, double defaultValue); static long string2Size(String size, long defaultValue); static long string2TimePeriod(String timePeriod, long defaultValue); static boolean equals(T t1, T t2); static boolean maybeUpdate(T tar, T src); static final long KB; static final long MB; static final long GB; static final long TB; static final long PB; static final long SEC; static final long MIN; static final long HOUR; static final long DAY; static final long WEEK; static final long MON; static final long QUAR; static final long YEAR; }