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 3b6bf78..f56a998 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java @@ -6,7 +6,6 @@ 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 { @@ -17,21 +16,26 @@ public class BigDecimals { return (a == b) || (a != null && a.compareTo(b) == 0); } - @Beta - public static boolean greaterThan(BigDecimal a, BigDecimal b) { + public static boolean gt(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) { + public static boolean ge(BigDecimal a, BigDecimal b) { + return gt(a, b) || equals(a, b); + } + + public static boolean lt(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 le(BigDecimal a, BigDecimal b) { + return lt(a, b) || equals(a, b); + } + public static BigDecimal of(final String val) { return (StringUtils.isBlank(val)) ? ZERO : new BigDecimal(val); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java index 0d0a8a2..2f1b18a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -85,9 +85,8 @@ public abstract class Enumeration> implements Comparabl this.valueMap = valueMap; } - @SafeVarargs @StaticFactoryMethod(ValueSet.class) - public static > ValueSet of(T... values) { + public static > ValueSet of(T[] values) { Map temp = Arrays.stream(values) .collect(Collectors.toMap(Enumeration::getId, Function.identity())); return new ValueSet<>(Collections.unmodifiableMap(temp)); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java index 4cbd3c0..3c499f0 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java @@ -16,6 +16,8 @@ package xyz.zhouxy.plusone.commons.util; +import java.math.BigDecimal; + /** * Numbers * @@ -23,10 +25,6 @@ package xyz.zhouxy.plusone.commons.util; */ public class Numbers { - private Numbers() { - throw new IllegalStateException("Utility class"); - } - // sum public static int sum(final short... numbers) { @@ -69,6 +67,14 @@ public class Numbers { return result; } + public static BigDecimal sum(final BigDecimal... numbers) { + BigDecimal result = BigDecimals.of("0.00"); + for (BigDecimal number : numbers) { + result = result.add(number); + } + return result; + } + // between public static boolean between(short value, short min, short max) { @@ -90,4 +96,12 @@ public class Numbers { public static boolean between(double value, double min, double max) { return value >= min && value < max; } + + public static boolean between(BigDecimal value, BigDecimal min, BigDecimal max) { + return BigDecimals.ge(value, min) && BigDecimals.lt(value, max); + } + + private Numbers() { + throw new IllegalStateException("Utility class"); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index dd04458..6bc4506 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -38,7 +38,7 @@ final class EntityStatus extends Enumeration { public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常"); public static final EntityStatus DISABLED = new EntityStatus(1, "禁用"); - private static final ValueSet VALUE_SET = ValueSet.of(AVAILABLE, DISABLED); + private static final ValueSet VALUE_SET = ValueSet.of(new EntityStatus[] { AVAILABLE, DISABLED }); public static EntityStatus of(int value) { return VALUE_SET.get(value); @@ -57,7 +57,7 @@ final class Result extends Enumeration { public static final Result SUCCESSFUL = new Result(1, "成功"); public static final Result FAILURE = new Result(0, "失败"); - private static final ValueSet VALUE_SET = ValueSet.of(SUCCESSFUL, FAILURE); + private static final ValueSet VALUE_SET = ValueSet.of(new Result[] { SUCCESSFUL, FAILURE }); public static Result of(int id) { return VALUE_SET.get(id);