CurrencyMatrixLookupFunction extends AbstractFunction.NonCompiledInvoker { @Override public Set<ValueRequirement> getRequirements(final FunctionCompilationContext context, final ComputationTarget target, final ValueRequirement desiredValue) { final CurrencyPair currencies = (CurrencyPair) target.getValue(); final ValueProperties.Builder constraints = ValueProperties .with(AbstractCurrencyMatrixSourcingFunction.SOURCE_CURRENCY_PROPERTY, currencies.getCounter().getCode()) .with(AbstractCurrencyMatrixSourcingFunction.TARGET_CURRENCY_PROPERTY, currencies.getBase().getCode()); ExternalIdBundle matrixIdentifiers = null; if (desiredValue.getConstraints().getProperties() != null) { for (String constraintName : desiredValue.getConstraints().getProperties()) { if (ValuePropertyNames.FUNCTION.equals(constraintName) || constraintName.startsWith(ValuePropertyNames.OUTPUT_RESERVED_PREFIX)) { continue; } final Set<String> values = desiredValue.getConstraints().getValues(constraintName); if (CURRENCY_MATRIX_NAME_PROPERTY.equals(constraintName)) { if (values.isEmpty()) { matrixIdentifiers = ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, getDefaultCurrencyMatrixName()).toBundle(); } else { if (values.size() == 1) { matrixIdentifiers = ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, values.iterator().next()).toBundle(); } else { final Collection<ExternalId> identifiers = new ArrayList<ExternalId>(values.size()); for (String matrixName : values) { identifiers.add(ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, matrixName)); } matrixIdentifiers = ExternalIdBundle.of(identifiers); } } } else { if (values.isEmpty()) { constraints.withAny(constraintName); } else { constraints.with(constraintName, values); } if (desiredValue.getConstraints().isOptional(constraintName)) { constraints.withOptional(constraintName); } } } } if (matrixIdentifiers == null) { matrixIdentifiers = ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, getDefaultCurrencyMatrixName()).toBundle(); } return Collections.singleton(new ValueRequirement(desiredValue.getValueName(), new ComputationTargetRequirement(CurrencyMatrixResolver.TYPE, matrixIdentifiers), constraints.get())); } CurrencyMatrixLookupFunction(final String defaultCurrencyMatrixName);  CurrencyMatrixLookupFunction(final String[] params); @Override ComputationTargetType getTargetType(); @Override Set<ValueSpecification> getResults(final FunctionCompilationContext context, final ComputationTarget target); @Override Set<ValueRequirement> getRequirements(final FunctionCompilationContext context, final ComputationTarget target, final ValueRequirement desiredValue); @Override Set<ValueSpecification> getResults(final FunctionCompilationContext context, final ComputationTarget target, final Map<ValueSpecification, ValueRequirement> inputs); @Override Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues); int getPriority();  }
@Test public void testGetRequirements() { ComputationTarget target = new ComputationTarget(CurrencyPair.TYPE, CurrencyPair.parse("GBP/USD")); Set<ValueRequirement> requirements = _function.getRequirements(_functionCompilationContext, target, new ValueRequirement(ValueRequirementNames.SPOT_RATE, target.toSpecification())); assertEquals(Iterables.getOnlyElement(requirements), new ValueRequirement(ValueRequirementNames.SPOT_RATE, new ComputationTargetRequirement(CurrencyMatrixResolver.TYPE, ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, "Foo")), ValueProperties.with(AbstractCurrencyMatrixSourcingFunction.SOURCE_CURRENCY_PROPERTY, "USD").with(AbstractCurrencyMatrixSourcingFunction.TARGET_CURRENCY_PROPERTY, "GBP").get())); requirements = _function.getRequirements(_functionCompilationContext, target, new ValueRequirement(ValueRequirementNames.SPOT_RATE, target.toSpecification(), ValueProperties.with(CurrencyMatrixLookupFunction.CURRENCY_MATRIX_NAME_PROPERTY, "Bar").get())); assertEquals(Iterables.getOnlyElement(requirements), new ValueRequirement(ValueRequirementNames.SPOT_RATE, new ComputationTargetRequirement(CurrencyMatrixResolver.TYPE, ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, "Bar")), ValueProperties.with(AbstractCurrencyMatrixSourcingFunction.SOURCE_CURRENCY_PROPERTY, "USD").with(AbstractCurrencyMatrixSourcingFunction.TARGET_CURRENCY_PROPERTY, "GBP").get())); requirements = _function.getRequirements(_functionCompilationContext, target, new ValueRequirement(ValueRequirementNames.SPOT_RATE, target.toSpecification(), ValueProperties.with(CurrencyMatrixLookupFunction.CURRENCY_MATRIX_NAME_PROPERTY, "Bar", "Foo").get())); assertEquals( Iterables.getOnlyElement(requirements), new ValueRequirement(ValueRequirementNames.SPOT_RATE, new ComputationTargetRequirement(CurrencyMatrixResolver.TYPE, ExternalIdBundle.of( ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, "Foo"), ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, "Bar"))), ValueProperties.with(AbstractCurrencyMatrixSourcingFunction.SOURCE_CURRENCY_PROPERTY, "USD").with(AbstractCurrencyMatrixSourcingFunction.TARGET_CURRENCY_PROPERTY, "GBP").get())); requirements = _function.getRequirements(_functionCompilationContext, target, new ValueRequirement(ValueRequirementNames.SPOT_RATE, target.toSpecification(), ValueProperties.withAny(CurrencyMatrixLookupFunction.CURRENCY_MATRIX_NAME_PROPERTY).get())); assertEquals(Iterables.getOnlyElement(requirements), new ValueRequirement(ValueRequirementNames.SPOT_RATE, new ComputationTargetRequirement(CurrencyMatrixResolver.TYPE, ExternalId.of(CurrencyMatrixResolver.IDENTIFIER_SCHEME, "Foo")), ValueProperties.with(AbstractCurrencyMatrixSourcingFunction.SOURCE_CURRENCY_PROPERTY, "USD").with(AbstractCurrencyMatrixSourcingFunction.TARGET_CURRENCY_PROPERTY, "GBP").get())); }