/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.livedata.server;

import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeField;
import org.fudgemsg.FudgeMsg;
import org.fudgemsg.MutableFudgeMsg;

/**
 * A store of historical message field values.
 * <p>
 * At the moment, the field history only stores the last value.
 * This class could be extended in future to store the last N 
 * values, values at a certain interval for the last N minutes, etc. 
 */
public class FieldHistoryStore {
  
  private final FudgeContext _context = FudgeContext.GLOBAL_DEFAULT;
  private final MutableFudgeMsg _lastKnownValues;
  
  public FieldHistoryStore() {
    _lastKnownValues = _context.newMessage();
  }
  
  public FieldHistoryStore(FudgeMsg history) {
    _lastKnownValues = _context.newMessage(history);   
  }
  
  public FieldHistoryStore(FieldHistoryStore original) {
    _lastKnownValues = _context.newMessage(original._lastKnownValues);   
  }
  
  public synchronized void liveDataReceived(FudgeMsg msg) {
    for (FudgeField field : msg.getAllFields()) {
      _lastKnownValues.remove(field.getName());
      _lastKnownValues.add(field);
    }
  }
  
  public void clear() {
    _lastKnownValues.clear();
  }
  
  public boolean isEmpty() {
    return _lastKnownValues.isEmpty();
  }
  
  public synchronized FudgeMsg getLastKnownValues() {
    return _lastKnownValues;
  }

}

@@ -171,16 +171,93 @@
 erver;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0A%0A
 import o
@@ -380,16 +380,60 @@
 udgeMsg;
+%0Aimport org.fudgemsg.UnmodifiableFudgeField;
 %0A%0A/**%0A *
@@ -807,18 +807,35 @@
  final M
-ut
+ap%3CString, Unmodifi
 ableFudg
@@ -835,19 +835,22 @@
 bleFudge
-Msg
+Field%3E
  _lastKn
@@ -917,35 +917,59 @@
 alues = 
-_context.newMessage
+new HashMap%3CString, UnmodifiableFudgeField%3E
 ();%0A  %7D%0A
@@ -1026,46 +1026,36 @@
     
-_lastKnownValues = _context.newMessage
+this();%0A    liveDataReceived
 (his
@@ -1060,19 +1060,16 @@
 istory);
-   
 %0A  %7D%0A  %0A
@@ -1133,46 +1133,12 @@
     
-_lastKnownValues = _context.newMessage
+this
 (ori
@@ -1143,18 +1143,20 @@
 riginal.
-_l
+getL
 astKnown
@@ -1161,16 +1161,18 @@
 wnValues
+()
 );   %0A  
@@ -1271,23 +1271,8 @@
  msg
-.getAllFields()
 ) %7B%0A
@@ -1298,14 +1298,11 @@
 ues.
-remove
+put
 (fie
@@ -1317,45 +1317,77 @@
 me()
-);%0A      _lastKnownValues.add(field);
+, UnmodifiableFudgeField.of(field)); //NOTE: duplicates are discarded
 %0A   
@@ -1589,32 +1589,43 @@
 () %7B%0A    return 
+getMessage(
 _lastKnownValues
@@ -1628,13 +1628,314 @@
 lues
+)
 ;%0A  %7D%0A%0A
+  private FudgeMsg getMessage(Map%3CString, UnmodifiableFudgeField%3E lastKnownValues) %7B%0A    MutableFudgeMsg newMessage = _context.newMessage();%0A    for (Entry%3CString, UnmodifiableFudgeField%3E entry : _lastKnownValues.entrySet()) %7B%0A      newMessage.add(entry.getValue());%0A    %7D%0A    return newMessage;%0A  %7D%0A
 %7D%0A

Implement FieldHistoryStore using a Map, not a Msg, so that it's not O(n^2) - NOTE: This introduces a slighty semantic change in the case of repeated fields, but it was quite magic what happened with those anyway.