BigDecimals:修复 equalsValue 方法第二个参数为 null 报空指针的问题;补充单元测试

pull/1/head
ZhouXY108 2024-12-04 01:16:00 +08:00
parent 65f0bb062c
commit 10761e92ec
2 changed files with 185 additions and 77 deletions

View File

@ -21,19 +21,27 @@ import java.math.BigDecimal;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/**
* BigDecimals
*
* <p>
* BigDecimal
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0
*/
public class BigDecimals {
public static boolean equalsValue(@Nullable BigDecimal a, @Nullable BigDecimal b) {
return (a == b) || (a != null && a.compareTo(b) == 0);
return (a == b) || (a != null && b != null && a.compareTo(b) == 0);
}
public static boolean gt(BigDecimal a, BigDecimal b) {
Preconditions.checkNotNull(a, "Parameter could not be null.");
Preconditions.checkNotNull(b, "Parameter could not be null.");
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) > 0);
}
@ -42,8 +50,8 @@ public class BigDecimals {
}
public static boolean lt(BigDecimal a, BigDecimal b) {
Preconditions.checkNotNull(a, "Parameter could not be null.");
Preconditions.checkNotNull(b, "Parameter could not be null.");
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) < 0);
}

View File

@ -16,11 +16,9 @@
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
@ -29,76 +27,178 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class BigDecimalsTests {
// TODO 【优化】 检查测试用例
@Test
void testToPlainString() {
BigDecimalFormatter formatter = BigDecimalFormatter.builder()
.setScale(2, RoundingMode.HALF_UP)
.stripTrailingZeros()
.build();
assertEquals("8.09", formatter.toPlainString(BigDecimals.of("8.090")));
assertEquals("8.09", formatter.toPlainString(BigDecimals.of("8.094")));
assertEquals("8.1", formatter.toPlainString(BigDecimals.of("8.095")));
assertEquals("8.1", formatter.toPlainString(BigDecimals.of("8.096")));
assertEquals("8.1", formatter.toPlainString(BigDecimals.of("8.100")));
void equalsValue_NullValues_ReturnsTrue() {
assertTrue(BigDecimals.equalsValue(null, null));
}
@Test
void test() {
Object a = 100 % 3.0;
log.info("a: {}", a);
}
void equalsValue_SameNonNullableValues_ReturnsTrue() {
BigDecimal bd1 = new BigDecimal("10");
BigDecimal bd2 = new BigDecimal("10.0");
assertTrue(BigDecimals.equalsValue(bd1, bd2));
}
class BigDecimalFormatter {
private final Function<BigDecimal, BigDecimal> func;
private BigDecimalFormatter(Function<BigDecimal, BigDecimal> wholeFunc) {
this.func = wholeFunc;
@Test
void equalsValue_DifferentNonNullableValues_ReturnsFalse() {
BigDecimal bd1 = new BigDecimal("10");
BigDecimal bd2 = new BigDecimal("20");
assertFalse(BigDecimals.equalsValue(bd1, bd2));
}
public static Builder builder() {
return new Builder();
@Test
void equalsValue_OneNullOneNonNullValue_ReturnsFalse() {
BigDecimal bd = new BigDecimal("10");
assertFalse(BigDecimals.equalsValue(bd, null));
assertFalse(BigDecimals.equalsValue(null, bd));
}
public String toPlainString(BigDecimal value) {
final BigDecimal finalDecimal = func == null ? value : func.apply(value);
return finalDecimal.toPlainString();
@Test
void gt_NullFirstValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.gt(null, bd));
}
public String toEngineeringString(BigDecimal value) {
final BigDecimal finalDecimal = func == null ? value : func.apply(value);
return finalDecimal.toEngineeringString();
@Test
void gt_NullSecondValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.gt(bd, null));
}
public static class Builder {
private Function<BigDecimal, BigDecimal> wholeFunc;
private Builder() {
@Test
void gt_SameValues_ReturnsFalse() {
BigDecimal bd = new BigDecimal("10");
assertFalse(BigDecimals.gt(bd, bd));
}
public Builder setScale(int newScale, RoundingMode roundingMode) {
final Function<BigDecimal, BigDecimal> func = value -> value.setScale(newScale, roundingMode);
if (wholeFunc == null) {
wholeFunc = func;
} else {
wholeFunc = func.andThen(func);
}
return this;
@Test
void gt_FirstGreaterThanSecond_ReturnsTrue() {
BigDecimal bd1 = new BigDecimal("20");
BigDecimal bd2 = new BigDecimal("10");
assertTrue(BigDecimals.gt(bd1, bd2));
}
public Builder stripTrailingZeros() {
if (wholeFunc == null) {
wholeFunc = BigDecimal::stripTrailingZeros;
} else {
wholeFunc = wholeFunc.andThen(BigDecimal::stripTrailingZeros);
}
return this;
@Test
void ge_NullFirstValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.ge(null, bd));
}
public BigDecimalFormatter build() {
return new BigDecimalFormatter(wholeFunc);
@Test
void ge_NullSecondValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.ge(bd, null));
}
@Test
void ge_SameValues_ReturnsTrue() {
BigDecimal bd = new BigDecimal("10");
assertTrue(BigDecimals.ge(bd, bd));
}
@Test
void ge_FirstGreaterThanSecond_ReturnsTrue() {
BigDecimal bd1 = new BigDecimal("20");
BigDecimal bd2 = new BigDecimal("10");
assertTrue(BigDecimals.ge(bd1, bd2));
}
@Test
void lt_NullFirstValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.lt(null, bd));
}
@Test
void lt_NullSecondValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.lt(bd, null));
}
@Test
void lt_SameValues_ReturnsFalse() {
BigDecimal bd = new BigDecimal("10");
assertFalse(BigDecimals.lt(bd, bd));
}
@Test
void lt_FirstLessThanSecond_ReturnsTrue() {
BigDecimal bd1 = new BigDecimal("10");
BigDecimal bd2 = new BigDecimal("20");
assertTrue(BigDecimals.lt(bd1, bd2));
}
@Test
void le_NullFirstValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.le(null, bd));
}
@Test
void le_NullSecondValue_ThrowsException() {
BigDecimal bd = new BigDecimal("10");
assertThrows(NullPointerException.class, () -> BigDecimals.le(bd, null));
}
@Test
void le_SameValues_ReturnsTrue() {
BigDecimal bd = new BigDecimal("10");
assertTrue(BigDecimals.le(bd, bd));
}
@Test
void le_FirstLessThanSecond_ReturnsTrue() {
BigDecimal bd1 = new BigDecimal("10");
BigDecimal bd2 = new BigDecimal("20");
assertTrue(BigDecimals.le(bd1, bd2));
}
@Test
void sum_NullArray_ReturnsZero() {
assertEquals(BigDecimal.ZERO, BigDecimals.sum());
}
@Test
void sum_SingleNonNullValue_ReturnsSameValue() {
BigDecimal bd = new BigDecimal("10");
assertEquals(bd, BigDecimals.sum(bd));
}
@Test
void sum_SingleNullValue_ReturnsZero() {
assertEquals(BigDecimal.ZERO, BigDecimals.sum((BigDecimal) null));
}
@Test
void sum_MultipleValues_ReturnsCorrectSum() {
BigDecimal bd1 = new BigDecimal("10");
BigDecimal bd2 = new BigDecimal("20");
BigDecimal bd3 = new BigDecimal("30");
assertEquals(new BigDecimal("60"), BigDecimals.sum(bd1, bd2, bd3));
}
@Test
void nullToZero_NullValue_ReturnsZero() {
assertEquals(BigDecimal.ZERO, BigDecimals.nullToZero(null));
}
@Test
void nullToZero_NonNullValue_ReturnsSameValue() {
BigDecimal bd = new BigDecimal("10");
assertEquals(bd, BigDecimals.nullToZero(bd));
}
@Test
void of_BlankString_ReturnsZero() {
assertEquals(BigDecimal.ZERO, BigDecimals.of(" "));
}
@Test
void of_NonBlankString_ReturnsCorrectBigDecimal() {
BigDecimal bd = new BigDecimal("10");
assertEquals(bd, BigDecimals.of("10"));
}
}