Commit 69f863d5 authored by Sören Henning's avatar Sören Henning

moving backwards iterable to interface

parent eba5b5e3
......@@ -6,13 +6,15 @@ import java.util.Deque;
import java.util.Iterator;
import java.util.stream.Stream;
import anomalydetection.util.BackwardsIterable;
/**
*
*
* @author Sören Henning
*
*/
public class TimeSeries implements Iterable<TimeSeriesPoint> {
public class TimeSeries implements Iterable<TimeSeriesPoint>, BackwardsIterable<TimeSeriesPoint> {
private final Deque<TimeSeriesPoint> timeSeriesPoints;
......@@ -112,24 +114,11 @@ public class TimeSeries implements Iterable<TimeSeriesPoint> {
* Returns an iterator over the time series points in this time series. The
* points will be returned in temporal order from latest to earliest.
*/
@Override
public Iterator<TimeSeriesPoint> backwardsIterator() {
return this.timeSeriesPoints.descendingIterator();
}
/**
* Returns an iterable that iterates the points backwards. It is backed by
* the time series, so changes to the time series are reflected in the
* iterable, and vice-versa.
*/
public Iterable<TimeSeriesPoint> backwards() {
return new Iterable<TimeSeriesPoint>() {
@Override
public Iterator<TimeSeriesPoint> iterator() {
return backwardsIterator();
}
};
}
/**
* Returns an array containing all of the values of the time series point
* in this time series in temporal order from earliest to latest.
......
package anomalydetection.util;
import java.util.Iterator;
/**
* Implementing this interface allows an object to get iterated backwards. The
* object returned by {@code backwards()} can be used by a "foreach" statement.
*
* @author Sören Henning
*
* @param <T>
* the type of elements returned by the iterator
*/
public interface BackwardsIterable<T> {
/**
* Returns an Iterator that iterates the elements backwards.
*/
public Iterator<T> backwardsIterator();
/**
* Returns an Iterable that iterates the elements backwards using the
* Iterator returned by {@code backwardsIterator()}.
*/
default public Iterable<T> backwards() {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return backwardsIterator();
}
};
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment