/** * 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; }
/** * 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; }
/** * 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); }
/** * 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); }