Merge pull request '合并更改。' (#1) from dev into master

Reviewed-on: #1
feature/net-util
ZhouXY108 2023-03-17 19:03:41 +08:00
commit a56c0aa09d
15 changed files with 323 additions and 8 deletions

27
pom.xml
View File

@ -14,19 +14,38 @@
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.18.24</lombok.version> <lombok.version>1.18.24</lombok.version>
<jackson.version>2.13.4</jackson.version> <jackson.version>2.13.4</jackson.version>
<google-jsr305.version>3.0.2</google-jsr305.version>
<guava.version>31.1-jre</guava.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>com.google.code.findbugs</groupId>
<artifactId>lombok</artifactId> <artifactId>jsr305</artifactId>
<version>${lombok.version}</version> <version>${google-jsr305.version}</version>
<optional>true</optional> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version> <version>${jackson.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.constant; package xyz.zhouxy.plusone.commons.constant;
import java.util.regex.Pattern; import java.util.regex.Pattern;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.constant; package xyz.zhouxy.plusone.commons.constant;
/** /**

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.exception; package xyz.zhouxy.plusone.commons.exception;
/** /**

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.exception; package xyz.zhouxy.plusone.commons.exception;
/** /**

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
public abstract class Arrays2 { public abstract class Arrays2 {
@ -31,6 +47,14 @@ public abstract class Arrays2 {
return values; return values;
} }
public static double[] of(final double... values) {
return values;
}
public static float[] of(final float... values) {
return values;
}
private Arrays2() { private Arrays2() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Objects; import java.util.Objects;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Map; import java.util.Map;
@ -43,9 +59,7 @@ public abstract class Enumeration<T extends Enumeration<T>> {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); return "[" + value + ": " + name + "]";
builder.append("[").append(value).append(": ").append(name).append("]");
return builder.toString();
} }
protected static final class EnumerationValuesHolder<T extends Enumeration<T>> { protected static final class EnumerationValuesHolder<T extends Enumeration<T>> {

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
/** /**
@ -11,7 +27,15 @@ public class NumberUtil {
throw new IllegalStateException("Utility class"); 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; int result = 0;
for (int number : numbers) { for (int number : numbers) {
result += number; result += number;
@ -26,4 +50,20 @@ public class NumberUtil {
} }
return result; 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;
}
} }

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.List; import java.util.List;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Arrays; import java.util.Arrays;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.regex.Pattern; import java.util.regex.Pattern;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2022-2023 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.
*/
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;

View File

@ -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<EntityStatus> {
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<EntityStatus> 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();
}
}

View File

@ -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);
}
}