com.opengamma.financial.analytics.UnitPositionScalingFunction.java Source code

Java tutorial

Introduction

Here is the source code for com.opengamma.financial.analytics.UnitPositionScalingFunction.java

Source

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

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.core.position.Position;
import com.opengamma.core.security.Security;
import com.opengamma.engine.ComputationTarget;
import com.opengamma.engine.ComputationTargetType;
import com.opengamma.engine.function.AbstractFunction;
import com.opengamma.engine.function.FunctionCompilationContext;
import com.opengamma.engine.function.FunctionExecutionContext;
import com.opengamma.engine.function.FunctionInputs;
import com.opengamma.engine.value.ComputedValue;
import com.opengamma.engine.value.ValueProperties;
import com.opengamma.engine.value.ValuePropertyNames;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueSpecification;

/**
 * 
 */
public class UnitPositionScalingFunction extends AbstractFunction.NonCompiledInvoker {

    private final String _requirementName;

    public UnitPositionScalingFunction(final String requirementName) {
        Validate.notNull(requirementName, "requirement name");
        _requirementName = requirementName;
    }

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

    @Override
    public ComputationTargetType getTargetType() {
        return ComputationTargetType.POSITION;
    }

    @Override
    public boolean canApplyTo(final FunctionCompilationContext context, final ComputationTarget target) {
        return true;
    }

    @Override
    public Set<ValueSpecification> getResults(final FunctionCompilationContext context,
            final ComputationTarget target) {
        return Sets.newHashSet(
                new ValueSpecification(_requirementName, target.toSpecification(), ValueProperties.all()));
    }

    @Override
    public Set<ValueRequirement> getRequirements(final FunctionCompilationContext context,
            final ComputationTarget target, final ValueRequirement desiredValue) {
        final Position position = target.getPosition();
        final Security security = position.getSecurity();
        final ValueProperties constraints = desiredValue.getConstraints().withoutAny(ValuePropertyNames.FUNCTION);
        final ValueRequirement requirement = new ValueRequirement(_requirementName, ComputationTargetType.SECURITY,
                security.getUniqueId(), constraints);
        return Collections.singleton(requirement);
    }

    private ValueProperties getResultProperties(final ValueSpecification input) {
        return input.getProperties().copy().withoutAny(ValuePropertyNames.FUNCTION)
                .with(ValuePropertyNames.FUNCTION, getUniqueId()).get();
    }

    @Override
    public Set<ValueSpecification> getResults(final FunctionCompilationContext context,
            final ComputationTarget target, final Map<ValueSpecification, ValueRequirement> inputs) {
        final ValueSpecification specification = new ValueSpecification(_requirementName, target.toSpecification(),
                getResultProperties(inputs.keySet().iterator().next()));
        return Collections.singleton(specification);
    }

    @Override
    public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs,
            final ComputationTarget target, final Set<ValueRequirement> desiredValues) {
        final ComputedValue value = inputs.getAllValues().iterator().next();
        final ValueSpecification specification = new ValueSpecification(
                new ValueRequirement(_requirementName, target.toSpecification()),
                getResultProperties(value.getSpecification()));
        return Sets.newHashSet(new ComputedValue(specification, value.getValue()));
    }

}