/**
 * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.engine.view.worker;

import java.util.HashMap;
import java.util.Map;

import org.threeten.bp.Instant;

import com.opengamma.engine.depgraph.DependencyNode;
import com.opengamma.engine.depgraph.impl.RootDiscardingSubgrapher;
import com.opengamma.engine.function.CompiledFunctionDefinition;
import com.opengamma.engine.function.resolver.CompiledFunctionResolver;

/**
 * Filters a dependency graph to exclude any nodes that use functions that aren't valid for the valuation time.
 */
/* package */final class InvalidFunctionDependencyNodeFilter extends RootDiscardingSubgrapher {

  private final CompiledFunctionResolver _functions;
  private final Instant _valuationTime;
  private final Map<String, Boolean> _expired = new HashMap<String, Boolean>();

  public InvalidFunctionDependencyNodeFilter(final CompiledFunctionResolver functions, final Instant valuationTime) {
    _functions = functions;
    _valuationTime = valuationTime;
  }

  // RootDiscardingSubgrapher

  @Override
  protected boolean acceptNode(final DependencyNode node) {
    final String functionId = node.getFunction().getFunctionId();
    final Boolean expired = _expired.get(functionId);
    if (expired == null) {
      final CompiledFunctionDefinition cfd = _functions.getFunction(functionId);
      Instant t = cfd.getEarliestInvocationTime();
      if (t != null) {
        if (_valuationTime.isBefore(t)) {
          _expired.put(functionId, Boolean.FALSE);
          return false;
        }
      }
      t = cfd.getLatestInvocationTime();
      if (t != null) {
        if (_valuationTime.isAfter(t)) {
          _expired.put(functionId, Boolean.FALSE);
          return false;
        }
      }
      _expired.put(functionId, Boolean.TRUE);
      return true;
    }
    return expired.booleanValue();
  }

}

@@ -1421,24 +1421,181 @@
 unctionId);%0A
+      if (cfd == null) %7B%0A        // Function no longer in compiled repository%0A        _expired.put(functionId, Boolean.FALSE);%0A        return false;%0A      %7D%0A
       Instan
@@ -1625,32 +1625,32 @@
 vocationTime();%0A
-
       if (t != n
@@ -1694,24 +1694,58 @@
 efore(t)) %7B%0A
+          // Function has expired%0A
           _e
@@ -1926,24 +1926,58 @@
 After(t)) %7B%0A
+          // Function has expired%0A
           _e
@@ -2045,32 +2045,32 @@
 ;%0A        %7D%0A
-
       %7D%0A
       _expir
@@ -2053,24 +2053,57 @@
   %7D%0A      %7D%0A
+      // Function is still valid%0A
       _expir

Handle invalid function identifiers