Files
plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java

90 lines
2.7 KiB
Java
Raw Normal View History

2024-10-12 00:57:35 +08:00
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-05-27 04:09:46 +08:00
package xyz.zhouxy.plusone.commons.util;
import java.math.BigDecimal;
import javax.annotation.Nonnull;
2023-06-20 20:07:40 +08:00
import javax.annotation.Nullable;
2024-10-21 23:17:52 +08:00
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
2023-05-27 04:09:46 +08:00
/**
* BigDecimals
*
* <p>
* BigDecimal 工具类
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0
*/
2024-10-21 23:17:52 +08:00
public class BigDecimals {
2023-06-20 20:07:40 +08:00
public static boolean equalsValue(@Nullable BigDecimal a, @Nullable BigDecimal b) {
return (a == b) || (a != null && b != null && a.compareTo(b) == 0);
2023-05-27 04:09:46 +08:00
}
2024-05-28 09:38:37 +08:00
public static boolean gt(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
2023-06-20 20:07:40 +08:00
return (a != b) && (a.compareTo(b) > 0);
}
2024-05-28 09:38:37 +08:00
public static boolean ge(BigDecimal a, BigDecimal b) {
return gt(a, b) || equalsValue(a, b);
2024-05-28 09:38:37 +08:00
}
public static boolean lt(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
2023-06-20 20:07:40 +08:00
return (a != b) && (a.compareTo(b) < 0);
}
2024-05-28 09:38:37 +08:00
public static boolean le(BigDecimal a, BigDecimal b) {
return lt(a, b) || equalsValue(a, b);
2024-05-28 09:38:37 +08:00
}
public static BigDecimal sum(final BigDecimal... numbers) {
if (ArrayTools.isNullOrEmpty(numbers)) {
return BigDecimal.ZERO;
}
BigDecimal result = BigDecimals.nullToZero(numbers[0]);
for (int i = 1; i < numbers.length; i++) {
BigDecimal value = numbers[i];
if (value != null) {
result = result.add(value);
}
}
return result;
}
@Nonnull
public static BigDecimal nullToZero(@Nullable final BigDecimal val) {
return val != null ? val : BigDecimal.ZERO;
}
2024-10-21 23:17:52 +08:00
@StaticFactoryMethod(BigDecimal.class)
2023-06-20 20:07:40 +08:00
public static BigDecimal of(final String val) {
2024-10-21 23:17:52 +08:00
return (StringTools.isNotBlank(val)) ? new BigDecimal(val) : BigDecimal.ZERO;
2024-10-09 18:46:18 +08:00
}
2023-05-27 04:09:46 +08:00
private BigDecimals() {
throw new IllegalStateException("Utility class");
}
}