class CouponIborAverageFixingDatesCompoundingDiscountingMethod {

  public MultipleCurrencyMulticurveSensitivity presentValueCurveSensitivity(final CouponIborAverageFixingDatesCompounding coupon, final MulticurveProviderInterface multicurve) {
    ArgumentChecker.notNull(coupon, "Coupon");
    ArgumentChecker.notNull(multicurve, "Curves");
    final int nPeriods = coupon.getFixingTime().length;
    final int[] nDates = new int[nPeriods];
    final double[] fctr = new double[nPeriods];
    double payOff = coupon.getInvestmentFactor();
    for (int i = 0; i < nPeriods; ++i) {
      double forward = ((i == 0) ? coupon.getAmountAccrued() : 0.0);
      nDates[i] = coupon.getFixingTime()[i].length;
      for (int j = 0; j < nDates[i]; ++j) {
        final double forward1 = multicurve.getSimplyCompoundForwardRate(coupon.getIndex(), coupon.getFixingPeriodStartTime()[i][j], coupon.getFixingPeriodEndTime()[i][j],
            coupon.getFixingPeriodAccrualFactor()[i][j]);
        forward += coupon.getWeight()[i][j] * forward1;
      }
      fctr[i] = (1.0 + forward * coupon.getPaymentAccrualFactors()[i]);
      payOff *= fctr[i];
    }
    final double df = multicurve.getDiscountFactor(coupon.getCurrency(), coupon.getPaymentTime());
    // Backward sweep.
    final double payOffBar = coupon.getNotional() * df;
    final double dfBar = coupon.getNotional() * (payOff - 1.0);
    final double[][] forwardBars = new double[nPeriods][];
    for (int i = 0; i < nPeriods; ++i) {
      forwardBars[i] = new double[nDates[i]];
      for (int j = 0; j < nDates[i]; ++j) {
        forwardBars[i][j] = coupon.getWeight()[i][j] * coupon.getPaymentAccrualFactors()[i] * payOff / fctr[i] * payOffBar;
      }
    }
    final Map<String, List<DoublesPair>> mapDsc = new HashMap<>();
    final List<DoublesPair> listDiscounting = new ArrayList<>();
    listDiscounting.add(DoublesPair.of(coupon.getPaymentTime(), -coupon.getPaymentTime() * df * dfBar));
    mapDsc.put(multicurve.getName(coupon.getCurrency()), listDiscounting);
    final Map<String, List<ForwardSensitivity>> mapFwd = new HashMap<>();
    final List<ForwardSensitivity> listForward = new ArrayList<>();
    for (int i = 0; i < nPeriods; ++i) {
      for (int j = 0; j < nDates[i]; ++j) {
        listForward.add(new SimplyCompoundedForwardSensitivity(coupon.getFixingPeriodStartTime()[i][j],
            coupon.getFixingPeriodEndTime()[i][j], coupon.getFixingPeriodAccrualFactor()[i][j], forwardBars[i][j]));
      }
    }
    mapFwd.put(multicurve.getName(coupon.getIndex()), listForward);
    return MultipleCurrencyMulticurveSensitivity.of(coupon.getCurrency(), MulticurveSensitivity.of(mapDsc, mapFwd));
  }

  private  CouponIborAverageFixingDatesCompoundingDiscountingMethod();

}