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

/**
 * Class containing utilities for bumping credit spread term structures by user defined methods and amounts
 */
public class CreditSpreadBumpers {

  // ----------------------------------------------------------------------------------------------------------------------------------------

  // TODO : Add error checkers for the input arguments

  // ----------------------------------------------------------------------------------------------------------------------------------------

  // Method to bump the credit spread term structure at every tenor point by a specified amount

  public double[] getBumpedCreditSpreads(final double[] marketSpreads, final double spreadBump, final SpreadBumpType spreadBumpType) {

    double[] bumpedCreditSpreads = new double[marketSpreads.length];

    // Calculate the bumped spreads

    for (int m = 0; m < marketSpreads.length; m++) {

      if (spreadBumpType == SpreadBumpType.ADDITIVE_PARALLEL) {
        bumpedCreditSpreads[m] = marketSpreads[m] + spreadBump;
      }

      if (spreadBumpType == SpreadBumpType.MULTIPLICATIVE_PARALLEL) {
        bumpedCreditSpreads[m] = marketSpreads[m] * (1 + spreadBump);
      }
    }

    return bumpedCreditSpreads;
  }

  // ----------------------------------------------------------------------------------------------------------------------------------------

  // Method to bump the credit spread term structure at a single (specified) tenor point by a specified amount

  public double[] getBumpedCreditSpreads(final double[] marketSpreads, final int spreadTenorToBump, final double spreadBump, final SpreadBumpType spreadBumpType) {

    double[] bumpedCreditSpreads = marketSpreads;

    // Calculate the bumped spreads

    if (spreadBumpType == SpreadBumpType.ADDITIVE_BUCKETED) {
      bumpedCreditSpreads[spreadTenorToBump] = bumpedCreditSpreads[spreadTenorToBump] + spreadBump;
    }

    if (spreadBumpType == SpreadBumpType.MULTIPLICATIVE_BUCKETED) {
      bumpedCreditSpreads[spreadTenorToBump] = bumpedCreditSpreads[spreadTenorToBump] * (1 + spreadBump);
    }

    return bumpedCreditSpreads;
  }

  // ----------------------------------------------------------------------------------------------------------------------------------------
}

@@ -191,16 +191,91 @@
 mpers;%0A%0A
+import java.util.Arrays;%0A%0Aimport com.opengamma.OpenGammaRuntimeException;%0A%0A
 /**%0A * C
@@ -1938,29 +1938,66 @@
 reads = 
-marketSpreads
+Arrays.copyOf(marketSpreads, marketSpreads.length)
 ;%0A%0A    /
@@ -2090,58 +2090,62 @@
 ETED
-) %7B%0A      bumpedCreditSpreads%5BspreadTenorToBump%5D =
+ %7C%7C spreadBumpType == SpreadBumpType.ADDITIVE) %7B%0A     
  bum
@@ -2181,16 +2181,17 @@
 oBump%5D +
+=
  spreadB
@@ -2200,21 +2200,21 @@
 p;%0A    %7D
-%0A%0A   
+ else
  if (spr
@@ -2266,16 +2266,67 @@
 BUCKETED
+ %7C%7C spreadBumpType == SpreadBumpType.MULTIPLICATIVE
 ) %7B%0A    
@@ -2370,53 +2370,115 @@
 mp%5D 
+*
 = 
-bumpedCreditSpreads%5BspreadTenorToBump%5D * (1
+(1 + spreadBump);%0A    %7D else %7B%0A      throw new OpenGammaRuntimeException(%22Unsupported spread bump type %22
  + s
@@ -2478,32 +2478,36 @@
 e %22 + spreadBump
+Type
 );%0A    %7D%0A%0A    re

Throw an error if given an unsupported credit bump