Skip to content

Commit

Permalink
FINERACT-2081: Refactor - Simplify LoanSchedulePeriodData.java class
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy committed Dec 9, 2024
1 parent bc2ef08 commit e42eeee
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 494 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ public static Long min(Long first, Long second, boolean notNull) {
* @param notNull
* if true then null parameter is omitted, otherwise returns null
*/
public static Long min(Long first, Long second, Long third, boolean notNull) {
return min(min(first, second, notNull), third, notNull);
public static Long min(boolean notNull, Long first, Long... amounts) {
Long result = first;
for (Long amount : amounts) {
result = min(result, amount, notNull);
}
return result;
}

/** @return sum the two values considering null values */
Expand All @@ -123,37 +127,37 @@ public static Long add(Long first, Long second) {
}

/** @return sum the values considering null values */
public static Long add(Long first, Long second, Long third) {
return add(add(first, second), third);
}

/** @return sum the values considering null values */
public static Long add(Long first, Long second, Long third, Long fourth) {
return add(add(add(first, second), third), fourth);
}

/** @return sum the values considering null values */
public static Long add(Long first, Long second, Long third, Long fourth, Long fifth) {
return add(add(add(add(first, second), third), fourth), fifth);
public static Long add(Long... amounts) {
Long result = null;
for (Long amount : amounts) {
result = add(result, amount);
}
return result;
}

/** @return first minus second considering null values, maybe negative */
public static Long subtract(Long first, Long second) {
return first == null ? null : second == null ? first : Math.subtractExact(first, second);
}

/**
* @return first minus the others considering null values, maybe negative
*/
public static Long subtractToZero(Long first, Long second, Long third) {
return subtractToZero(subtract(first, second), third);
/** @return first minus second considering null values, maybe negative */
public static Long subtract(Long first, Long... amounts) {
Long result = first;
for (Long amount : amounts) {
result = subtract(result, amount);
}
return result;
}

/**
* @return first minus the others considering null values, maybe negative
*/
public static Long subtractToZero(Long first, Long second, Long third, Long fourth) {
return subtractToZero(subtract(subtract(first, second), third), fourth);
public static Long subtractToZero(Long first, Long... amounts) {
Long result = first;
for (Long amount : amounts) {
result = subtractToZero(result, amount);
}
return result;
}

/** @return NONE negative first minus second considering null values */
Expand Down Expand Up @@ -241,8 +245,12 @@ public static BigDecimal min(BigDecimal first, BigDecimal second, boolean notNul
* @param notNull
* if true then null parameter is omitted, otherwise returns null
*/
public static BigDecimal min(BigDecimal first, BigDecimal second, BigDecimal third, boolean notNull) {
return min(min(first, second, notNull), third, notNull);
public static BigDecimal min(boolean notNull, BigDecimal first, BigDecimal... amounts) {
BigDecimal result = first;
for (BigDecimal amount : amounts) {
result = min(result, amount, notNull);
}
return result;
}

/** @return sum the two values considering null values */
Expand All @@ -256,44 +264,21 @@ public static BigDecimal add(BigDecimal first, BigDecimal second, MathContext mc
}

/** @return sum the values considering null values */
public static BigDecimal add(BigDecimal first, BigDecimal second, BigDecimal third) {
return add(first, second, third, MoneyHelper.getMathContext());
}

/** @return sum the values considering null values */
public static BigDecimal add(BigDecimal first, BigDecimal second, BigDecimal third, MathContext mc) {
return add(add(first, second, mc), third, mc);
}

/** @return sum the values considering null values */
public static BigDecimal add(BigDecimal first, BigDecimal second, BigDecimal third, BigDecimal fourth) {
return add(first, second, third, fourth, MoneyHelper.getMathContext());
}

/** @return sum the values considering null values */
public static BigDecimal add(BigDecimal first, BigDecimal second, BigDecimal third, BigDecimal fourth, MathContext mc) {
return add(add(add(first, second, mc), third, mc), fourth, mc);
}

/** @return sum the values considering null values */
public static BigDecimal add(BigDecimal first, BigDecimal second, BigDecimal third, BigDecimal fourth, BigDecimal fifth) {
return add(first, second, third, fourth, fifth, MoneyHelper.getMathContext());
}

/** @return sum the values considering null values */
public static BigDecimal add(BigDecimal first, BigDecimal second, BigDecimal third, BigDecimal fourth, BigDecimal fifth,
MathContext mc) {
return add(add(add(add(first, second, mc), third, mc), fourth, mc), fifth, mc);
public static BigDecimal add(BigDecimal... amounts) {
BigDecimal result = null;
for (BigDecimal amount : amounts) {
result = add(result, amount, MoneyHelper.getMathContext());
}
return result;
}

/** @return first minus second considering null values, maybe negative */
public static BigDecimal subtract(BigDecimal first, BigDecimal second) {
return subtract(first, second, MoneyHelper.getMathContext());
}

/** @return first minus the others considering null values, maybe negative */
public static BigDecimal subtract(BigDecimal first, BigDecimal second, BigDecimal third) {
return subtract(subtract(first, second), third);
public static BigDecimal subtract(BigDecimal first, BigDecimal... amounts) {
BigDecimal result = first;
for (BigDecimal amount : amounts) {
result = subtract(result, amount, MoneyHelper.getMathContext());
}
return result;
}

/** @return first minus second considering null values, maybe negative */
Expand All @@ -302,22 +287,9 @@ public static BigDecimal subtract(BigDecimal first, BigDecimal second, MathConte
}

/** @return NONE negative first minus second considering null values */
public static BigDecimal subtractToZero(BigDecimal first, BigDecimal second) {
return negativeToZero(subtract(first, second));
}

/**
* @return first minus the others considering null values, maybe negative
*/
public static BigDecimal subtractToZero(BigDecimal first, BigDecimal second, BigDecimal third) {
return subtractToZero(subtract(first, second), third);
}

/**
* @return first minus the others considering null values, maybe negative
*/
public static BigDecimal subtractToZero(BigDecimal first, BigDecimal second, BigDecimal third, BigDecimal fourth) {
return subtractToZero(subtract(subtract(first, second), third), fourth);
public static BigDecimal subtractToZero(BigDecimal first, BigDecimal... amounts) {
BigDecimal result = subtract(first, amounts);
return negativeToZero(result);
}

/**
Expand Down Expand Up @@ -409,39 +381,39 @@ public static Money plus(Money first, Money second, MathContext mc) {
return first == null ? second : second == null ? first : first.plus(second, mc);
}

public static Money plus(Money first, Money second, Money third) {
return plus(plus(first, second), third);
}

public static Money plus(Money first, Money second, Money third, MathContext mc) {
return plus(plus(first, second), third, mc);
public static Money plus(Money... multipleAmounts) {
Money result = null;
for (Money amount : multipleAmounts) {
result = plus(result, amount);
}
return result;
}

public static Money plus(Money first, Money second, Money third, Money fourth) {
return plus(plus(plus(first, second), third), fourth);
public static Money plus(MathContext mc, Money... multipleAmounts) {
Money result = null;
for (Money amount : multipleAmounts) {
result = plus(result, amount, mc);
}
return result;
}

public static Money minus(Money first, Money second) {
return first == null ? null : second == null ? first : first.minus(second);
}

/**
* @return first minus the others considering null values, maybe negative
*/
public static Money minusToZero(Money first, Money second, Money third) {
return minusToZero(minus(first, second), third);
public static Money minus(Money first, Money... multipleAmounts) {
Money result = first;
for (Money amount : multipleAmounts) {
result = minus(result, amount);
}
return result;
}

/**
* @return first minus the others considering null values, maybe negative
*/
public static Money minusToZero(Money first, Money second, Money third, Money fourth) {
return minusToZero(minus(minus(first, second), third), fourth);
}

/** @return NONE negative first minus second considering null values */
public static Money minusToZero(Money first, Money second) {
return negativeToZero(minus(first, second));
public static Money minusToZero(Money first, Money... multipleAmounts) {
return negativeToZero(minus(first, multipleAmounts));
}

/**
Expand All @@ -459,8 +431,12 @@ public static Money min(Money first, Money second, boolean notNull) {
* @param notNull
* if true then null parameter is omitted, otherwise returns null
*/
public static Money min(Money first, Money second, Money third, boolean notNull) {
return min(min(first, second, notNull), third, notNull);
public static Money min(boolean notNull, Money first, Money... amounts) {
Money result = first;
for (Money amount : amounts) {
result = min(result, amount, notNull);
}
return result;
}

/** @return Money null safe negate */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,10 @@ private LoanSchedulePeriodData fetchLoanSchedulePeriodData(ResultSet rs) throws
final LocalDate dueDate = JdbcSupport.getLocalDate(rs, "dueDate");
final BigDecimal principalDue = JdbcSupport.getBigDecimalDefaultToZeroIfNull(rs, "principalAmount");
final BigDecimal interestDueOnPrincipalOutstanding = JdbcSupport.getBigDecimalDefaultToZeroIfNull(rs, "interestAmount");
final BigDecimal totalInstallmentAmount = principalDue.add(interestDueOnPrincipalOutstanding);
final BigDecimal feeChargesDueForPeriod = JdbcSupport.getBigDecimalDefaultToZeroIfNull(rs, "feeAmount");
final BigDecimal penaltyChargesDueForPeriod = JdbcSupport.getBigDecimalDefaultToZeroIfNull(rs, "penaltyAmount");
final Integer periodNumber = null;
final LocalDate fromDate = null;
final BigDecimal principalOutstanding = null;
final BigDecimal totalDueForPeriod = null;
return LoanSchedulePeriodData.repaymentOnlyPeriod(periodNumber, fromDate, dueDate, principalDue, principalOutstanding,
interestDueOnPrincipalOutstanding, feeChargesDueForPeriod, penaltyChargesDueForPeriod, totalDueForPeriod,
totalInstallmentAmount);
return LoanSchedulePeriodData.repaymentOnlyPeriod(null, null, dueDate, principalDue, null, interestDueOnPrincipalOutstanding,
feeChargesDueForPeriod, penaltyChargesDueForPeriod);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class LoanScheduleModelDownPaymentPeriod implements LoanScheduleMod

private final int periodNumber;
private final LocalDate periodDate;
private Money principalDue;
private final Money principalDue;
private final Money outstandingLoanBalance;

public static LoanScheduleModelDownPaymentPeriod downPayment(final int periodNumber, final LocalDate periodDate,
Expand Down
Loading

0 comments on commit e42eeee

Please sign in to comment.