@Test public void of() { assertDuration(0, TimeUnit.NANOSECONDS, "0ns"); assertDuration(200, TimeUnit.NANOSECONDS, "200ns"); assertDuration(0, TimeUnit.MICROSECONDS, "0us"); assertDuration(200, TimeUnit.MICROSECONDS, "200us"); assertDuration(0, TimeUnit.MILLISECONDS, "0ms"); assertDuration(200, TimeUnit.MILLISECONDS, "200ms"); assertDuration(0, TimeUnit.SECONDS, "0s"); assertDuration(200, TimeUnit.SECONDS, "200s"); assertDuration(0, TimeUnit.MINUTES, "0m"); assertDuration(200, TimeUnit.MINUTES, "200m"); assertDuration(0, TimeUnit.HOURS, "0h"); assertDuration(200, TimeUnit.HOURS, "200h"); assertDuration(0, TimeUnit.DAYS, "0d"); assertDuration(200, TimeUnit.DAYS, "200d"); }
public static Duration of(String rawDuration) { if (rawDuration == null) { return null; } if (rawDuration.length() <= 1) { throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must be longer than 1", Duration.class.getSimpleName(), rawDuration)); } int len = rawDuration.length(); char lastChar = rawDuration.charAt(len - 1); try { switch (lastChar) { case 's': char lastButOneChar = rawDuration.charAt(len - 2); switch (lastButOneChar) { case 'n': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.NANOSECONDS); case 'u': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MICROSECONDS); case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MILLISECONDS); default: return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.SECONDS); } case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.MINUTES); case 'h': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.HOURS); case 'd': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.DAYS); default: throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must end with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format( "Cannot parse a %s out of string [%s]: must consist of a number appended with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } }
Duration { public static Duration of(String rawDuration) { if (rawDuration == null) { return null; } if (rawDuration.length() <= 1) { throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must be longer than 1", Duration.class.getSimpleName(), rawDuration)); } int len = rawDuration.length(); char lastChar = rawDuration.charAt(len - 1); try { switch (lastChar) { case 's': char lastButOneChar = rawDuration.charAt(len - 2); switch (lastButOneChar) { case 'n': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.NANOSECONDS); case 'u': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MICROSECONDS); case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MILLISECONDS); default: return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.SECONDS); } case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.MINUTES); case 'h': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.HOURS); case 'd': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.DAYS); default: throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must end with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format( "Cannot parse a %s out of string [%s]: must consist of a number appended with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } }
Duration { public static Duration of(String rawDuration) { if (rawDuration == null) { return null; } if (rawDuration.length() <= 1) { throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must be longer than 1", Duration.class.getSimpleName(), rawDuration)); } int len = rawDuration.length(); char lastChar = rawDuration.charAt(len - 1); try { switch (lastChar) { case 's': char lastButOneChar = rawDuration.charAt(len - 2); switch (lastButOneChar) { case 'n': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.NANOSECONDS); case 'u': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MICROSECONDS); case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MILLISECONDS); default: return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.SECONDS); } case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.MINUTES); case 'h': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.HOURS); case 'd': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.DAYS); default: throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must end with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format( "Cannot parse a %s out of string [%s]: must consist of a number appended with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } Duration(long value, TimeUnit unit); }
Duration { public static Duration of(String rawDuration) { if (rawDuration == null) { return null; } if (rawDuration.length() <= 1) { throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must be longer than 1", Duration.class.getSimpleName(), rawDuration)); } int len = rawDuration.length(); char lastChar = rawDuration.charAt(len - 1); try { switch (lastChar) { case 's': char lastButOneChar = rawDuration.charAt(len - 2); switch (lastButOneChar) { case 'n': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.NANOSECONDS); case 'u': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MICROSECONDS); case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MILLISECONDS); default: return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.SECONDS); } case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.MINUTES); case 'h': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.HOURS); case 'd': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.DAYS); default: throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must end with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format( "Cannot parse a %s out of string [%s]: must consist of a number appended with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } Duration(long value, TimeUnit unit); static Duration maxValue(); static Duration of(String rawDuration); @Override boolean equals(Object obj); TimeUnit getUnit(); long getValue(); @Override int hashCode(); long toMilliseconds(); @Override String toString(); }
Duration { public static Duration of(String rawDuration) { if (rawDuration == null) { return null; } if (rawDuration.length() <= 1) { throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must be longer than 1", Duration.class.getSimpleName(), rawDuration)); } int len = rawDuration.length(); char lastChar = rawDuration.charAt(len - 1); try { switch (lastChar) { case 's': char lastButOneChar = rawDuration.charAt(len - 2); switch (lastButOneChar) { case 'n': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.NANOSECONDS); case 'u': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MICROSECONDS); case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 2)), TimeUnit.MILLISECONDS); default: return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.SECONDS); } case 'm': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.MINUTES); case 'h': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.HOURS); case 'd': return new Duration(Long.parseLong(rawDuration.substring(0, len - 1)), TimeUnit.DAYS); default: throw new IllegalArgumentException( String.format("Cannot parse a %s out of string [%s]: must end with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format( "Cannot parse a %s out of string [%s]: must consist of a number appended with any of time units [%s]", Duration.class.getSimpleName(), rawDuration, UNIT_TO_STRING_MAP.values())); } } Duration(long value, TimeUnit unit); static Duration maxValue(); static Duration of(String rawDuration); @Override boolean equals(Object obj); TimeUnit getUnit(); long getValue(); @Override int hashCode(); long toMilliseconds(); @Override String toString();  }