添加比较方法。

feature/net-util
ZhouXY108 2023-06-20 20:07:40 +08:00
parent 7d4ae57842
commit e61a393b93
1 changed files with 29 additions and 1 deletions

View File

@ -2,12 +2,40 @@ package xyz.zhouxy.plusone.commons.util;
import java.math.BigDecimal;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
public class BigDecimals {
public static boolean equals(BigDecimal a, BigDecimal b) {
public static final BigDecimal ZERO = new BigDecimal("0.00");
public static boolean equals(@Nullable BigDecimal a, @Nullable BigDecimal b) {
return (a == b) || (a != null && a.compareTo(b) == 0);
}
@Beta
public static boolean greaterThan(BigDecimal a, BigDecimal b) {
Preconditions.checkNotNull(a, "Parameter could not be null.");
Preconditions.checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) > 0);
}
@Beta
public static boolean lessThan(BigDecimal a, BigDecimal b) {
Preconditions.checkNotNull(a, "Parameter could not be null.");
Preconditions.checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) < 0);
}
@Beta
public static BigDecimal of(final String val) {
return (StringUtils.isBlank(val)) ? ZERO : new BigDecimal(val);
}
private BigDecimals() {
throw new IllegalStateException("Utility class");
}