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

import java.util.SortedMap;
import java.util.TreeMap;

import cz.cuni.mff.d3s.spl.core.data.SampleStorage;

/** Stores measured samples in memory. */
public class InMemorySamples implements SampleStorage {

	private SortedMap<Long, Long> data = new TreeMap<>();
	private String id = "";
	
	public InMemorySamples(String id) {
		this.id = id;
	}

	@Override
	public synchronized void add(long sample, long clock) {
		data.put(clock, sample);
	}

	@Override
	public synchronized SortedMap<Long, Long> get(long startTime, long endTime) {
		return new TreeMap<>(data.subMap(startTime, endTime));
	}
	
	@Override
	public String toString() {
		return String.format("Samples[%s](%d)", id, data.size());
	}

	@Override
	public boolean equals(Object otherObj) {
		if (this == otherObj) {
			return true;
		}
		
		if (!(otherObj instanceof InMemorySamples)) {
			return false;
		}
		
		InMemorySamples other = (InMemorySamples) otherObj;
		
		return this.id.equals(other.id);
	}

}

@@ -62,40 +62,95 @@
 til.
-SortedMap;%0Aimport java.util.Tree
+Collections;%0Aimport java.util.SortedMap;%0Aimport java.util.concurrent.ConcurrentSkipList
 Map;
@@ -341,28 +341,42 @@
  data = new 
-Tree
+ConcurrentSkipList
 Map%3C%3E();%0A%09pr
@@ -397,16 +397,39 @@
 d = %22%22;%0A
+%09private int hash = 0;%0A
 %09%0A%09publi
@@ -475,16 +475,45 @@
 d = id;%0A
+%09%09this.hash = id.hashCode();%0A
 %09%7D%0A%0A%09@Ov
@@ -527,29 +527,16 @@
 %09public 
-synchronized 
 void add
@@ -617,21 +617,8 @@
 lic 
-synchronized 
 Sort
@@ -684,21 +684,41 @@
 urn 
-new Tree
+Collections.unmodifiableSorted
 Map
-%3C%3E
 (dat
@@ -850,24 +850,80 @@
 .size());%0A%09%7D
+%0A%09%0A%09@Override%0A%09public int hashCode() %7B%0A%09%09return hash;%0A%09%7D
 %0A%0A%09@Override

Use implicitly synchronized map