/** * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.core.position.impl; import java.util.Collection; import com.opengamma.core.position.PortfolioNode; import com.opengamma.core.position.Position; import com.opengamma.util.ArgumentChecker; /** * Implementation of {@code PortfolioNodeTraversalCallback} that collects values from a portfolio. * <p> * A {@link PortfolioMapperFunction} is used at each node in the portfolio, and the return value from its apply() * method is stored. Any null values generated by the mapper function are ignored. * * @param <T> the type that is collected * @param <V> the collection type into which the values are collected */ /*package*/ class MappingCallback<T, V extends Collection<T>> extends AbstractPortfolioNodeTraversalCallback { /** * The collected values */ private final V _values; /** * The mapper function which generates the values */ private final PortfolioMapperFunction<T> _fn; /** * Constructs an instance. * * @param fn the function that generates values for the portfolio's nodes, not null * @param resultCollection the collection instance to which the results will be added, not null */ public MappingCallback(final PortfolioMapperFunction<T> fn, final V resultCollection) { ArgumentChecker.notNull(fn, "fn"); ArgumentChecker.notNull(resultCollection, "resultCollection"); _fn = fn; _values = resultCollection; } @Override public void preOrderOperation(final PortfolioNode portfolioNode) { final T result = _fn.apply(portfolioNode); if (result != null) { _values.add(result); } } @Override public void preOrderOperation(final PortfolioNode parentNode, final Position position) { final T result = _fn.apply(parentNode, position); if (result != null) { _values.add(result); } } /** @return The values generated by the mapper function */ public V getValues() { return _values; } }