/**
 * Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.strata.function.rate.swap;

import com.opengamma.strata.basics.currency.CurrencyAmount;
import com.opengamma.strata.engine.calculations.CalculationRequirements;
import com.opengamma.strata.engine.calculations.function.EngineSingleFunction;
import com.opengamma.strata.engine.marketdata.CalculationMarketData;
import com.opengamma.strata.finance.rate.swap.NotionalSchedule;
import com.opengamma.strata.finance.rate.swap.RateCalculationSwapLeg;
import com.opengamma.strata.finance.rate.swap.SwapTrade;

/**
 * Returns the notional amount of a {@code SwapTrade}.
 */
public class SwapTradeNotionalFunction
    implements EngineSingleFunction<SwapTrade, CurrencyAmount> {
  // TODO: what is correct result?
  // which leg as they can differ?
  // what notional - current period, initial, final or max?

  @Override
  public CalculationRequirements requirements(SwapTrade target) {
    return CalculationRequirements.EMPTY;
  }

  @Override
  public CurrencyAmount execute(SwapTrade input, CalculationMarketData marketData) {
    NotionalSchedule notionalSchedule = getNotionalSchedule(input);
    return CurrencyAmount.of(notionalSchedule.getCurrency(), notionalSchedule.getAmount().getInitialValue());
  }
  
  private NotionalSchedule getNotionalSchedule(SwapTrade input) {
    return input.getProduct().getLegs().stream()
        .filter(RateCalculationSwapLeg.class::isInstance)
        .map(RateCalculationSwapLeg.class::cast)
        .findFirst()
        .orElseThrow(() -> new IllegalArgumentException("Unsupported swap"))
        .getNotionalSchedule();
  }

}

@@ -182,16 +182,111 @@
 .swap;%0A%0A
+import java.util.List;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.IntStream;%0A%0A
 import c
@@ -562,72 +562,8 @@
 ta;%0A
-import com.opengamma.strata.finance.rate.swap.NotionalSchedule;%0A
 impo
@@ -835,16 +835,26 @@
 pTrade, 
+List%3CList%3C
 Currency
@@ -860,16 +860,18 @@
 yAmount%3E
+%3E%3E
  %7B%0A  // 
@@ -1142,16 +1142,26 @@
  public 
+List%3CList%3C
 Currency
@@ -1166,16 +1166,18 @@
 cyAmount
+%3E%3E
  execute
@@ -1234,32 +1234,36 @@
 ) %7B%0A    
-NotionalSchedule
+List%3CCurrencyAmount%3E
  notiona
@@ -1263,24 +1263,16 @@
 notional
-Schedule
  = getNo
@@ -1277,24 +1277,16 @@
 Notional
-Schedule
 (input);
@@ -1301,140 +1301,228 @@
 urn 
-CurrencyAmount.of(notionalSchedule.getCurrency(), notionalSchedule.getAmount().getInitialValue());%0A  %7D%0A  %0A  private NotionalSchedule
+IntStream.range(0, marketData.getScenarioCount())%0A        .mapToObj(i -%3E notional)%0A        .collect(Collectors.toList());%0A  %7D%0A%0A  // Not a MultiCurrencyAmount as legs should be kept separate%0A  private List%3CCurrencyAmount%3E
  get
@@ -1529,24 +1529,16 @@
 Notional
-Schedule
 (SwapTra
@@ -1718,126 +1718,258 @@
    .
-findFirst()%0A        .orElseThrow(() -%3E new IllegalArgumentException(%22Unsupported swap%22))%0A        .getNotionalSchedule(
+map(l -%3E CurrencyAmount.of(l.getNotionalSchedule().getCurrency(), l.getNotionalSchedule().getAmount().getInitialValue()))%0A        .distinct() // if legs have the same notional then represent these as a single item%0A        .collect(Collectors.toList()
 );%0A 

Support cross-currency swaps