[공통] util → framework 유틸 #7
1 changed files with 97 additions and 0 deletions
|
|
@ -0,0 +1,97 @@
|
|||
package com.klaro.acquirecore.framework.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* Static utility class for amount/money operations migrated from legacy C util_amount.c
|
||||
*/
|
||||
public final class AmountUtil {
|
||||
|
||||
private static final int SCALE = 2;
|
||||
private static final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP;
|
||||
|
||||
private AmountUtil() {
|
||||
// Utility class - prevent instantiation
|
||||
}
|
||||
|
||||
public static BigDecimal fromCents(long cents) {
|
||||
return BigDecimal.valueOf(cents, SCALE);
|
||||
}
|
||||
|
||||
public static BigDecimal fromString(String str) {
|
||||
if (str == null || str.isEmpty()) return null;
|
||||
return new BigDecimal(str).setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static String toString(BigDecimal amount) {
|
||||
return amount == null ? null : amount.setScale(SCALE, ROUNDING_MODE).toPlainString();
|
||||
}
|
||||
|
||||
public static BigDecimal add(BigDecimal a, BigDecimal b) {
|
||||
if (a == null) return b;
|
||||
if (b == null) return a;
|
||||
return a.add(b).setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal subtract(BigDecimal a, BigDecimal b) {
|
||||
if (a == null || b == null) throw new IllegalArgumentException("Amounts must not be null");
|
||||
return a.subtract(b).setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal multiply(BigDecimal amount, double factor) {
|
||||
if (amount == null) throw new IllegalArgumentException("Amount must not be null");
|
||||
return amount.multiply(BigDecimal.valueOf(factor)).setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal multiply(BigDecimal a, BigDecimal b) {
|
||||
if (a == null || b == null) throw new IllegalArgumentException("Amounts must not be null");
|
||||
return a.multiply(b).setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal divide(BigDecimal amount, BigDecimal divisor) {
|
||||
if (amount == null || divisor == null) throw new IllegalArgumentException("Arguments must not be null");
|
||||
if (divisor.compareTo(BigDecimal.ZERO) == 0) throw new ArithmeticException("Division by zero");
|
||||
return amount.divide(divisor, SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static int compare(BigDecimal a, BigDecimal b) {
|
||||
if (a == null && b == null) return 0;
|
||||
if (a == null) return -1;
|
||||
if (b == null) return 1;
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
public static long toCents(BigDecimal amount) {
|
||||
if (amount == null) return 0L;
|
||||
return amount.setScale(SCALE, ROUNDING_MODE).movePointRight(SCALE).longValue();
|
||||
}
|
||||
|
||||
public static boolean isZero(BigDecimal amount) {
|
||||
return amount != null && amount.compareTo(BigDecimal.ZERO) == 0;
|
||||
}
|
||||
|
||||
public static boolean isPositive(BigDecimal amount) {
|
||||
return amount != null && amount.compareTo(BigDecimal.ZERO) > 0;
|
||||
}
|
||||
|
||||
public static boolean isNegative(BigDecimal amount) {
|
||||
return amount != null && amount.compareTo(BigDecimal.ZERO) < 0;
|
||||
}
|
||||
|
||||
public static BigDecimal abs(BigDecimal amount) {
|
||||
return amount == null ? null : amount.abs().setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal negate(BigDecimal amount) {
|
||||
return amount == null ? null : amount.negate().setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal zero() {
|
||||
return BigDecimal.ZERO.setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
|
||||
public static BigDecimal of(double value) {
|
||||
return BigDecimal.valueOf(value).setScale(SCALE, ROUNDING_MODE);
|
||||
}
|
||||
}
|
||||
Reference in a new issue