/** * Returns an {@link IntervalsList} built from a set of {@link Timestamp} using the following * algorithm: if the {@link TimeDuration} between two {@link Timestamp} is inferior to the defined * tolerance, they are grouped in the same {@link TimeInterval}. A {@link TimeInterval} duration * can not be inferior to the defined tolerance. TODO: better algorithm ? */ public IntervalsList toIntervalsList() { IntervalsList ilist = new IntervalsList(); if (isEmpty()) // empty list return ilist; Iterator<Timestamp> iterator = timestamps.iterator(); Timestamp first = iterator.next(); Timestamp previous = first; while (iterator.hasNext()) { Timestamp next = iterator.next(); if (previous.plus(tolerance).compareTo(next) < 0) { if (first.equals(previous)) { ilist.addToSelf(intervalOfSinglePoint(first)); } else { ilist.addToSelf(TimeInterval.between(first, previous)); } first = next; } previous = next; } // Process end of set if (first.equals(previous)) { ilist.addToSelf(intervalOfSinglePoint(first)); } else { ilist.addToSelf(TimeInterval.between(first, previous)); } return ilist; }
// Generate a TimeInterval from a single timestamp private TimeInterval intervalOfSinglePoint(Timestamp first) { return TimeInterval.between(first, first.plus(tolerance)); }