@Test public void testString2TimePeriod() { assertEquals(-1, TypeUtils.string2TimePeriod(null, -1)); assertEquals(-1, TypeUtils.string2TimePeriod("", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0SEC", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0MIN", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0HOUR", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0DAY", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0WEEK", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0MON", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0QUAR", -1)); assertEquals(0, TypeUtils.string2TimePeriod("0YEAR", -1)); assertEquals(12345, TypeUtils.string2TimePeriod("12345", -1)); assertEquals(TypeUtils.SEC, TypeUtils.string2TimePeriod("1SEC", -1)); assertEquals(TypeUtils.MIN, TypeUtils.string2TimePeriod("1MIN", -1)); assertEquals(TypeUtils.HOUR, TypeUtils.string2TimePeriod("1HOUR", -1)); assertEquals(TypeUtils.DAY, TypeUtils.string2TimePeriod("1DAY", -1)); assertEquals(TypeUtils.WEEK, TypeUtils.string2TimePeriod("1WEEK", -1)); assertEquals(TypeUtils.MON, TypeUtils.string2TimePeriod("1MON", -1)); assertEquals(TypeUtils.QUAR, TypeUtils.string2TimePeriod("1QUAR", -1)); assertEquals(TypeUtils.YEAR, TypeUtils.string2TimePeriod("1YEAR", -1)); assertEquals(TypeUtils.SEC, TypeUtils.string2TimePeriod("1 SEC", -1)); assertEquals(TypeUtils.MIN, TypeUtils.string2TimePeriod("1 MIN", -1)); assertEquals(TypeUtils.HOUR, TypeUtils.string2TimePeriod("1 HOUR", -1)); assertEquals(TypeUtils.DAY, TypeUtils.string2TimePeriod("1 DAY", -1)); assertEquals(TypeUtils.WEEK, TypeUtils.string2TimePeriod("1 WEEK", -1)); assertEquals(TypeUtils.MON, TypeUtils.string2TimePeriod("1 MON", -1)); assertEquals(TypeUtils.QUAR, TypeUtils.string2TimePeriod("1 QUAR", -1)); assertEquals(TypeUtils.YEAR, TypeUtils.string2TimePeriod("1 YEAR", -1)); assertEquals(TypeUtils.SEC, TypeUtils.string2TimePeriod("1Sec", -1)); assertEquals(TypeUtils.MIN, TypeUtils.string2TimePeriod("1Min", -1)); assertEquals(TypeUtils.HOUR, TypeUtils.string2TimePeriod("1hoUR", -1)); assertEquals(TypeUtils.DAY, TypeUtils.string2TimePeriod("1day", -1)); assertEquals(TypeUtils.WEEK, TypeUtils.string2TimePeriod("1weEK", -1)); assertEquals(TypeUtils.MON, TypeUtils.string2TimePeriod("1mon", -1)); assertEquals(TypeUtils.QUAR, TypeUtils.string2TimePeriod("1QuaR", -1)); assertEquals(TypeUtils.YEAR, TypeUtils.string2TimePeriod("1YeaR", -1)); assertEquals(0.5 * TypeUtils.MON, TypeUtils.string2TimePeriod("0.5MON", -1), 3); assertEquals(1.7 * TypeUtils.YEAR, TypeUtils.string2TimePeriod("1.7YEAR", -1), 3); assertEquals(6.843 * TypeUtils.HOUR, TypeUtils.string2TimePeriod("6.843 hour", -1), 3); }
public static long string2TimePeriod(String timePeriod, long defaultValue) { if (StringUtils.isBlank(timePeriod)) { return defaultValue; } final Matcher matcher = TIME_PERIOD_PATTERN.matcher(timePeriod); 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("SEC")) { return (long) (value * SEC); } else if (units.equalsIgnoreCase("MIN")) { return (long) (value * MIN); } else if (units.equalsIgnoreCase("HOUR")) { return (long) (value * HOUR); } else if (units.equalsIgnoreCase("DAY")) { return (long) (value * DAY); } else if (units.equalsIgnoreCase("WEEK")) { return (long) (value * WEEK); } else if (units.equalsIgnoreCase("MON")) { return (long) (value * MON); } else if (units.equalsIgnoreCase("QUAR")) { return (long) (value * QUAR); } else if (units.equalsIgnoreCase("YEAR")) { return (long) (value * YEAR); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to time period fail, default: %d.", timePeriod, defaultValue)); } } } return defaultValue; }
TypeUtils { public static long string2TimePeriod(String timePeriod, long defaultValue) { if (StringUtils.isBlank(timePeriod)) { return defaultValue; } final Matcher matcher = TIME_PERIOD_PATTERN.matcher(timePeriod); 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("SEC")) { return (long) (value * SEC); } else if (units.equalsIgnoreCase("MIN")) { return (long) (value * MIN); } else if (units.equalsIgnoreCase("HOUR")) { return (long) (value * HOUR); } else if (units.equalsIgnoreCase("DAY")) { return (long) (value * DAY); } else if (units.equalsIgnoreCase("WEEK")) { return (long) (value * WEEK); } else if (units.equalsIgnoreCase("MON")) { return (long) (value * MON); } else if (units.equalsIgnoreCase("QUAR")) { return (long) (value * QUAR); } else if (units.equalsIgnoreCase("YEAR")) { return (long) (value * YEAR); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to time period fail, default: %d.", timePeriod, defaultValue)); } } } return defaultValue; } }
TypeUtils { public static long string2TimePeriod(String timePeriod, long defaultValue) { if (StringUtils.isBlank(timePeriod)) { return defaultValue; } final Matcher matcher = TIME_PERIOD_PATTERN.matcher(timePeriod); 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("SEC")) { return (long) (value * SEC); } else if (units.equalsIgnoreCase("MIN")) { return (long) (value * MIN); } else if (units.equalsIgnoreCase("HOUR")) { return (long) (value * HOUR); } else if (units.equalsIgnoreCase("DAY")) { return (long) (value * DAY); } else if (units.equalsIgnoreCase("WEEK")) { return (long) (value * WEEK); } else if (units.equalsIgnoreCase("MON")) { return (long) (value * MON); } else if (units.equalsIgnoreCase("QUAR")) { return (long) (value * QUAR); } else if (units.equalsIgnoreCase("YEAR")) { return (long) (value * YEAR); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to time period fail, default: %d.", timePeriod, defaultValue)); } } } return defaultValue; } private  TypeUtils(); }
TypeUtils { public static long string2TimePeriod(String timePeriod, long defaultValue) { if (StringUtils.isBlank(timePeriod)) { return defaultValue; } final Matcher matcher = TIME_PERIOD_PATTERN.matcher(timePeriod); 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("SEC")) { return (long) (value * SEC); } else if (units.equalsIgnoreCase("MIN")) { return (long) (value * MIN); } else if (units.equalsIgnoreCase("HOUR")) { return (long) (value * HOUR); } else if (units.equalsIgnoreCase("DAY")) { return (long) (value * DAY); } else if (units.equalsIgnoreCase("WEEK")) { return (long) (value * WEEK); } else if (units.equalsIgnoreCase("MON")) { return (long) (value * MON); } else if (units.equalsIgnoreCase("QUAR")) { return (long) (value * QUAR); } else if (units.equalsIgnoreCase("YEAR")) { return (long) (value * YEAR); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to time period fail, default: %d.", timePeriod, 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 string2TimePeriod(String timePeriod, long defaultValue) { if (StringUtils.isBlank(timePeriod)) { return defaultValue; } final Matcher matcher = TIME_PERIOD_PATTERN.matcher(timePeriod); 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("SEC")) { return (long) (value * SEC); } else if (units.equalsIgnoreCase("MIN")) { return (long) (value * MIN); } else if (units.equalsIgnoreCase("HOUR")) { return (long) (value * HOUR); } else if (units.equalsIgnoreCase("DAY")) { return (long) (value * DAY); } else if (units.equalsIgnoreCase("WEEK")) { return (long) (value * WEEK); } else if (units.equalsIgnoreCase("MON")) { return (long) (value * MON); } else if (units.equalsIgnoreCase("QUAR")) { return (long) (value * QUAR); } else if (units.equalsIgnoreCase("YEAR")) { return (long) (value * YEAR); } } catch (final ParseException e) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to time period fail, default: %d.", timePeriod, 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; }