}
@SuppressWarnings("unchecked")
@Override
public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) {
ComputedValue inputValue = null;
DoubleTimeSeries<LocalDate> exchangeRates = null;
Double exchangeRate = null;
for (final ComputedValue input : inputs.getAllValues()) {
if (ValueRequirementNames.HISTORICAL_FX_TIME_SERIES.equals(input.getSpecification().getValueName())) {
if (input.getValue() instanceof Double) {
// Note: The rate might be a DOUBLE if the matrix being used has a hard coded constant in it. Improbable for a time-series conversion, but possible.
exchangeRate = (Double) input.getValue();
} else if (input.getValue() instanceof DoubleTimeSeries) {
// TODO: Note the unchecked cast. We'll either get a zero intersection and empty result if the values aren't the same type or a class cast exception.
exchangeRates = (DoubleTimeSeries) input.getValue();
} else {
return null;
}
} else {
inputValue = input;
}
}
if (inputValue == null) {
return null;
}
final ValueRequirement desiredValue = desiredValues.iterator().next();
final String outputCurrency = desiredValue.getConstraint(ValuePropertyNames.CURRENCY);
final String inputCurrency = inputValue.getSpecification().getProperty(ValuePropertyNames.CURRENCY);
if (outputCurrency.equals(inputCurrency)) {
// Don't think this should happen
return Collections.singleton(inputValue);
} else {
s_logger.debug("Converting from {} to {}", inputCurrency, outputCurrency);
final Object converted;
if (exchangeRates != null) {
converted = convertValue(inputValue, desiredValue, exchangeRates);
} else if (exchangeRate != null) {
converted = convertValue(inputValue, desiredValue, exchangeRate);
} else {
return null;
}
if (converted != null) {
return Collections.singleton(new ComputedValue(new ValueSpecification(desiredValue.getValueName(), target.toSpecification(), desiredValue.getConstraints()), converted));
} else {
return null;
}
}
}