com.opengamma.financial.analytics.model.bond.BondPV01CountryCurveFunction.java Source code

Java tutorial

Introduction

Here is the source code for com.opengamma.financial.analytics.model.bond.BondPV01CountryCurveFunction.java

Source

/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.model.bond;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.Validate;

import com.google.common.collect.Sets;
import com.opengamma.engine.ComputationTarget;
import com.opengamma.engine.function.FunctionCompilationContext;
import com.opengamma.engine.target.ComputationTargetType;
import com.opengamma.engine.value.ValueProperties;
import com.opengamma.engine.value.ValuePropertyNames;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueRequirementNames;
import com.opengamma.engine.value.ValueSpecification;
import com.opengamma.financial.analytics.ircurve.YieldCurveFunction;

/**
 * 
 */
public class BondPV01CountryCurveFunction extends BondPV01Function {

    @Override
    public String getShortName() {
        return "BondPV01CountryCurveFunction";
    }

    @Override
    public Set<ValueRequirement> getRequirements(final FunctionCompilationContext context,
            final ComputationTarget target, final ValueRequirement desiredValue) {
        final String curveName = YieldCurveFunction.getCurveName(context, desiredValue);
        return Sets.newHashSet(new ValueRequirement(ValueRequirementNames.YIELD_CURVE,
                ComputationTargetType.PRIMITIVE, BondFunctionUtils.getCountryID(target),
                ValueProperties.with(ValuePropertyNames.CURVE, curveName).get()));
    }

    @Override
    public Set<ValueSpecification> getResults(final FunctionCompilationContext context,
            final ComputationTarget target) {
        return Collections.singleton(new ValueSpecification(ValueRequirementNames.PV01, target.toSpecification(),
                createValueProperties().with(ValuePropertyNames.COUNTRY, BondFunctionUtils.getCountryName(target))
                        .withAny(ValuePropertyNames.CURVE).get()));
    }

    @Override
    public Set<ValueSpecification> getResults(final FunctionCompilationContext context,
            final ComputationTarget target, final Map<ValueSpecification, ValueRequirement> inputs) {
        String curveName = null;
        for (ValueSpecification input : inputs.keySet()) {
            if (ValueRequirementNames.YIELD_CURVE.equals(input.getValueName())) {
                curveName = input.getProperty(ValuePropertyNames.CURVE);
                break;
            }
        }
        Validate.notNull(curveName, "curveName");
        return Collections.singleton(new ValueSpecification(ValueRequirementNames.PV01, target.toSpecification(),
                createValueProperties().with(ValuePropertyNames.COUNTRY, BondFunctionUtils.getCountryName(target))
                        .with(ValuePropertyNames.CURVE, curveName).get()));
    }
}