/** * Adds another timespan to this timespan and return a brand new one. Note that the unit is * preserved if <code>other</code> has the same unit as 'this'. * * @param other * the timespan to add * * @return a brand new timespan. */ public Timespan add(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { return new Timespan(getDuration() + other.getDuration(), getTimeUnit()); } return new Timespan(getDurationInMilliseconds() + other.getDurationInMilliseconds(), TimeUnit.MILLISECOND); }
/** * Creates a timespan from a list of other timespans. * * @return a timespan representing the sum of all the timespans provided */ public static Timespan create(Timespan... timespans) { if (timespans == null) { return null; } if (timespans.length == 0) { return ZERO_MILLISECONDS; } Timespan res = timespans[0]; for (int i = 1; i < timespans.length; i++) { Timespan timespan = timespans[i]; res = res.add(timespan); } return res; }
/** * @return a string representing this timespan (ex: 3h2m23s). Note that if a duration is missing, * it is not part of the string (the string would be 3h23s and not 3h0m23s). * * @see #getCanonicalTimespans() */ public String getCanonicalString() { return getAsString(CANONICAL_TIME_UNITS); }
/** * Shortcut for creating a timespan and then retrieving the value in milliseconds. * * @param timespan * the timespan as a string * * @return the value in milliseconds */ public static long toMilliseconds(String timespan) { return parseTimespan(timespan).getDurationInMilliseconds(); } }
/** * @param timeUnit * the unit of time you want this timespan ass * * @return the duration of this timespan expressed in the time unit provided. Note that all * units below timeUnit will be truncated! (ex: 3h20m45s will return 3 if timeUnit=HOUR). */ public long getDuration(TimeUnit timeUnit) { return truncate(timeUnit).getDuration(); }
/** * @return a (potentially new) version of this timestamp where the unit is * {@link TimeUnit#MILLISECOND}. */ public Timespan toMillisecondsTimespan() { if (getTimeUnit() == TimeUnit.MILLISECOND) { return this; } return new Timespan(getDurationInMilliseconds(), TimeUnit.MILLISECOND); }
private void executeSleep(Sleep sleep) { final long milliseconds = Timespan.toMilliseconds(sleep.value()); try { TimeUnit.MILLISECONDS.sleep(milliseconds); } catch (InterruptedException e) { throw new RuntimeException(e); } }
/** * Truncates this timespan to the given time unit. Example: if this is 1h20m5s then * <code>truncate(TimeUnit.HOUR)</code> will return 1h, * <code>truncate(TimeUnit.MINUTE)</code> will return 1h20m and * <code>truncate(TimeUnit.SECOND)</code> will return 1h20m5s * * @param timeUnit * the unit you want the timespan in * * @return this timespan if time unit matches otherwise a new one with the given unit */ public Timespan truncate(TimeUnit timeUnit) { if (getTimeUnit() == timeUnit) { return this; } return truncateDurationToUnit(getDurationInMilliseconds(), timeUnit); }
/** * 2 timespans can be different (1h and 3600s) while representing the same duration expressed * in milliseconds... This method tests for this. * * @param timespan * the other timespan to compare with * * @return <code>true</code> if this timespan and the provided one represent the same * duration of time. */ public boolean equalsDurationInMilliseconds(Timespan timespan) { // shortcut when time unit are the same if (timespan.getTimeUnit() == getTimeUnit()) { return timespan.getDuration() == getDuration(); } else { return getDurationInMilliseconds() == timespan.getDurationInMilliseconds(); } }
Timespan res = null; EnumMap<TimeUnit, Timespan> canonicalTimespans = getCanonicalTimespans(); if (timespan != null && timespan.getDuration() > 0) { if (res == null) { res = timespan; } else { res = res.add(timespan);
/** * Returns a string representing this timespan expressed with the units provided. * * @param timeUnits * the timeunits you want in the decomposition * * @return a string representation using the units. * * @see #getAsTimespans(EnumSet) */ public String getAsString(EnumSet<TimeUnit> timeUnits) { StringBuilder sb = new StringBuilder(); EnumMap<TimeUnit, Timespan> canonicalTimespans = getAsTimespans(timeUnits); for (TimeUnit timeUnit : TIME_UNIT_ORDER) { if (canonicalTimespans.containsKey(timeUnit)) { long duration = canonicalTimespans.get(timeUnit).getDuration(); if (duration > 0) { sb.append(duration).append(timeUnit.getDisplayChar()); } } } if (sb.length() == 0) { sb.append(0); if (timeUnits.contains(getTimeUnit())) { sb.append(getTimeUnit().getDisplayChar()); } } return sb.toString(); }
if (isDigit(c)) { expectingDigits = false; continue; try { long duration = Long.parseLong(timespan.substring(startDigitsIdx, i)); timespans[timeUnitOrderIdx++] = new Timespan(duration, timeUnit); startDigitsIdx = i + 1; expectingDigits = true; try { long duration = Long.parseLong(timespan.substring(startDigitsIdx, len)); timespans[timeUnitOrderLen - 1] = new Timespan(duration, TimeUnit.MILLISECOND); count++; } catch (NumberFormatException e) { return create(ts);
/** * Convenient call when providing seconds * * @return the timespan */ public static Timespan seconds(long seconds) { return new Timespan(seconds, TimeUnit.SECOND); }
/** * @return the duration of this timespan in milliseconds */ public long getDurationInMilliseconds() { // 100% equivalent to getDuration(TimeUnit.MILLISECOND) but faster! return getDuration() * getTimeUnit().getMillisecondsCount(); }
/** * Decomposes this timespan as a map for each unit provided. Example: if this timespan represents * 63s and you provide m/s/ms then you will get 3 timespans in the map: one of 1mn, one for 3s * and one for 0ms. * * @param timeUnits * the time units you want to be part of the decomposition. * * @return a map containing an entry for each time unit provided. All others are <code>null</code>. */ public EnumMap<TimeUnit, Timespan> getAsTimespans(EnumSet<TimeUnit> timeUnits) { EnumMap<TimeUnit, Timespan> res = new EnumMap<TimeUnit, Timespan>(TimeUnit.class); long durationInMillis = getDurationInMilliseconds(); for (TimeUnit timeUnit : TIME_UNIT_ORDER) { if (timeUnits.contains(timeUnit)) { Timespan timespan = truncateDurationToUnit(durationInMillis, timeUnit); res.put(timeUnit, timespan); durationInMillis -= timespan.getDurationInMilliseconds(); } } return res; }
/** * @return the duration of this timespan expressed in hours. If m/s/ms are present, * they will be truncated! */ public long getDurationInHours() { return getDuration(TimeUnit.HOUR); }
/** * @param baseMillis * the starting point * * @return baseMillis - this timespan */ public long pastTimeMillis(long baseMillis) { return baseMillis - getDurationInMilliseconds(); }
/** * There are many ways to represent the same timespan: 3h, 180m... this call return the unique * way to express it such that ms is < 1000, s is < 60, m is < 60, h is < 24, * d is < 7. * * @return this timespan as a canonical representation: an entry for w/dh/m/s/ms is returned * * @see #getAsTimespans(EnumSet) */ public EnumMap<TimeUnit, Timespan> getCanonicalTimespans() { return getAsTimespans(CANONICAL_TIME_UNITS); }
/** * @return a string representation of the object. */ @Override public String toString() { return getCanonicalString(); }
private void executeSleep(Sleep sleep) { final long milliseconds = Timespan.toMilliseconds(sleep.value()); try { TimeUnit.MILLISECONDS.sleep(milliseconds); } catch (InterruptedException e) { throw new RuntimeException(e); } }