[공통] util → framework 유틸 #5

Open
forge-bot wants to merge 8 commits from forge/ACM-CM-002-attempt-2-run-87bf2707792a into main
Showing only changes of commit 2757f6a51b - Show all commits

View file

@ -0,0 +1,105 @@
package com.klaro.acquirecore.framework.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* Static utility class for monetary amount operations migrated from legacy C code.
* Provides parsing, formatting, and arithmetic for BigDecimal amounts.
*/
public final class AmountUtil {
public static final String DEFAULT_CURRENCY = "KRW";
private static final int DEFAULT_SCALE = 2;
private static final RoundingMode DEFAULT_ROUNDING = RoundingMode.HALF_UP;
private AmountUtil() {
// Utility class - prevent instantiation
}
public static BigDecimal parse(String str) {
if (str == null || str.isBlank()) {
return null;
}
try {
String cleaned = str.trim().replaceAll(",", "");
cleaned = cleaned.replaceAll("[A-Z]{3}\\s*", "");
return new BigDecimal(cleaned).setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
} catch (NumberFormatException e) {
return null;
}
}
public static String format(BigDecimal amount, String currency) {
if (amount == null) {
return "";
}
String curr = (currency != null && !currency.isBlank()) ? currency : DEFAULT_CURRENCY;
return String.format("%s %s", curr, amount.setScale(DEFAULT_SCALE, DEFAULT_ROUNDING).toPlainString());
}
public static String format(BigDecimal amount) {
return format(amount, DEFAULT_CURRENCY);
}
public static BigDecimal add(BigDecimal a, BigDecimal b) {
if (a == null || b == null) {
return null;
}
return a.add(b).setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
}
public static BigDecimal subtract(BigDecimal a, BigDecimal b) {
if (a == null || b == null) {
return null;
}
return a.subtract(b).setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
}
public static BigDecimal multiply(BigDecimal amount, int factor) {
if (amount == null) {
return null;
}
return amount.multiply(BigDecimal.valueOf(factor)).setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
}
public static BigDecimal multiply(BigDecimal amount, BigDecimal factor) {
if (amount == null || factor == null) {
return null;
}
return amount.multiply(factor).setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
}
public static BigDecimal divide(BigDecimal amount, BigDecimal divisor) {
if (amount == null || divisor == null || divisor.compareTo(BigDecimal.ZERO) == 0) {
return null;
}
return amount.divide(divisor, DEFAULT_SCALE, DEFAULT_ROUNDING);
}
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 boolean isZero(BigDecimal amount) {
return amount != null && amount.compareTo(BigDecimal.ZERO) == 0;
}
public static BigDecimal abs(BigDecimal amount) {
if (amount == null) {
return null;
}
return amount.abs().setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
}
public static BigDecimal negate(BigDecimal amount) {
if (amount == null) {
return null;
}
return amount.negate().setScale(DEFAULT_SCALE, DEFAULT_ROUNDING);
}
}