/** * 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; }
private void prune() { // Remove all values that are too old TimeInterval periodAllowed = cachedPeriod.before(buffer.getLast().getTimestamp()); while (!buffer.isEmpty() && !periodAllowed.contains(buffer.getFirst().getTimestamp())) { // Discard value buffer.removeFirst(); } }
/** * Returns all values since last check and removes values from the queue. * * @return a new array with the value; never null */ @Override public List<T> getValue() { synchronized (buffer) { if (buffer.isEmpty()) return Collections.emptyList(); // period allowed time = latest - msCache / 1000 TimeInterval periodAllowed = cachedPeriod.before(TimeStamp.asTimestamp(TimeSupport.timestampOf(buffer.getLast()))); while (!buffer.isEmpty() && !periodAllowed.contains( TimeStamp.asTimestamp(TimeSupport.timestampOf(buffer.getFirst())))) { // Discard value buffer.removeFirst(); } return new ArrayList<T>(buffer); } }
// Generate a TimeInterval from a single timestamp private TimeInterval intervalOfSinglePoint(Timestamp first) { return TimeInterval.between(first, first.plus(tolerance)); }