package cz.cuni.mff.d3s.spl.core.data.segment;

import cz.cuni.mff.d3s.spl.core.data.SampleBasedDataSource;
import cz.cuni.mff.d3s.spl.core.data.Statistics;

public class SlidingTimeSlotDataSource implements SampleBasedDataSource {
	
	public SlidingTimeSlotDataSource(long start, long end) {
	}
	
	public void shift(long offset) {
		
	}

	@Override
	public Statistics get() {
		return null;
	}

	@Override
	public void newSample(long sample, long clock) {
	}

	@Override
	public void addSubSource(SampleBasedDataSource subSource) {
	}

}

@@ -41,16 +41,131 @@
 gment;%0A%0A
+import java.util.SortedMap;%0Aimport java.util.TreeMap;%0A%0Aimport cz.cuni.mff.d3s.spl.core.data.PrecomputedStatistics;%0A
 import c
@@ -345,309 +345,1611 @@
  %7B%0A%09
-%0A%09public SlidingTimeSlotDataSource(long start, long end) %7B%0A%09%7D%0A%09%0A%09public void shift(long offset) %7B%0A%09%09%0A%09%7D%0A%0A%09@Override%0A%09public Statistics get() %7B%0A%09%09return null;%0A%09%7D%0A%0A%09@Override%0A%09public void newSample(long sample, long clock) %7B%0A%09%7D%0A%0A%09@Override%0A%09public void addSubSource(SampleBasedDataSource subSource) %7B%0A%09%7D%0A
+private static class TickRecords %7B%0A%09%09public long sampleCount = 0;%0A%09%09public double sampleSum = 0.0;%0A%09%09%0A%09%09public TickRecords() %7B%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09public void newSample(long sample) %7B%0A%09%09%09sampleCount++;%0A%09%09%09sampleSum += (double) sample;%0A%09%09%7D%0A%09%7D%0A%09%0A%09private SortedMap%3CLong, TickRecords%3E samples = new TreeMap%3C%3E();%0A%09private long clockStart = Long.MIN_VALUE;%0A%09private long clockEnd = Long.MAX_VALUE;%0A%09%0A%09public SlidingTimeSlotDataSource(long start, long end) %7B%0A%09%09if (start %3E end) %7B%0A%09%09%09throw new IllegalArgumentException(%22It is forbidden to have %60from %3E end'.%22);%0A%09%09%7D%0A%09%09clockStart = start;%0A%09%09clockEnd = end;%0A%09%7D%0A%09%0A%09public void shift(long offset) %7B%0A%09%09clockStart += offset;%0A%09%09clockEnd += offset;%0A%09%09samples = samples.subMap(clockStart, clockEnd);%0A%09%7D%0A%0A%09@Override%0A%09public Statistics get() %7B%0A%09%09double totalSum = 0;%0A%09%09long sampleCount = 0;%0A%09%09for (TickRecords rec : samples.values()) %7B%0A%09%09%09%09totalSum += rec.sampleSum;%0A%09%09%09%09sampleCount += rec.sampleCount;%0A%09%09%7D%0A%09%09if (sampleCount == 0) %7B%0A%09%09%09return PrecomputedStatistics.empty;%0A%09%09%7D else %7B%0A%09%09%09return PrecomputedStatistics.create(totalSum / (double) sampleCount, sampleCount);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void newSample(long sample, long clock) %7B%0A%09%09if ((clock %3C clockStart) %7C%7C (clock %3E= clockEnd)) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%0A%09%09getTickRecord(clock).newSample(sample);%0A%09%7D%0A%0A%09@Override%0A%09public void addSubSource(SampleBasedDataSource subSource) %7B%0A%09%09throw new UnsupportedOperationException(%22Not implemented.%22);%0A%09%7D%0A%09%0A%09private TickRecords getTickRecord(long clock) %7B%0A%09%09TickRecords rec = samples.get(clock);%0A%09%09if (rec == null) %7B%0A%09%09%09rec = new TickRecords();%0A%09%09%09samples.put(clock, rec);%0A%09%09%7D%0A%09%09return rec;%0A%09%7D
 %0A%7D%0A

Implement SlidingTimeSlotDataSource