/** * @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); }
/** * @return the duration of this timespan in milliseconds */ public long getDurationInMilliseconds() { // 100% equivalent to getDuration(TimeUnit.MILLISECOND) but faster! return getDuration() * getTimeUnit().getMillisecondsCount(); }
/** * @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); }
/** * @return the duration of this timespan in milliseconds */ public long getDurationInMilliseconds() { // 100% equivalent to getDuration(TimeUnit.MILLISECOND) but faster! return getDuration() * getTimeUnit().getMillisecondsCount(); }
/** * 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(); }
/** * 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(); } }
/** * 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(); }
/** * 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(); } }
/** * 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 and returns a new timespan whose duration is {@code this} * timespan's duration minus the {@code other} timespan's duration. * <p> * The time unit is preserved if {@code other} has the same unit * as {@code this}. * <p> * Negative timespans are not supported, so if the {@code other} * timespan duration is greater than {@code this} timespan duration, * a timespan of zero is returned (i.e., a negative timespan is never * returned). * * @param other * the timespan to subtract from this one * * @return a new timespan representing {@code this - other} */ public Timespan substractWithZeroFloor(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { long delta = Math.max(0, getDuration() - other.getDuration()); return new Timespan(delta, getTimeUnit()); } long delta = Math.max(0, getDurationInMilliseconds() - other.getDurationInMilliseconds()); return new Timespan(delta, TimeUnit.MILLISECOND); }
/** * 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); }
/** * 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); }
/** * Creates and returns a new timespan whose duration is {@code this} * timespan's duration minus the {@code other} timespan's duration. * <p> * The time unit is preserved if {@code other} has the same unit * as {@code this}. * <p> * Negative timespans are not supported, so if the {@code other} * timespan duration is greater than {@code this} timespan duration, * a timespan of zero is returned (i.e., a negative timespan is never * returned). * * @param other * the timespan to subtract from this one * * @return a new timespan representing {@code this - other} */ public Timespan substractWithZeroFloor(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { long delta = Math.max(0, getDuration() - other.getDuration()); return new Timespan(delta, getTimeUnit()); } long delta = Math.max(0, getDurationInMilliseconds() - other.getDurationInMilliseconds()); return new Timespan(delta, TimeUnit.MILLISECOND); }