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

import com.opengamma.core.position.Position;
import com.opengamma.core.security.Security;
import com.opengamma.financial.security.FinancialSecurity;
import com.opengamma.financial.security.FinancialSecurityVisitor;
import com.opengamma.financial.security.bond.BondSecurity;
import com.opengamma.financial.security.cash.CashSecurity;
import com.opengamma.financial.security.equity.EquitySecurity;
import com.opengamma.financial.security.fra.FRASecurity;
import com.opengamma.financial.security.future.FutureSecurity;
import com.opengamma.financial.security.option.OptionSecurity;
import com.opengamma.financial.security.swap.SwapSecurity;
import com.opengamma.util.money.Currency;

/**
 * Function to classify positions by Currency.
 *
 */
public class CurrencyAggregationFunction implements AggregationFunction<Currency> {
  private static final String NAME = "Currency";
  
  @Override
  public Currency classifyPosition(Position position) {
    Security security = position.getSecurity();
    if (security instanceof FinancialSecurity) {
      FinancialSecurity finSec = (FinancialSecurity) security;
      return finSec.accept(new FinancialSecurityVisitor<Currency>() {

        @Override
        public Currency visitBondSecurity(BondSecurity security) {
          return security.getCurrency();
        }

        @Override
        public Currency visitCashSecurity(CashSecurity security) {
          return null;
        }

        @Override
        public Currency visitEquitySecurity(EquitySecurity security) {
          return security.getCurrency();
        }

        @Override
        public Currency visitFRASecurity(FRASecurity security) {
          return null;
        }

        @Override
        public Currency visitFutureSecurity(FutureSecurity security) {
          return security.getCurrency();
        }

        @Override
        public Currency visitOptionSecurity(OptionSecurity security) {
          return security.getCurrency();
        }

        @Override
        public Currency visitSwapSecurity(SwapSecurity security) {
          return null;
        }

      });
    } else {
      return null;
    }
  }

  public String getName() {
    return NAME;
  }
}

@@ -247,2078 +247,430 @@
 mma.
-core.security.Security;%0Aimport com.opengamma.financial.security.FinancialSecurity;%0Aimport com.opengamma.financial.security.FinancialSecurityVisitor;%0Aimport com.opengamma.financial.security.bond.BondSecurity;%0Aimport com.opengamma.financial.security.cash.CashSecurity;%0Aimport com.opengamma.financial.security.equity.EquitySecurity;%0Aimport com.opengamma.financial.security.fra.FRASecurity;%0Aimport com.opengamma.financial.security.future.FutureSecurity;%0Aimport com.opengamma.financial.security.option.OptionSecurity;%0Aimport com.opengamma.financial.security.swap.SwapSecurity;%0Aimport com.opengamma.util.money.Currency;%0A%0A/**%0A * Function to classify positions by Currency.%0A *%0A */%0Apublic class CurrencyAggregationFunction implements AggregationFunction%3CCurrency%3E %7B%0A  private static final String NAME = %22Currency%22;%0A  %0A  @Override%0A  public Currency classifyPosition(Position position) %7B%0A    Security security = position.getSecurity();%0A    if (security instanceof FinancialSecurity) %7B%0A      FinancialSecurity finSec = (FinancialSecurity) security;%0A      return finSec.accept(new FinancialSecurityVisitor%3CCurrency%3E() %7B%0A%0A        @Override%0A        public Currency visitBondSecurity(BondSecurity security) %7B%0A          return security.getCurrency();%0A        %7D%0A%0A        @Override%0A        public Currency visitCashSecurity(CashSecurity security) %7B%0A          return null;%0A        %7D%0A%0A        @Override%0A        public Currency visitEquitySecurity(EquitySecurity security) %7B%0A          return security.getCurrency();%0A        %7D%0A%0A        @Override%0A        public Currency visitFRASecurity(FRASecurity security) %7B%0A          return null;%0A        %7D%0A%0A        @Override%0A        public Currency visitFutureSecurity(FutureSecurity security) %7B%0A          return security.getCurrency();%0A        %7D%0A%0A        @Override%0A        public Currency visitOptionSecurity(OptionSecurity security) %7B%0A          return security.getCurrency();%0A        %7D%0A%0A        @Override%0A        public Currency visitSwapSecurity(SwapSecurity security) %7B%0A          return null;%0A        %7D%0A%0A      %7D);%0A    %7D else %7B%0A      return null;%0A    %7D
+financial.security.FinancialSecurityUtils;%0Aimport com.opengamma.util.money.Currency;%0A%0A/**%0A * Function to classify positions by Currency.%0A *%0A */%0Apublic class CurrencyAggregationFunction implements AggregationFunction%3CCurrency%3E %7B%0A  private static final String NAME = %22Currency%22;%0A  %0A  @Override%0A  public Currency classifyPosition(Position position) %7B%0A    return FinancialSecurityUtils.getCurrencyUnit(position.getSecurity());
 %0A  %7D

Use FinancialSecurityUtils instead of code duplication to to the currency lookup.