List of usage examples for org.joda.time Duration toIntervalFrom
public Interval toIntervalFrom(ReadableInstant startInstant)
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Make this TL discrete, returning the TL; * As a side effect, removes values outside the start and end times. * * @param start/*from w ww .j av a 2 s . co m*/ * the start * @param end * the end * @param stepSize * the step size * @return this */ @JsonIgnore public PowerTimeLine discrete(final DateTime start, final DateTime end, final Duration stepSize) { final PowerTimeLine newTL = new PowerTimeLine(); newTL.timestamp = this.timestamp; final ArrayList<PowerTime> steps = new ArrayList<PowerTime>(); long offset = new Duration(timestamp, start).getMillis(); Interval interval = stepSize.toIntervalFrom(start); while (interval.getEnd().isBefore(end) || interval.getEnd().isEqual(end)) { steps.add(new PowerTime(offset, 0)); offset += interval.toDurationMillis(); interval = stepSize.toIntervalFrom(timestamp.plusMillis((int) offset)); } newTL.setSeries(steps); this.add(newTL).zeroBefore(start).zeroFrom(end); final Duration diff = new Duration(start, end); if (series.size() > (diff.getMillis() / stepSize.getMillis())) { int index = 0; long expectedOffset = new Duration(timestamp, start).getMillis() + stepSize.getMillis(); while (index < series.size() - 1) { PowerTime pt = series.get(index); ArrayList<PowerTime> temp = new ArrayList<PowerTime>(); int nextIndex = index + 1; PowerTime next = series.get(nextIndex); while (next.getOffset() < expectedOffset) { temp.add(next); series.remove(nextIndex); if (nextIndex == series.size()) { break; } next = series.get(nextIndex); } if (temp.size() > 0) { temp.add(0, pt); double integral = getIntegral(pt.getOffset(), pt.getOffset() + stepSize.getMillis(), temp); series.set(index, new PowerTime(pt.getOffset(), integral / stepSize.getMillis())); } index++; expectedOffset += stepSize.getMillis(); } } return this; }