From e3af2bd4e7bc0df2892917975b6043f445b2a118 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 17 Mar 2023 18:20:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E8=BD=BD=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/NumberUtil.java | 26 ++++++++++++- .../plusone/commons/EnumerationTests.java | 39 +++++++++++++++++++ .../plusone/commons/util/NumberUtilTest.java | 35 +++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java index eb32634..088e005 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java @@ -27,7 +27,15 @@ public class NumberUtil { throw new IllegalStateException("Utility class"); } - public static int sum(int... numbers) { + public static int sum(short... numbers) { + int result = 0; + for (short number : numbers) { + result += number; + } + return result; + } + + public static long sum(int... numbers) { int result = 0; for (int number : numbers) { result += number; @@ -42,4 +50,20 @@ public class NumberUtil { } return result; } + + public static double sum(float... numbers) { + double result = 0; + for (double number : numbers) { + result += number; + } + return result; + } + + public static double sum(double... numbers) { + double result = 0; + for (double number : numbers) { + result += number; + } + return result; + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java new file mode 100644 index 0000000..6e950a7 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -0,0 +1,39 @@ +package xyz.zhouxy.plusone.commons; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import xyz.zhouxy.plusone.commons.util.Enumeration; + +class EnumerationTests { + + @Test + void testEnumeration() { + assertEquals(EntityStatus.AVAILABLE, EntityStatus.of(0)); + } +} + +final class EntityStatus extends Enumeration { + + private EntityStatus(int value, String name) { + super(value, name); + } + + // 常量 + public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常"); + public static final EntityStatus DISABLED = new EntityStatus(1, "禁用"); + + private static final EnumerationValuesHolder ENUMERATION_VALUES = new EnumerationValuesHolder<>( + AVAILABLE, + DISABLED); + + public static EntityStatus of(int value) { + return ENUMERATION_VALUES.get(value); + } + + @Override + public String toString() { + return "EntityStatus" + super.toString(); + } +} \ No newline at end of file diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java b/src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java new file mode 100644 index 0000000..f330013 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java @@ -0,0 +1,35 @@ +package xyz.zhouxy.plusone.commons.util; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class NumberUtilTest { + @Test + void testSum() { + long result = 0; + for (int i = 0; i < Integer.MAX_VALUE; i++) { + result += Short.MAX_VALUE; + } + System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE); + System.out.println("result: " + result); + assertFalse(Integer.MAX_VALUE > result); + + result = 0; + for (int i = 0; i < Integer.MAX_VALUE; i++) { + result += Short.MAX_VALUE; + } + System.out.println("Long.MAX_VALUE: " + Long.MAX_VALUE); + System.out.println("result: " + result); + assertTrue(Long.MAX_VALUE > result); + + result = 0; + for (int i = 0; i < Integer.MAX_VALUE; i++) { + result += Integer.MAX_VALUE; + } + System.out.println("Long.MAX_VALUE: " + Long.MAX_VALUE); + System.out.println("result: " + result); + assertTrue(Long.MAX_VALUE > result); + } +}