Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a 'SCALED' item in ShiftType. #1402

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public double applyShift(double value, double shiftAmount) {
public ValueAdjustment toValueAdjustment(double shiftAmount) {
return ValueAdjustment.ofDeltaAmount(shiftAmount);
}
},

/**
* An scaled shift where the value is multiplied by the shift.
* <p>
* {@code shiftedValue = (value * shiftAmount)}
*/
SCALED {
@Override
public double applyShift(double value, double shiftAmount) {
return value * shiftAmount;
}

@Override
public ValueAdjustment toValueAdjustment(double shiftAmount) {
return ValueAdjustment.ofMultiplier(shiftAmount);
}
};

//-------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ public class ShiftTypeTest {
public void test_applyShift() {
assertEquals(ShiftType.ABSOLUTE.applyShift(2, 0.1), 2.1);
assertEquals(ShiftType.RELATIVE.applyShift(2, 0.1), 2.2);
assertEquals(ShiftType.SCALED.applyShift(2, 1.1), 2.2);
}

public void test_toValueAdjustment() {
assertEquals(ShiftType.ABSOLUTE.toValueAdjustment(0.1).adjust(2), 2.1);
assertEquals(ShiftType.RELATIVE.toValueAdjustment(0.1).adjust(2), 2.2);
assertEquals(ShiftType.SCALED.toValueAdjustment(1.1).adjust(2), 2.2);
}

public void test_name() {
assertEquals(ShiftType.ABSOLUTE.name(), "ABSOLUTE");
assertEquals(ShiftType.RELATIVE.name(), "RELATIVE");
assertEquals(ShiftType.SCALED.name(), "SCALED");
}

public void test_toString() {
assertEquals(ShiftType.ABSOLUTE.toString(), "Absolute");
assertEquals(ShiftType.RELATIVE.toString(), "Relative");
assertEquals(ShiftType.SCALED.toString(), "Scaled");
}

public void coverage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,18 +795,8 @@ private double bumpedSpread(double spread, double amount, ShiftType shiftType) {
private double[] makeBumpedSpreads(double[] spreads, double amount, ShiftType shiftType) {
int n = spreads.length;
double[] res = new double[n];

if (shiftType == ShiftType.ABSOLUTE) {
for (int i = 0; i < n; i++) {
res[i] = spreads[i] + amount;
}
} else if (shiftType == ShiftType.RELATIVE) {
double a = 1 + amount;
for (int i = 0; i < n; i++) {
res[i] = spreads[i] * a;
}
} else {
throw new IllegalArgumentException("ShiftType " + shiftType + " is not supported");
for (int i = 0; i < n; i++) {
res[i] = shiftType.applyShift(spreads[i], amount);
}
return res;
}
Expand Down Expand Up @@ -885,17 +875,8 @@ private double[] makeBumpedSpreads(double[] spreads, double amount, ShiftType sh
int n = spreads.length;
double[] res = new double[n];
System.arraycopy(spreads, 0, res, 0, n);

switch (shiftType) {
case ABSOLUTE:
res[index] += amount;
break;
case RELATIVE:
res[index] += res[index] * amount;
break;
default:
throw new IllegalArgumentException("ShiftType " + shiftType + " is not supported");
}
res[index] = shiftType.applyShift(res[index], amount);
return res;
}

}