/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.view;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.opengamma.engine.ComputationTargetSpecification;
import com.opengamma.engine.depgraph.DependencyGraphBuilder;
import com.opengamma.engine.position.PortfolioNode;
import com.opengamma.engine.position.Position;
import com.opengamma.engine.value.ComputedValue;

/**
 * A simple implementation of the calculation result model.
 */
public class ViewCalculationResultModelImpl implements Serializable,
    ViewCalculationResultModel {
  private DependencyGraphBuilder _dependencyGraphModel;
  private final Map<ComputationTargetSpecification, Map<String, ComputedValue>> _values =
    new HashMap<ComputationTargetSpecification, Map<String, ComputedValue>>();

  public void setDependencyGraphModel(DependencyGraphBuilder dependencyGraphModel) {
    _dependencyGraphModel = dependencyGraphModel;
  }
  
  public DependencyGraphBuilder getDependencyGraphModel() {
    return _dependencyGraphModel;
  }

  @Override
  public Collection<ComputationTargetSpecification> getAllTargets() {
    return Collections.unmodifiableSet(_values.keySet());
  }
  
  @Override
  public Map<String, ComputedValue> getValues(
      ComputationTargetSpecification target) {
    return _values.get(target);
  }

  protected void recursiveAddPortfolio(PortfolioNode node) {
    for (Position position : node.getPositions()) {
      ComputationTargetSpecification targetSpec = new ComputationTargetSpecification(position);
      if (!_values.containsKey(targetSpec)) {
        _values.put(targetSpec, new HashMap<String, ComputedValue>());
      }
    }
    ComputationTargetSpecification targetSpec = new ComputationTargetSpecification(node);
    if (!_values.containsKey(targetSpec)) {
      _values.put(targetSpec, new HashMap<String, ComputedValue>());
    }
    for (PortfolioNode child : node.getChildNodes()) {
      recursiveAddPortfolio(child);
    }
  }

  public void addValue(ComputedValue value) {
    ComputationTargetSpecification targetSpec = value.getSpecification().getRequirementSpecification().getTargetSpecification();
    if (!(_values.containsKey(targetSpec))) {
      throw new IllegalArgumentException("Target spec " + targetSpec + " not reachable from initialization in recursiveAddPortfolio");
    }
    _values.get(targetSpec).put(value.getSpecification().getRequirementSpecification().getValueName(), value);
  }

}

@@ -2106,24 +2106,70 @@
 );%0A    %7D%0A  %7D
+%0A  %0A  // Add in a .addPrimitive() method here.
 %0A%0A  public v
@@ -2341,218 +2341,868 @@
     
-if (!(_values.containsKey(targetSpec))) %7B%0A      throw new IllegalArgumentException(%22Target spec %22 + targetSpec + %22 not reachable from initialization in recursiveAddPortfolio%22);%0A    %7D%0A    _values.get(targetSpec)
+// TODO kirk 2010-07-05 -- Not sure if this is the right approach.%0A    // The check was based on recursiveAddPortfolio(), and not having to make sure that we%0A    // don't have the inner Map%3C%3E already. This needs to be reviewed and the design%0A    // of this class potentially changed.%0A    /*if (!(_values.containsKey(targetSpec))) %7B%0A      throw new IllegalArgumentException(%22Target spec %22 + targetSpec + %22 not reachable from initialization in recursiveAddPortfolio%22);%0A    %7D%0A    _values.get(targetSpec).put(value.getSpecification().getRequirementSpecification().getValueName(), value);%0A    */%0A    Map%3CString, ComputedValue%3E targetSpecificValues = _values.get(targetSpec);%0A    if (targetSpecificValues == null) %7B%0A      targetSpecificValues = new HashMap%3CString, ComputedValue%3E();%0A      _values.put(targetSpec, targetSpecificValues);%0A    %7D%0A    targetSpecificValues
 .put

Support security and primitive targets