From e61a393b93c0112418e2ffc9addf75254bc7682d Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 20 Jun 2023 20:07:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=AF=94=E8=BE=83=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/BigDecimals.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java b/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java index bc1b623..3b6bf78 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java @@ -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"); }