package org.arbeitspferde.groningen.historydatastore;

import com.google.common.collect.Lists;

import org.arbeitspferde.groningen.HistoryDatastore;
import org.arbeitspferde.groningen.PipelineHistoryState;
import org.arbeitspferde.groningen.PipelineId;
import org.joda.time.Instant;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Memory-only implementation of {@link HistoryDatastore}.
 */
public class MemoryHistoryDatastore implements HistoryDatastore {

  private Map<PipelineId, List<PipelineHistoryState>> data =
      new HashMap<PipelineId, List<PipelineHistoryState>>();
  
  @Override
  public void writeState(PipelineHistoryState state) {
    List<PipelineHistoryState> states = data.get(state.pipelineId());
    if (states == null) {
      states = new ArrayList<PipelineHistoryState>();
      data.put(state.pipelineId(), states);
    }
    states.add(state);
    Collections.sort(states, new Comparator<PipelineHistoryState>() {
      @Override
      public int compare(PipelineHistoryState s1, PipelineHistoryState s2) {
        return s1.endTimestamp().compareTo(s2.endTimestamp());
      }
    });
  }

  @Override
  public List<PipelineId> listPipelinesIds() {
    return Lists.newArrayList(data.keySet());
  }

  @Override
  public List<PipelineHistoryState> getStatesForPipelineId(PipelineId pipelineId) {
    List<PipelineHistoryState> states = data.get(pipelineId);
    if (states == null) {
      states = new ArrayList<PipelineHistoryState>();
    }
    return states;
  }

  @Override
  public List<PipelineHistoryState> getStatesForPipelineId(
      PipelineId pipelineId, Instant afterTimestamp) {
    List<PipelineHistoryState> states = data.get(pipelineId);
    List<PipelineHistoryState> results = new ArrayList<PipelineHistoryState>();
    if (states != null) {
      for (PipelineHistoryState s : states) {
        if (s.endTimestamp().isAfter(afterTimestamp)) {
          results.add(s);
        }
      }
    }
    return results;
  }
}

@@ -692,18 +692,16 @@
 te%3E%3E();%0A
-  
 %0A  @Over
@@ -1342,34 +1342,23 @@
 %0A  %7D%0A%0A  
-@Override%0A  public
+private
  List%3CPi
@@ -1377,38 +1377,26 @@
 yState%3E 
-get
+read
 States
-ForPipelineId
 (Pipelin
@@ -1581,24 +1581,190 @@
 tates;%0A  %7D%0A%0A
+  @Override%0A  public List%3CPipelineHistoryState%3E getStatesForPipelineId(PipelineId pipelineId) %7B%0A    return Collections.unmodifiableList(readStates(pipelineId));%0A  %7D%0A%0A
   @Override%0A
@@ -1882,70 +1882,8 @@
 ) %7B%0A
-    List%3CPipelineHistoryState%3E states = data.get(pipelineId);%0A
     
@@ -1966,72 +1966,64 @@
     
-if (states != null) %7B%0A      for (PipelineHistoryState s : states
+for (PipelineHistoryState state : readStates(pipelineId)
 ) %7B%0A
@@ -2028,23 +2028,25 @@
 %7B%0A      
- 
- 
 if (s
+tate
 .endTime
@@ -2088,18 +2088,16 @@
         
-  
 results.
@@ -2105,21 +2105,23 @@
 dd(s
+tate
 );%0A
-  
       %7D%0A
     
@@ -2116,24 +2116,16 @@
       %7D%0A
-      %7D%0A
     %7D%0A  
@@ -2133,23 +2133,53 @@
  return 
+Collections.unmodifiableList(
 results
+)
 ;%0A  %7D%0A%7D%0A

Make behavior of getStatesForPipelineId more consistent