first commit.

feature/net-util
ZhouXY108 2022-11-07 17:49:27 +08:00
commit 0d5eef24e3
9 changed files with 349 additions and 0 deletions

35
.gitignore vendored 100644
View File

@ -0,0 +1,35 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

30
pom.xml 100644
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.4</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package xyz.zhouxy.plusone.exception;
/**
* {@code getCode}
* {@code code}
* 便 {@code code}
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see PlusoneException
*/
public interface IWithCode {
int getCode();
}

View File

@ -0,0 +1,33 @@
package xyz.zhouxy.plusone.exception;
/**
* 9999999
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class PlusoneException extends RuntimeException implements IWithCode {
private static final long serialVersionUID = -2546365325001947203L;
private final int code;
protected PlusoneException(int code, String msg) {
super(msg);
this.code = code;
}
protected PlusoneException(int code, Throwable cause) {
super(cause);
this.code = code;
}
protected PlusoneException(int code, String msg, Throwable cause) {
super(msg, cause);
this.code = code;
}
@Override
public int getCode() {
return this.code;
}
}

View File

@ -0,0 +1,91 @@
package xyz.zhouxy.plusone.util;
import java.util.Objects;
/**
*
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public final class EnumUtil {
private EnumUtil() {
throw new IllegalStateException("Utility class");
}
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal
* @return
*/
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) {
E[] values = clazz.getEnumConstants();
try {
return values[ordinal];
} catch (IndexOutOfBoundsException e) {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
}
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal
* @return
*/
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, Integer ordinal) {
E[] values = clazz.getEnumConstants();
try {
return Objects.nonNull(ordinal) ? values[ordinal] : values[0];
} catch (IndexOutOfBoundsException e) {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
}
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal
* @return
*/
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, Integer ordinal) {
E[] values = clazz.getEnumConstants();
try {
return Objects.nonNull(ordinal) ? values[ordinal] : null;
} catch (IndexOutOfBoundsException e) {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
}
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
if (ordinal == null) {
throw new IllegalArgumentException("ordinal 不能为空");
}
E[] values = clazz.getEnumConstants();
if (ordinal >= 0 && ordinal < values.length) {
return ordinal;
}
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, Integer ordinal) {
if (Objects.isNull(ordinal)) {
return null;
}
return checkOrdinal(clazz, ordinal);
}
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, Integer ordinal) {
if (Objects.isNull(ordinal)) {
return 0;
}
return checkOrdinal(clazz, ordinal);
}
}

View File

@ -0,0 +1,38 @@
package xyz.zhouxy.plusone.util;
import java.util.Objects;
public abstract class Enumeration<T extends Enumeration<T>> {
protected final int value;
protected final String name;
protected Enumeration(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Enumeration<?> other = (Enumeration<?>) obj;
return value == other.value;
}
}

View File

@ -0,0 +1,22 @@
package xyz.zhouxy.plusone.util;
import java.util.HashMap;
import java.util.Map;
public final class EnumerationValuesHolder<T extends Enumeration<T>> {
private final Map<Integer, T> constants = new HashMap<>();
public EnumerationValuesHolder(T[] values) {
for (T value : values) {
put(value);
}
}
private void put(T constant) {
this.constants.put(constant.getValue(), constant);
}
public T get(int value) {
return this.constants.get(value);
}
}

View File

@ -0,0 +1,29 @@
package xyz.zhouxy.plusone.util;
/**
* NumberUtil
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class NumberUtil {
private NumberUtil() {
throw new IllegalStateException("Utility class");
}
public static int sum(int... numbers) {
int result = 0;
for (int number : numbers) {
result += number;
}
return result;
}
public static long sum(long... numbers) {
long result = 0;
for (long number : numbers) {
result += number;
}
return result;
}
}

View File

@ -0,0 +1,58 @@
package xyz.zhouxy.plusone.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
/**
*
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@ToString
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class RestfulResult {
public static final int SUCCESS_STATUS = 2000000;
private final Object status;
private final String message;
private final Object data;
private RestfulResult(Object status, String message) {
this(status, message, null);
}
public static RestfulResult success() {
return new RestfulResult(SUCCESS_STATUS, "操作成功");
}
public static RestfulResult success(String message) {
return new RestfulResult(SUCCESS_STATUS, message);
}
public static RestfulResult success(String message, Object data) {
return new RestfulResult(SUCCESS_STATUS, message, data);
}
public static RestfulResult error() {
return new RestfulResult(500000, "未知错误");
}
public static RestfulResult error(Object status, String message) {
return new RestfulResult(status, message);
}
public static RestfulResult error(Object status, String message, Object data) {
return new RestfulResult(status, message, data);
}
public static RestfulResult error(Object status, Throwable e) {
return new RestfulResult(status, e.getMessage());
}
}