2 Commits

Author SHA1 Message Date
8e7ee5b448 refactor(util): ArrayTools 中的 isNullOrEmpty 重命名为 isEmpty
保持方法命名的一致性

BREAKING CHANGE: `ArrayTools#isNullOrEmpty` 重命名为 `ArrayTools#isEmpty`

Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/48
Co-authored-by: ZhouXY108 <luquanlion@outlook.com>
Co-committed-by: ZhouXY108 <luquanlion@outlook.com>
2025-05-01 02:50:36 +08:00
ba12fec71d docs: 修改段落问题
Signed-off-by: zhouxy108 <luquanlion@outlook.com>
2025-04-27 07:57:19 +08:00
110 changed files with 774 additions and 1644 deletions

View File

@@ -73,7 +73,7 @@ System.out.println(result); // Output: Return string
异常实现 `MultiTypesException` 的 `MultiTypesException#getType` 方法,返回对应的场景类型。 异常实现 `MultiTypesException` 的 `MultiTypesException#getType` 方法,返回对应的场景类型。
表示场景类型的枚举实现 `MultiTypesException.ExceptionType`,其中的工厂方法用于创建对应类型的异常 表示场景类型的枚举实现 `MultiTypesException.ExceptionType`,其中的工厂方法用于创建类型对象
```java ```java
public final class LoginException public final class LoginException
extends RuntimeException extends RuntimeException

View File

@@ -1,7 +1,7 @@
{ {
"version": "0.2", "version": "0.2",
"ignorePaths": [ "ignorePaths": [
"*/src/test" "src/test"
], ],
"dictionaryDefinitions": [], "dictionaryDefinitions": [],
"dictionaries": [], "dictionaries": [],
@@ -14,31 +14,25 @@
"cspell", "cspell",
"databind", "databind",
"datasource", "datasource",
"dbutils",
"fasterxml", "fasterxml",
"findbugs", "findbugs",
"gson", "gson",
"Hikari", "Hikari",
"hutool", "hutool",
"jasypt",
"jbcrypt",
"Jdbc", "Jdbc",
"joda", "joda",
"logback", "logback",
"mapstruct", "mapstruct",
"mindrot",
"Multimap", "Multimap",
"Multiset", "Multiset",
"mybatis", "mybatis",
"Nonnull", "Nonnull",
"NOSONAR", "NOSONAR",
"okhttp", "okhttp",
"ooxml",
"overriden", "overriden",
"plusone", "plusone",
"println", "println",
"projectlombok", "projectlombok",
"querydsl",
"regexs", "regexs",
"Seata", "Seata",
"sonarlint", "sonarlint",

View File

@@ -1,124 +0,0 @@
<?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>
<parent>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId>
<version>1.1.0-RC1</version>
</parent>
<artifactId>plusone-commons</artifactId>
<description>
常见工具集,结合 guava 使用。
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-dependencies</artifactId>
<version>1.1.0-RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- ========== Compile Dependencies ========== -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<optional>true</optional>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
<!-- ========== Test Dependencies ========== -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,187 +0,0 @@
/*
* Copyright 2025 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.gson.adapter;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 包含 JSR-310 相关数据类型的 {@code TypeAdapter}
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.1.0
* @see TypeAdapter
* @see com.google.gson.GsonBuilder
*/
public class JSR310TypeAdapters {
/**
* {@code LocalDate} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code LocalDate} 进行相互转换。
*/
public static final class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
private final DateTimeFormatter dateTimeFormatter;
/**
* 默认构造函数,
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE} 进行 {@link LocalDate} 的序列化与反序列化。
*/
public LocalDateTypeAdapter() {
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
}
/**
* 构造函数,
* 使用传入的 {@link DateTimeFormatter} 进行 {@link LocalDate} 的序列化与反序列化。
*
* @param formatter 用于序列化 {@link LocalDate} 的格式化器,不可为 {@code null}。
*/
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
this.dateTimeFormatter = formatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, LocalDate value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public LocalDate read(JsonReader in) throws IOException {
return LocalDate.parse(in.nextString(), dateTimeFormatter);
}
}
/**
* {@code LocalDateTime} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code LocalDateTime} 进行相互转换。
*/
public static final class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
private final DateTimeFormatter dateTimeFormatter;
/**
* 默认构造函数,
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} 进行 {@link LocalDateTime} 的序列化与反序列化。
*/
public LocalDateTimeTypeAdapter() {
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
}
/**
* 构造函数,
* 使用传入的 {@link DateTimeFormatter} 进行 {@link LocalDateTime} 的序列化与反序列化。
*
* @param formatter 用于序列化 {@link LocalDateTime} 的格式化器,不可为 {@code null}。
*/
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
this.dateTimeFormatter = formatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, LocalDateTime value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public LocalDateTime read(JsonReader in) throws IOException {
return LocalDateTime.parse(in.nextString(), dateTimeFormatter);
}
}
/**
* {@code ZonedDateTime} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code ZonedDateTime} 进行相互转换。
*/
public static final class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
private final DateTimeFormatter dateTimeFormatter;
/**
* 默认构造函数,
* 使用 {@link DateTimeFormatter#ISO_ZONED_DATE_TIME} 进行 {@link ZonedDateTime} 的序列化与反序列化。
*/
public ZonedDateTimeTypeAdapter() {
this.dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
}
/**
* 构造函数,
* 使用传入的 {@link DateTimeFormatter} 进行 {@link ZonedDateTime} 的序列化与反序列化。
*
* @param formatter 用于序列化 {@link ZonedDateTime} 的格式化器,不可为 {@code null}。
*/
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
this.dateTimeFormatter = formatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public ZonedDateTime read(JsonReader in) throws IOException {
return ZonedDateTime.parse(in.nextString(), dateTimeFormatter);
}
}
/**
* {@code Instant} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code Instant} 进行相互转换。
*
* <p>
* 使用 {@link DateTimeFormatter#ISO_INSTANT} 进行 {@link Instant} 的序列化与反序列化。
*
*/
public static final class InstantTypeAdapter extends TypeAdapter<Instant> {
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, Instant value) throws IOException {
out.value(DateTimeFormatter.ISO_INSTANT.format(value));
}
/** {@inheritDoc} */
@Override
public Instant read(JsonReader in) throws IOException {
return Instant.parse(in.nextString());
}
}
private JSR310TypeAdapters() {
throw new IllegalStateException("Utility class");
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2025 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.
*/
/**
* Gson 相关类型适配器
*/
package xyz.zhouxy.plusone.commons.gson.adapter;

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2025 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.
*/
/**
* Gson 相关辅助工具
*/
package xyz.zhouxy.plusone.commons.gson;

View File

@@ -1,145 +0,0 @@
/*
* Copyright 2025 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.gson.adapter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.*;
@Slf4j
public final class JSR310TypeAdaptersTests {
final Gson gsonWithDefaultFormatter = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter().nullSafe())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter().nullSafe())
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter().nullSafe())
.registerTypeAdapter(Instant.class, new InstantTypeAdapter().nullSafe())
.create();
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
final DateTimeFormatter localDateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy/MM/dd HH:mm:ss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter();
final DateTimeFormatter zonedDateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy/MM/dd HH:mm:ss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.appendZoneId()
.toFormatter();
final Gson gsonWithSpecifiedFormatter = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter(dateFormatter).nullSafe())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter(localDateTimeFormatter).nullSafe())
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter(zonedDateTimeFormatter).nullSafe())
.create();
final LocalDate date = LocalDate.of(2025, 6, 6);
final LocalDateTime localDateTime = date.atTime(6, 6, 6, 666000000);
final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("+08:00"));
final Instant instant = zonedDateTime.toInstant();
@DisplayName("测试使用 TypeAdapter 中默认的 formatter 进行序列化")
@Test
void test_serialize_defaultFormatter() {
Foo foo = new Foo();
foo.localDate = date;
foo.localDateTime = localDateTime;
foo.zonedDateTime = zonedDateTime;
foo.instant = instant;
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\",\"instant\":\"%s\"}",
DateTimeFormatter.ISO_LOCAL_DATE.format(date),
DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime),
DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime),
DateTimeFormatter.ISO_INSTANT.format(instant)
);
assertEquals(json, gsonWithDefaultFormatter.toJson(foo));
}
@DisplayName("测试指定 formatter 进行序列化")
@Test
void test_serialize_specifiedFormatter() {
Foo foo = new Foo();
foo.localDate = date;
foo.localDateTime = localDateTime;
foo.zonedDateTime = zonedDateTime;
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\"}",
dateFormatter.format(date),
localDateTimeFormatter.format(localDateTime),
zonedDateTimeFormatter.format(zonedDateTime)
);
assertEquals(json, gsonWithSpecifiedFormatter.toJson(foo));
}
@DisplayName("测试使用 TypeAdapter 中默认的 formatter 进行反序列化")
@Test
void test_deserialize_defaultFormatter() {
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\",\"instant\":\"%s\"}",
DateTimeFormatter.ISO_LOCAL_DATE.format(date),
DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime),
DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime),
DateTimeFormatter.ISO_INSTANT.format(instant)
);
Foo foo = gsonWithDefaultFormatter.fromJson(json, Foo.class);
assertEquals(date, foo.localDate);
assertEquals(localDateTime, foo.localDateTime);
assertEquals(zonedDateTime, foo.zonedDateTime);
assertEquals(instant, foo.instant);
}
@DisplayName("测试指定 formatter 进行反序列化")
@Test
void test_deserialize_specifiedFormatter() {
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\"}",
dateFormatter.format(date),
localDateTimeFormatter.format(localDateTime),
zonedDateTimeFormatter.format(zonedDateTime)
);
Foo foo = gsonWithSpecifiedFormatter.fromJson(json, Foo.class);
assertEquals(date, foo.localDate);
assertEquals(localDateTime, foo.localDateTime);
assertEquals(zonedDateTime, foo.zonedDateTime);
}
static class Foo {
LocalDate localDate;
LocalDateTime localDateTime;
ZonedDateTime zonedDateTime;
Instant instant;
}
}

View File

@@ -1,263 +0,0 @@
<?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>
<parent>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId>
<version>1.1.0-RC1</version>
</parent>
<artifactId>plusone-dependencies</artifactId>
<packaging>pom</packaging>
<description>
plusone 相关项目依赖管理
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<guava.version>33.4.8-jre</guava.version>
<joda-time.version>2.14.0</joda-time.version>
<google-jsr305.version>3.0.2</google-jsr305.version>
<checker-qual.version>3.49.2</checker-qual.version>
<commons-lang3.version>3.17.0</commons-lang3.version>
<commons-collections4.version>4.5.0</commons-collections4.version>
<commons-io.version>2.19.0</commons-io.version>
<commons-dbutils.version>1.8.1</commons-dbutils.version>
<commons-crypto.version>1.2.0</commons-crypto.version>
<logback.version>1.3.15</logback.version>
<jackson.version>2.18.3</jackson.version>
<gson.version>2.13.1</gson.version>
<mapstruct.version>1.6.3</mapstruct.version>
<h2.version>2.2.224</h2.version>
<mybatis.version>3.5.19</mybatis.version>
<querydsl.version>5.1.0</querydsl.version>
<byte-buddy.version>1.17.5</byte-buddy.version>
<poi.version>5.4.1</poi.version>
<java-jwt.version>4.5.0</java-jwt.version>
<jasypt.version>1.9.3</jasypt.version>
<jbcrypt.version>0.4</jbcrypt.version>
<lombok.version>1.18.36</lombok.version>
<hutool.version>5.8.37</hutool.version>
<junit.version>5.12.1</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>${google-jsr305.version}</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>${checker-qual.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-crypto</artifactId>
<version>${commons-crypto.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- Query DSL -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byte-buddy.version}</version>
</dependency>
<!-- Apache POI - Core -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- Apache POI - Excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>${java-jwt.version}</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>${jasypt.version}</version>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>${jbcrypt.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-bom</artifactId>
<version>${hutool.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

178
pom.xml
View File

@@ -5,14 +5,178 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>xyz.zhouxy.plusone</groupId> <groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId> <artifactId>plusone-commons</artifactId>
<version>1.1.0-RC1</version> <version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules> <properties>
<module>plusone-commons</module> <!-- Basic properties -->
<module>plusone-dependencies</module> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</modules> <java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Versions of compile dependencies -->
<guava.version>33.4.2-jre</guava.version>
<joda-time.version>2.14.0</joda-time.version>
<!-- Versions of test dependencies -->
<commons-lang3.version>3.17.0</commons-lang3.version>
<logback.version>1.2.13</logback.version>
<junit.version>5.12.1</junit.version>
<lombok.version>1.18.36</lombok.version>
<hutool.version>5.8.37</hutool.version>
<mybatis.version>3.5.19</mybatis.version>
<h2.version>2.2.224</h2.version>
<jackson.version>2.18.3</jackson.version>
<gson.version>2.12.1</gson.version>
</properties>
<dependencies>
<!-- ========== Compile Dependencies ========== -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
<optional>true</optional>
</dependency>
<!-- ========== Test Dependencies ========== -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories> <repositories>
<repository> <repository>

View File

@@ -24,15 +24,17 @@
* 标识<b>静态工厂方法</b> * 标识<b>静态工厂方法</b>
* Effective Java Item1 建议考虑用静态工厂方法替换构造器 * Effective Java Item1 建议考虑用静态工厂方法替换构造器
* 因而考虑有一个注解可以标记一下静态工厂方法以和其它方法进行区分 * 因而考虑有一个注解可以标记一下静态工厂方法以和其它方法进行区分
* </p>
* *
* <h3> * <h3>
* 2. {@link ReaderMethod} {@link WriterMethod} * 2. {@link ReaderMethod} {@link WriterMethod}
* </h3> * </h3>
* <p> * <p>
* 分别标识<b>读方法</b> getter<b>写方法</b> setter * 分别标识<b>读方法</b> getter<b>写方法</b> setter
* * </p>
* <p> * <p>
* 最早是写了一个集合类为了方便判断使用读写锁时哪些情况下使用读锁哪些情况下使用写锁 * 最早是写了一个集合类为了方便判断使用读写锁时哪些情况下使用读锁哪些情况下使用写锁
* </p>
* *
* <h3> * <h3>
* 3. {@link UnsupportedOperation} * 3. {@link UnsupportedOperation}
@@ -40,19 +42,22 @@
* <p> * <p>
* 标识该方法不被支持或没有实现将抛出 {@link UnsupportedOperationException} * 标识该方法不被支持或没有实现将抛出 {@link UnsupportedOperationException}
* 为了方便在使用时不需要点进源码就能知道该方法没有实现 * 为了方便在使用时不需要点进源码就能知道该方法没有实现
* </p>
* *
* <h3> * <h3>
* 4. {@link Virtual} * 4. {@link Virtual}
* </h3> * </h3>
* <p> * <p>
* Java final 的实例方法对应 C++/C&num; 中的虚方法允许被子类覆写 * Java final 的实例方法对应 C++/C# 中的虚方法允许被子类覆写
* {@link Virtual} 注解旨在设计父类时强调该方法父类虽然有默认实现但子类可以根据自己的需要覆写 * {@link Virtual} 注解旨在设计父类时强调该方法父类虽然有默认实现但子类可以根据自己的需要覆写
* </p>
* *
* <h3> * <h3>
* 5. {@link ValueObject} * 5. {@link ValueObject}
* </h3> * </h3>
* <p> * <p>
* 标记一个类表示其作为值对象区别于 Entity * 标记一个类表示其作为值对象区别于 Entity
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */

View File

@@ -27,7 +27,7 @@ import javax.annotation.Nullable;
/** /**
* {@link Ref} 包装了一个值表示对该值的应用 * {@link Ref} 包装了一个值表示对该值的应用
* *
* <p>灵感来自于 C&num; {@code ref} 参数修饰符C&num; 允许通过以下方式将值返回给调用端</p> * <p>灵感来自于 C# {@value ref} 参数修饰符C# 允许通过以下方式将值返回给调用端</p>
* <pre> * <pre>
* void Method(ref int refArgument) * void Method(ref int refArgument)
* { * {
@@ -51,7 +51,7 @@ import javax.annotation.Nullable;
* <p> * <p>
* 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值 * 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值
* 调用方在调用方法之后使用 {@code getValue()} 获取结果 * 调用方在调用方法之后使用 {@code getValue()} 获取结果
* * </p>
* <pre> * <pre>
* String method(final Ref&lt;Integer&gt; intRefArgument, final Ref&lt;String&gt; strRefArgument) { * String method(final Ref&lt;Integer&gt; intRefArgument, final Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44); * intRefArgument.transformValue(i -&gt; i + 44);

View File

@@ -20,8 +20,8 @@
* <h3>1. Ref</h3> * <h3>1. Ref</h3>
* <p> * <p>
* {@link Ref} 包装了一个值表示对该值的应用 * {@link Ref} 包装了一个值表示对该值的应用
* * </p>
* <p>灵感来自于 C&num; {@code ref} 参数修饰符C&num; 允许通过以下方式将值返回给调用端</p> * <p>灵感来自于 C# {@value ref} 参数修饰符C# 允许通过以下方式将值返回给调用端</p>
* <pre> * <pre>
* void Method(ref int refArgument) * void Method(ref int refArgument)
* { * {
@@ -45,7 +45,7 @@
* <p> * <p>
* 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值 * 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值
* 调用方在调用方法之后使用 {@code getValue()} 获取结果 * 调用方在调用方法之后使用 {@code getValue()} 获取结果
* * </p>
* <pre> * <pre>
* String method(Ref&lt;Integer&gt; intRefArgument, Ref&lt;String&gt; strRefArgument) { * String method(Ref&lt;Integer&gt; intRefArgument, Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44); * intRefArgument.transformValue(i -&gt; i + 44);
@@ -65,6 +65,7 @@
* <p> * <p>
* 类似于枚举这样的类型通常需要设置固定的码值表示对应的含义 * 类似于枚举这样的类型通常需要设置固定的码值表示对应的含义
* 可实现 {@link IWithCode}{@link IWithIntCode}{@link IWithLongCode}便于在需要的地方对这些接口的实现进行处理 * 可实现 {@link IWithCode}{@link IWithIntCode}{@link IWithLongCode}便于在需要的地方对这些接口的实现进行处理
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */

View File

@@ -15,7 +15,7 @@
*/ */
/** /**
* <h2>集合</h2> * <h2>集合<h2>
* *
* <h3> * <h3>
* 1. {@link CollectionTools} * 1. {@link CollectionTools}

View File

@@ -31,7 +31,7 @@ public final class PatternConsts {
* yyyyMMdd * yyyyMMdd
* *
* @see RegexConsts#BASIC_ISO_DATE * @see RegexConsts#BASIC_ISO_DATE
* * </p>
*/ */
public static final Pattern BASIC_ISO_DATE = Pattern.compile(RegexConsts.BASIC_ISO_DATE); public static final Pattern BASIC_ISO_DATE = Pattern.compile(RegexConsts.BASIC_ISO_DATE);

View File

@@ -15,7 +15,7 @@
*/ */
/** /**
* <h2>常量</h2> * <h2>常量<h2>
* *
* <h3> * <h3>
* 1. 正则常量 * 1. 正则常量

View File

@@ -24,12 +24,13 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* *
* <p> * <p>
* 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型 * 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型
* * </p>
* <p> * <p>
* 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型 * 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型
* * </p>
* <p> * <p>
* 表示场景类型的枚举实现 {@link ExceptionType}其中的工厂方法用于创建对应类型的异常 * 表示场景类型的枚举实现 {@link ExceptionType}其中的工厂方法用于创建类型对象
* </p>
* *
* <pre> * <pre>
* public final class LoginException * public final class LoginException
@@ -60,7 +61,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* *
* // ... * // ...
* *
* public enum Type implements ExceptionType&lt;LoginException&gt; { * public enum Type implements ExceptionType<LoginException> {
* DEFAULT("00", "当前会话未登录"), * DEFAULT("00", "当前会话未登录"),
* NOT_TOKEN("10", "未提供token"), * NOT_TOKEN("10", "未提供token"),
* INVALID_TOKEN("20", "token无效"), * INVALID_TOKEN("20", "token无效"),
@@ -116,6 +117,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* <pre> * <pre>
* throw LoginException.Type.TOKEN_TIMEOUT.create(); * throw LoginException.Type.TOKEN_TIMEOUT.create();
* </pre> * </pre>
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -33,6 +33,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
* <pre> * <pre>
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE)); * throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
* </pre> * </pre>
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -21,10 +21,11 @@ package xyz.zhouxy.plusone.commons.exception.business;
* *
* <p> * <p>
* 业务异常 * 业务异常
* </p>
* *
* <p> * <p>
* <b>NOTE: 通常表示业务中的意外情况用户错误输入缺失必填字段用户余额不足等</b> * <b>NOTE: 通常表示业务中的意外情况用户错误输入缺失必填字段用户余额不足等</b>
* * </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0
*/ */

View File

@@ -26,10 +26,11 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
* *
* <p> * <p>
* 用户输入内容非法 * 用户输入内容非法
* </p>
* *
* <p> * <p>
* <b>NOTE: 属业务异常</b> * <b>NOTE: 属业务异常</b>
* * </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0
*/ */

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
* *
* <p> * <p>
* 用户请求参数错误 * 用户请求参数错误
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -15,17 +15,18 @@
*/ */
/** /**
* <h2>异常</h2> * <h2>异常<h2>
* *
* <h3>1. {@link MultiTypesException} - 多类型异常</h3> * <h3>1. {@link MultiTypesException} - 多类型异常</h3>
* <p> * <p>
* 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型 * 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型
* * </p>
* <p> * <p>
* 异常实现 {@link MultiTypesException} {@link MultiTypesException#getType} 方法返回对应的场景类型 * 异常实现 {@link MultiTypesException} {@link MultiTypesException#getType} 方法返回对应的场景类型
* * </p>
* <p> * <p>
* 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType}其中的工厂方法用于创建对应类型的异常 * 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType}其中的工厂方法用于创建类型对象
* </p>
* *
* <pre> * <pre>
* public final class LoginException * public final class LoginException
@@ -56,7 +57,7 @@
* *
* // ... * // ...
* *
* public enum Type implements ExceptionType&lt;LoginException&gt; { * public enum Type implements ExceptionType<LoginException> {
* DEFAULT("00", "当前会话未登录"), * DEFAULT("00", "当前会话未登录"),
* NOT_TOKEN("10", "未提供token"), * NOT_TOKEN("10", "未提供token"),
* INVALID_TOKEN("20", "token无效"), * INVALID_TOKEN("20", "token无效"),
@@ -112,6 +113,7 @@
* <pre> * <pre>
* throw LoginException.Type.TOKEN_TIMEOUT.create(); * throw LoginException.Type.TOKEN_TIMEOUT.create();
* </pre> * </pre>
* </p>
* *
* <h3>2. 业务异常</h3> * <h3>2. 业务异常</h3>
* 预设常见的业务异常可继承 {@link BizException} 自定义业务异常 * 预设常见的业务异常可继承 {@link BizException} 自定义业务异常

View File

@@ -21,60 +21,55 @@ package xyz.zhouxy.plusone.commons.exception.system;
* *
* <p> * <p>
* 当数据操作的结果不符合预期时抛出 * 当数据操作的结果不符合预期时抛出
* </p>
* *
* <p> * <p>
* 比如当一个 insert update 操作时预计影响数据库中的一行数据但结果却影响了零条数据或多条数据 * 比如当一个 insert update 操作时预计影响数据库中的一行数据但结果却影响了零条数据或多条数据
* 当出现这种始料未及的诡异情况时抛出 {@link DataOperationResultException} 并回滚事务 * 当出现这种始料未及的诡异情况时抛出 {@link DataOperationResultException} 并回滚事务
* 后续需要排查原因 * 后续需要排查原因
* * </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0
*/ */
public final class DataOperationResultException extends SysException { public final class DataOperationResultException extends SysException {
private final long expected; private static final String DEFAULT_MSG = "数据操作的结果不符合预期";
private final long actual;
/** /**
* 创建一个 {@code DataOperationResultException} 对象 * 使用默认 message 构造新的 {@code DataOperationResultException}
* * {@code cause} 未初始化后面可能会通过调用 {@link #initCause} 进行初始化
* @param expected 预期影响的行数
* @param actual 实际影响的行数
*/ */
public DataOperationResultException(long expected, long actual) { public DataOperationResultException() {
super(String.format("The number of rows affected is expected to be %d, but is: %d", expected, actual)); super(DEFAULT_MSG);
this.expected = expected;
this.actual = actual;
} }
/** /**
* 创建一个 {@code DataOperationResultException} 对象 * 使用指定的 {@code message} 构造新的 {@code DataOperationResultException}
* {@code cause} 未初始化后面可能会通过调用 {@link #initCause} 进行初始化
* *
* @param expected 预期影响的行数 * @param message 异常信息
* @param actual 实际影响的行数
* @param message 错误信息
*/ */
public DataOperationResultException(long expected, long actual, String message) { public DataOperationResultException(String message) {
super(message); super(message);
this.expected = expected;
this.actual = actual;
} }
/** /**
* 预期影响的行数 * 使用指定的 {@code cause} 构造新的 {@code DataOperationResultException}
* {@code message} (cause==null ? null : cause.toString())
* *
* @return the expected * @param cause 包装的异常
*/ */
public long getExpected() { public DataOperationResultException(Throwable cause) {
return expected; super(cause);
} }
/** /**
* 实际影响的行数 * 使用指定的 {@code message} {@code cause} 构造新的 {@code DataOperationResultException}
* *
* @return the actual * @param message 异常信息
* @param cause 包装的异常
*/ */
public long getActual() { public DataOperationResultException(String message, Throwable cause) {
return actual; super(message, cause);
} }
} }

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* *
* <p> * <p>
* 在无法找到可访问的 Mac 地址时抛出 * 在无法找到可访问的 Mac 地址时抛出
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* *
* <p> * <p>
* 通常表示应用代码存在问题或因环境问题引发异常 * 通常表示应用代码存在问题或因环境问题引发异常
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -24,6 +24,7 @@ import com.google.common.annotations.Beta;
* <p> * <p>
* 一个特殊的 {@link java.util.function.UnaryOperator} * 一个特殊的 {@link java.util.function.UnaryOperator}
* 表示对 {@code boolean} 值的一元操作 * 表示对 {@code boolean} 值的一元操作
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -24,6 +24,7 @@ import com.google.common.annotations.Beta;
* <p> * <p>
* 一个特殊的 {@link java.util.function.UnaryOperator} * 一个特殊的 {@link java.util.function.UnaryOperator}
* 表示对 {@code char} 的一元操作 * 表示对 {@code char} 的一元操作
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.function;
* *
* <p> * <p>
* 表示一个无入参无返回值的操作可抛出异常 * 表示一个无入参无返回值的操作可抛出异常
* </p>
* *
* @param <E> 可抛出的异常类型 * @param <E> 可抛出的异常类型
* *

View File

@@ -24,6 +24,7 @@ import java.util.function.Supplier;
* *
* <p> * <p>
* 返回 {@code Optional&lt;T&gt;} 对象 * 返回 {@code Optional&lt;T&gt;} 对象
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -23,6 +23,7 @@ import java.util.function.Predicate;
* *
* <p> * <p>
* {@link Predicate} 相关操作 * {@link Predicate} 相关操作
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.function;
* *
* <p> * <p>
* 允许抛出异常的消费操作是一个特殊的 {@link java.util.function.Consumer} * 允许抛出异常的消费操作是一个特殊的 {@link java.util.function.Consumer}
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -20,6 +20,7 @@ package xyz.zhouxy.plusone.commons.function;
* *
* <p> * <p>
* 接收一个参数并返回一个结果可以抛出异常 * 接收一个参数并返回一个结果可以抛出异常
* </p>
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param <R> 返回结果类型 * @param <R> 返回结果类型

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.function;
* *
* <p> * <p>
* 接收一个参数返回一个布尔值可抛出异常 * 接收一个参数返回一个布尔值可抛出异常
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -21,6 +21,7 @@ package xyz.zhouxy.plusone.commons.function;
* *
* <p> * <p>
* 允许抛出异常的 Supplier 接口 * 允许抛出异常的 Supplier 接口
* </p>
* *
* @param <T> 结果类型 * @param <T> 结果类型
* @param <E> 异常类型 * @param <E> 异常类型

View File

@@ -20,6 +20,7 @@
* <h3>1. PredicateTools</h3> * <h3>1. PredicateTools</h3>
* <p> * <p>
* {@link PredicateTools} 用于 {@link java.util.function.Predicate} 的相关操作 * {@link PredicateTools} 用于 {@link java.util.function.Predicate} 的相关操作
* </p>
* *
* <h3>2. Functional interfaces</h3> * <h3>2. Functional interfaces</h3>
* <p> * <p>
@@ -38,6 +39,7 @@
* | Optional | ToOptionalBiFunction | Optional&lt;R&gt; apply(T,U) | * | Optional | ToOptionalBiFunction | Optional&lt;R&gt; apply(T,U) |
* | Optional | ToOptionalFunction | Optional&lt;R&gt; apply(T) | * | Optional | ToOptionalFunction | Optional&lt;R&gt; apply(T) |
* </pre> * </pre>
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */

View File

@@ -40,6 +40,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* *
* <p> * <p>
* 中国第二代居民身份证号 * 中国第二代居民身份证号
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -23,8 +23,6 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
/** /**
* 身份证号 * 身份证号
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */
public interface IDCardNumber { public interface IDCardNumber {

View File

@@ -37,9 +37,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* <p> * <p>
* 根据传入的 {@code size} {@code pageNum} * 根据传入的 {@code size} {@code pageNum}
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值 * 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PagingParams
* @see PageResult * @see PageResult
*/ */
public class PagingAndSortingQueryParams { public class PagingAndSortingQueryParams {

View File

@@ -21,12 +21,6 @@ import java.util.List;
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams.SortableProperty; import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams.SortableProperty;
/**
* 分页参数
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PagingAndSortingQueryParams
*/
public class PagingParams { public class PagingParams {
/** 每页大小 */ /** 每页大小 */

View File

@@ -40,7 +40,7 @@ public class UnifiedResponses {
* @return {@code UnifiedResponse} 对象 * @return {@code UnifiedResponse} 对象
* {@code code} = "2000000", {@code message} = "SUCCESS", {@code data} = null * {@code code} = "2000000", {@code message} = "SUCCESS", {@code data} = null
*/ */
public static <T> UnifiedResponse<T> success() { public static UnifiedResponse<Void> success() {
return new UnifiedResponse<>(SUCCESS_CODE, DEFAULT_SUCCESS_MSG); return new UnifiedResponse<>(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
} }
@@ -51,7 +51,7 @@ public class UnifiedResponses {
* @return {@code UnifiedResponse} 对象 * @return {@code UnifiedResponse} 对象
* {@code code} = "2000000", {@code data} = null * {@code code} = "2000000", {@code data} = null
*/ */
public static <T> UnifiedResponse<T> success(@Nullable String message) { public static UnifiedResponse<Void> success(@Nullable String message) {
return new UnifiedResponse<>(SUCCESS_CODE, message); return new UnifiedResponse<>(SUCCESS_CODE, message);
} }
@@ -83,7 +83,7 @@ public class UnifiedResponses {
* @param message 错误信息 * @param message 错误信息
* @return {@code UnifiedResponse} 对象{@code data} {@code null} * @return {@code UnifiedResponse} 对象{@code data} {@code null}
*/ */
public static <T> UnifiedResponse<T> error(String code, @Nullable String message) { public static UnifiedResponse<Void> error(String code, @Nullable String message) {
return new UnifiedResponse<>(code, message); return new UnifiedResponse<>(code, message);
} }
@@ -109,7 +109,7 @@ public class UnifiedResponses {
* {@code message} 为异常的 {@code message} * {@code message} 为异常的 {@code message}
* {@code data} {@code null} * {@code data} {@code null}
*/ */
public static <T> UnifiedResponse<T> error(String code, Throwable e) { public static UnifiedResponse<Void> error(String code, Throwable e) {
return new UnifiedResponse<>(code, e.getMessage()); return new UnifiedResponse<>(code, e.getMessage());
} }
@@ -128,7 +128,7 @@ public class UnifiedResponses {
* @param message 响应信息 * @param message 响应信息
* @return {@code UnifiedResponse} 对象{@code data} {@code null} * @return {@code UnifiedResponse} 对象{@code data} {@code null}
*/ */
public static <T> UnifiedResponse<T> of(String code, @Nullable String message) { public static UnifiedResponse<Void> of(String code, @Nullable String message) {
return new UnifiedResponse<>(code, message); return new UnifiedResponse<>(code, message);
} }

View File

@@ -22,12 +22,12 @@
* 分页组件由 {@link PagingAndSortingQueryParams} 作为入参 * 分页组件由 {@link PagingAndSortingQueryParams} 作为入参
* 因为分页必须伴随着排序不然可能出现同一个对象重复出现在不同页有的对象不被查询到的情况 * 因为分页必须伴随着排序不然可能出现同一个对象重复出现在不同页有的对象不被查询到的情况
* 所以分页查询的入参必须包含排序条件 * 所以分页查询的入参必须包含排序条件
* * </p>
* <p> * <p>
* 用户可继承 {@link PagingAndSortingQueryParams} * 用户可继承 {@link PagingAndSortingQueryParams}
* 构建自己的分页查询入参需在构造器中调用 {@link PagingAndSortingQueryParams} 的构造器传入一个 Map 作为白名单 * 构建自己的分页查询入参需在构造器中调用 {@link PagingAndSortingQueryParams} 的构造器传入一个 Map 作为白名单
* key 是供前端指定用于排序的属性名value 是对应数据库中的字段名只有在白名单中指定的属性名才允许作为排序条件 * key 是供前端指定用于排序的属性名value 是对应数据库中的字段名只有在白名单中指定的属性名才允许作为排序条件
* * </p>
* <p> * <p>
* {@link PagingAndSortingQueryParams} 包含三个主要的属性 * {@link PagingAndSortingQueryParams} 包含三个主要的属性
* <ul> * <ul>
@@ -37,20 +37,21 @@
* </ul> * </ul>
* 其中 orderBy 是一个 List可以指定多个排序条件每个排序条件是一个字符串 * 其中 orderBy 是一个 List可以指定多个排序条件每个排序条件是一个字符串
* 格式为属性名-ASC属性名-DESC分别表示升序和降序 * 格式为属性名-ASC属性名-DESC分别表示升序和降序
* * </p>
* <p> * <p>
* 比如前端传入的 orderBy ["name-ASC","age-DESC"]意味着要按 name 进行升序name 相同的情况下则按 age 进行降序 * 比如前端传入的 orderBy ["name-ASC","age-DESC"]意味着要按 name 进行升序name 相同的情况下则按 age 进行降序
* * </p>
* <p> * <p>
* 使用时调用 {@link PagingAndSortingQueryParams#buildPagingParams()} 方法获取分页参数 {@link PagingParams} * 使用时调用 {@link PagingAndSortingQueryParams#buildPagingParams()} 方法获取分页参数 {@link PagingParams}
* * </p>
* <p> * <p>
* 分页结果可以存放到 {@link PageResult} 作为出参 * 分页结果可以存放到 {@link PageResult} 作为出参
* </p>
* *
* <h3>2. {@link UnifiedResponse}</h3> * <h3>2. {@link UnifiedResponse}</h3>
* <p> * <p>
* {@link UnifiedResponse} 对返回给前端的数据进行封装包含 codemessagedata * {@link UnifiedResponse} 对返回给前端的数据进行封装包含 codemessagedata
* * </p>
* <p> * <p>
* 可使用 {@link UnifiedResponses} 快速构建 {@link UnifiedResponse} 对象 * 可使用 {@link UnifiedResponses} 快速构建 {@link UnifiedResponse} 对象
* {@link UnifiedResponses} 默认的成功代码为 "2000000" * {@link UnifiedResponses} 默认的成功代码为 "2000000"
@@ -59,6 +60,7 @@
* 中所示范的继承 {@link UnifiedResponses} 实现自己的工厂类 * 中所示范的继承 {@link UnifiedResponses} 实现自己的工厂类
* 自定义 SUCCESS_CODE DEFAULT_SUCCESS_MSG 和工厂方法 * 自定义 SUCCESS_CODE DEFAULT_SUCCESS_MSG 和工厂方法
* <a href="http://zhouxy.xyz:3000/plusone/plusone-commons/issues/22">issue#22</a> * <a href="http://zhouxy.xyz:3000/plusone/plusone-commons/issues/22">issue#22</a>
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */

View File

@@ -18,6 +18,7 @@
* <h2>业务建模组件</h2> * <h2>业务建模组件</h2>
* <p> * <p>
* 包含业务建模可能用到的性别身份证等元素也包含 DTO 相关类如分页查询参数响应结果分页结果等 * 包含业务建模可能用到的性别身份证等元素也包含 DTO 相关类如分页查询参数响应结果分页结果等
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */

View File

@@ -15,7 +15,7 @@
*/ */
/** /**
* <h2>时间 API</h2> * <h2>时间 API<h2>
* *
* <h3>1. 季度 API</h3> * <h3>1. 季度 API</h3>
* *

View File

@@ -33,6 +33,7 @@ import javax.annotation.Nullable;
* *
* <p> * <p>
* 数组工具类 * 数组工具类
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -16,6 +16,7 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -29,6 +30,7 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
* *
* <p> * <p>
* 本工具类不封装过多判断逻辑鼓励充分使用项目中的工具类进行逻辑判断 * 本工具类不封装过多判断逻辑鼓励充分使用项目中的工具类进行逻辑判断
* </p>
* *
* <pre> * <pre>
* AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank."); * AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
@@ -61,104 +63,40 @@ public class AssertTools {
* 检查实参 * 检查实参
* *
* @param condition 判断参数是否符合条件的结果 * @param condition 判断参数是否符合条件的结果
* @param errorMessage 异常信息 * @param errMsg 异常信息
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, @Nullable String errorMessage) { public static void checkArgument(boolean condition, @Nullable String errMsg) {
checkCondition(condition, () -> new IllegalArgumentException(errorMessage)); checkCondition(condition, () -> new IllegalArgumentException(errMsg));
} }
/** /**
* 检查实参 * 检查实参
* *
* @param condition 判断参数是否符合条件的结果 * @param condition 判断参数是否符合条件的结果
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, Supplier<String> errorMessageSupplier) { public static void checkArgument(boolean condition, Supplier<String> messageSupplier) {
checkCondition(condition, () -> new IllegalArgumentException(errorMessageSupplier.get())); checkCondition(condition, () -> new IllegalArgumentException(messageSupplier.get()));
} }
/** /**
* 检查实参 * 检查实参
* *
* @param condition 判断参数是否符合条件的结果 * @param condition 判断参数是否符合条件的结果
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, public static void checkArgument(boolean condition, String format, Object... args) {
String errorMessageTemplate, Object... errorMessageArgs) { checkCondition(condition, () -> new IllegalArgumentException(String.format(format, args)));
checkCondition(condition,
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
// ================================ // ================================
// #endregion - Argument // #endregion - Argument
// ================================ // ================================
// ================================
// #region - ArgumentNotNull
// ================================
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @throws IllegalArgumentException {@code obj} {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj) {
checkCondition(obj != null, IllegalArgumentException::new);
return obj;
}
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @param errorMessage 异常信息
* @throws IllegalArgumentException {@code obj} {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessage) {
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessage));
return obj;
}
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @param errorMessageSupplier 异常信息
* @throws IllegalArgumentException {@code obj} {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessageSupplier.get()));
return obj;
}
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
* @throws IllegalArgumentException {@code obj} {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(obj != null,
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)));
return obj;
}
// ================================
// #endregion - ArgumentNotNull
// ================================
// ================================ // ================================
// #region - State // #region - State
// ================================ // ================================
@@ -177,40 +115,38 @@ public class AssertTools {
* 检查状态 * 检查状态
* *
* @param condition 判断状态是否符合条件的结果 * @param condition 判断状态是否符合条件的结果
* @param errorMessage 异常信息 * @param errMsg 异常信息
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, @Nullable String errorMessage) { public static void checkState(boolean condition, @Nullable String errMsg) {
checkCondition(condition, () -> new IllegalStateException(errorMessage)); checkCondition(condition, () -> new IllegalStateException(errMsg));
} }
/** /**
* 检查状态 * 检查状态
* *
* @param condition 判断状态是否符合条件的结果 * @param condition 判断状态是否符合条件的结果
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, Supplier<String> errorMessageSupplier) { public static void checkState(boolean condition, Supplier<String> messageSupplier) {
checkCondition(condition, () -> new IllegalStateException(errorMessageSupplier.get())); checkCondition(condition, () -> new IllegalStateException(messageSupplier.get()));
} }
/** /**
* 检查状态 * 检查状态
* *
* @param condition 判断状态是否符合条件的结果 * @param condition 判断状态是否符合条件的结果
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, public static void checkState(boolean condition, String format, Object... args) {
String errorMessageTemplate, Object... errorMessageArgs) { checkCondition(condition, () -> new IllegalStateException(String.format(format, args)));
checkCondition(condition,
() -> new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
// ================================ // ================================
// #endregion - State // #endregion
// ================================ // ================================
// ================================ // ================================
@@ -221,7 +157,7 @@ public class AssertTools {
* 判空 * 判空
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参 *
* @throws NullPointerException {@code obj} {@code null} 时抛出 * @throws NullPointerException {@code obj} {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj) { public static <T> void checkNotNull(@Nullable T obj) {
@@ -233,11 +169,11 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errorMessage 异常信息 * @param errMsg 异常信息 *
* @throws NullPointerException {@code obj} {@code null} 时抛出 * @throws NullPointerException {@code obj} {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, String errorMessage) { public static <T> void checkNotNull(@Nullable T obj, String errMsg) {
checkCondition(obj != null, () -> new NullPointerException(errorMessage)); checkCondition(obj != null, () -> new NullPointerException(errMsg));
} }
/** /**
@@ -245,11 +181,11 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息 *
* @throws NullPointerException {@code obj} {@code null} 时抛出 * @throws NullPointerException {@code obj} {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) { public static <T> void checkNotNull(@Nullable T obj, Supplier<String> messageSupplier) {
checkCondition(obj != null, () -> new NullPointerException(errorMessageSupplier.get())); checkCondition(obj != null, () -> new NullPointerException(messageSupplier.get()));
} }
/** /**
@@ -257,18 +193,16 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
* @throws NullPointerException {@code obj} {@code null} 时抛出 * @throws NullPointerException {@code obj} {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, public static <T> void checkNotNull(@Nullable T obj, String format, Object... args) {
String errorMessageTemplate, Object... errorMessageArgs) { checkCondition(obj != null, () -> new NullPointerException(String.format(format, args)));
checkCondition(obj != null,
() -> new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
// ================================ // ================================
// #endregion - NotNull // #endregion
// ================================ // ================================
// ================================ // ================================
@@ -285,7 +219,7 @@ public class AssertTools {
*/ */
public static <T> T checkExists(@Nullable T obj) public static <T> T checkExists(@Nullable T obj)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, DataNotExistsException::new); checkCondition(Objects.nonNull(obj), DataNotExistsException::new);
return obj; return obj;
} }
@@ -294,13 +228,13 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errorMessage 异常信息 * @param message 异常信息
* @return 如果 {@code obj} 存在返回 {@code obj} 本身 * @return 如果 {@code obj} 存在返回 {@code obj} 本身
* @throws DataNotExistsException {@code obj} 不存在时抛出 * @throws DataNotExistsException {@code obj} 不存在时抛出
*/ */
public static <T> T checkExists(@Nullable T obj, String errorMessage) public static <T> T checkExists(@Nullable T obj, String message)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, () -> new DataNotExistsException(errorMessage)); checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(message));
return obj; return obj;
} }
@@ -309,13 +243,13 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
* @return 如果 {@code obj} 存在返回 {@code obj} 本身 * @return 如果 {@code obj} 存在返回 {@code obj} 本身
* @throws DataNotExistsException {@code obj} 不存在时抛出 * @throws DataNotExistsException {@code obj} 不存在时抛出
*/ */
public static <T> T checkExists(@Nullable T obj, Supplier<String> errorMessageSupplier) public static <T> T checkExists(@Nullable T obj, Supplier<String> messageSupplier)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, () -> new DataNotExistsException(errorMessageSupplier.get())); checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(messageSupplier.get()));
return obj; return obj;
} }
@@ -324,16 +258,14 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
* @return 如果 {@code obj} 存在返回 {@code obj} 本身 * @return 如果 {@code obj} 存在返回 {@code obj} 本身
* @throws DataNotExistsException {@code obj} 不存在时抛出 * @throws DataNotExistsException {@code obj} 不存在时抛出
*/ */
public static <T> T checkExists(@Nullable T obj, public static <T> T checkExists(@Nullable T obj, String format, Object... args)
String errorMessageTemplate, Object... errorMessageArgs)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(String.format(format, args)));
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)));
return obj; return obj;
} }
@@ -356,13 +288,13 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @param errorMessage 异常信息 * @param message 异常信息
* @return 如果 {@code optional} 存在返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在返回 {@code optional} 包含的值
* @throws DataNotExistsException {@code optional} 的值不存在时抛出 * @throws DataNotExistsException {@code optional} 的值不存在时抛出
*/ */
public static <T> T checkExists(Optional<T> optional, String errorMessage) public static <T> T checkExists(Optional<T> optional, String message)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessage)); checkCondition(optional.isPresent(), () -> new DataNotExistsException(message));
return optional.get(); return optional.get();
} }
@@ -371,13 +303,13 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
* @return 如果 {@code optional} 存在返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在返回 {@code optional} 包含的值
* @throws DataNotExistsException {@code optional} 的值不存在时抛出 * @throws DataNotExistsException {@code optional} 的值不存在时抛出
*/ */
public static <T> T checkExists(Optional<T> optional, Supplier<String> errorMessageSupplier) public static <T> T checkExists(Optional<T> optional, Supplier<String> messageSupplier)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessageSupplier.get())); checkCondition(optional.isPresent(), () -> new DataNotExistsException(messageSupplier.get()));
return optional.get(); return optional.get();
} }
@@ -386,16 +318,14 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
* @return 如果 {@code optional} 存在返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在返回 {@code optional} 包含的值
* @throws DataNotExistsException {@code optional} 的值不存在时抛出 * @throws DataNotExistsException {@code optional} 的值不存在时抛出
*/ */
public static <T> T checkExists(Optional<T> optional, public static <T> T checkExists(Optional<T> optional, String format, Object... args)
String errorMessageTemplate, Object... errorMessageArgs)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), checkCondition(optional.isPresent(), () -> new DataNotExistsException(String.format(format, args)));
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)));
return optional.get(); return optional.get();
} }
@@ -410,154 +340,140 @@ public class AssertTools {
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 期影响的行数 * @param expectedValue 计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
*/ */
public static void checkAffectedRows(int expected, int actualRowCount) { public static void checkAffectedRows(int expectedValue, int result) {
if (expected != actualRowCount) { checkAffectedRows(expectedValue, result,
throw new DataOperationResultException(expected, actualRowCount); "The number of rows affected is expected to be %d, but is: %d", expectedValue, result);
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 期影响的行数 * @param expectedValue 计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessage 异常信息 * @param message 异常信息
*/ */
public static void checkAffectedRows(int expected, int actualRowCount, public static void checkAffectedRows(int expectedValue, int result, @Nullable String message) {
@Nullable String errorMessage) { checkCondition(expectedValue == result, () -> new DataOperationResultException(message));
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount, errorMessage);
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 预期影响的行数 * @param expectedValue 预计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
*/ */
public static void checkAffectedRows(int expected, int actualRowCount, public static void checkAffectedRows(int expectedValue, int result,
Supplier<String> errorMessageSupplier) { Supplier<String> messageSupplier) {
if (expected != actualRowCount) { checkCondition(expectedValue == result,
throw new DataOperationResultException(expected, actualRowCount, errorMessageSupplier.get()); () -> new DataOperationResultException(messageSupplier.get()));
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 期影响的行数 * @param expectedValue 计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
*/ */
public static void checkAffectedRows(int expected, int actualRowCount, public static void checkAffectedRows(int expectedValue, int result,
String errorMessageTemplate, Object... errorMessageArgs) { String format, Object... args) {
if (expected != actualRowCount) { checkCondition(expectedValue == result,
throw new DataOperationResultException(expected, actualRowCount, () -> new DataOperationResultException(String.format(format, args)));
String.format(errorMessageTemplate, errorMessageArgs));
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 期影响的行数 * @param expectedValue 计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
*/ */
public static void checkAffectedRows(long expected, long actualRowCount) { public static void checkAffectedRows(long expectedValue, long result) {
if (expected != actualRowCount) { checkAffectedRows(expectedValue, result,
throw new DataOperationResultException(expected, actualRowCount); "The number of rows affected is expected to be %d, but is: %d", expectedValue, result);
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 期影响的行数 * @param expectedValue 计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessage 异常信息 * @param message 异常信息
*/ */
public static void checkAffectedRows(long expected, long actualRowCount, public static void checkAffectedRows(long expectedValue, long result, @Nullable String message) {
@Nullable String errorMessage) { checkCondition(expectedValue == result, () -> new DataOperationResultException(message));
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount, errorMessage);
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 预期影响的行数 * @param expectedValue 预计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
*/ */
public static void checkAffectedRows(long expected, long actualRowCount, public static void checkAffectedRows(long expectedValue, long result,
Supplier<String> errorMessageSupplier) { Supplier<String> messageSupplier) {
if (expected != actualRowCount) { checkCondition(expectedValue == result,
throw new DataOperationResultException(expected, actualRowCount, errorMessageSupplier.get()); () -> new DataOperationResultException(messageSupplier.get()));
}
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException} * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
* *
* @param expected 期影响的行数 * @param expectedValue 计的数量
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
*/ */
public static void checkAffectedRows(long expected, long actualRowCount, public static void checkAffectedRows(long expectedValue, long result,
String errorMessageTemplate, Object... errorMessageArgs) { String format, Object... args) {
if (expected != actualRowCount) { checkCondition(expectedValue == result,
throw new DataOperationResultException(expected, actualRowCount, () -> new DataOperationResultException(String.format(format, args)));
String.format(errorMessageTemplate, errorMessageArgs));
}
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
*/ */
public static void checkAffectedOneRow(int actualRowCount) { public static void checkAffectedOneRow(int result) {
checkAffectedRows(1, actualRowCount); checkAffectedRows(1, result,
() -> "The number of rows affected is expected to be 1, but is: " + result);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessage 异常信息 * @param message 异常信息
*/ */
public static void checkAffectedOneRow(int actualRowCount, String errorMessage) { public static void checkAffectedOneRow(int result, String message) {
checkAffectedRows(1, actualRowCount, errorMessage); checkAffectedRows(1, result, message);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
*/ */
public static void checkAffectedOneRow(int actualRowCount, Supplier<String> errorMessageSupplier) { public static void checkAffectedOneRow(int result, Supplier<String> messageSupplier) {
checkAffectedRows(1, actualRowCount, errorMessageSupplier); checkAffectedRows(1, result, messageSupplier);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
*/ */
public static void checkAffectedOneRow(int actualRowCount, public static void checkAffectedOneRow(int result, String format, Object... args) {
String errorMessageTemplate, Object... errorMessageArgs) { checkAffectedRows(1, result, format, args);
checkAffectedRows(1, actualRowCount, errorMessageTemplate, errorMessageArgs);
} }
/** /**
@@ -566,39 +482,40 @@ public class AssertTools {
* @param result 实际影响的数据量 * @param result 实际影响的数据量
*/ */
public static void checkAffectedOneRow(long result) { public static void checkAffectedOneRow(long result) {
checkAffectedRows(1L, result); checkAffectedRows(1L, result,
() -> "The number of rows affected is expected to be 1, but is: " + result);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessage 异常信息 * @param message 异常信息
*/ */
public static void checkAffectedOneRow(long actualRowCount, String errorMessage) { public static void checkAffectedOneRow(long result, String message) {
checkAffectedRows(1L, actualRowCount, errorMessage); checkAffectedRows(1L, result, message);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageSupplier 异常信息 * @param messageSupplier 异常信息
*/ */
public static void checkAffectedOneRow(long actualRowCount, Supplier<String> errorMessageSupplier) { public static void checkAffectedOneRow(long result, Supplier<String> messageSupplier) {
checkAffectedRows(1L, actualRowCount, errorMessageSupplier); checkAffectedRows(1L, result, messageSupplier);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException} * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
* *
* @param actualRowCount 实际影响的 * @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板 * @param format 异常信息模板
* @param errorMessageArgs 异常信息参数 * @param args 异常信息参数
*/ */
public static void checkAffectedOneRow(long actualRowCount, public static void checkAffectedOneRow(long result,
String errorMessageTemplate, Object... errorMessageArgs) { String format, Object... args) {
checkAffectedRows(1L, actualRowCount, errorMessageTemplate, errorMessageArgs); checkAffectedRows(1L, result, format, args);
} }
// ================================ // ================================

View File

@@ -28,6 +28,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
* *
* <p> * <p>
* BigDecimal 工具类 * BigDecimal 工具类
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0

View File

@@ -215,6 +215,7 @@ public class DateTimeTools {
* <p> * <p>
* 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上还是同一时间戳 * 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上还是同一时间戳
* 只是不同时区的表示 * 只是不同时区的表示
* </p>
* *
* @param timeMillis 时间戳 * @param timeMillis 时间戳
* @param zone 时区 * @param zone 时区
@@ -229,6 +230,7 @@ public class DateTimeTools {
* <p> * <p>
* 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上还是同一时间戳 * 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上还是同一时间戳
* 只是不同时区的表示 * 只是不同时区的表示
* </p>
* *
* @param dateTime {@link Date} 对象 * @param dateTime {@link Date} 对象
* @param zone 时区 * @param zone 时区
@@ -243,6 +245,7 @@ public class DateTimeTools {
* <p> * <p>
* 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上表示的还是还是同一时间戳的时间 * 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上表示的还是还是同一时间戳的时间
* 只是不同时区的表示 * 只是不同时区的表示
* </p>
* *
* @param dateTime {@link Date} 对象 * @param dateTime {@link Date} 对象
* @param timeZone 时区 * @param timeZone 时区

View File

@@ -33,8 +33,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
* *
* 参考 <a href="https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">Enumeration classes</a> * 参考 <a href="https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">Enumeration classes</a>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @deprecated 设计 Enumeration 的灵感来自于 .net 社区因为 C# 的枚举不带行为
* @deprecated 设计 Enumeration 的灵感来自于 .net 社区因为 C&num; 的枚举不带行为
* Java 的枚举可以带行为故大多数情况下不需要这种设计 * Java 的枚举可以带行为故大多数情况下不需要这种设计
*/ */
@Deprecated @Deprecated

View File

@@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
* *
* <p> * <p>
* 生成 UUID 修改版雪花IDSeata 版本 * 生成 UUID 修改版雪花IDSeata 版本
* </p>
* *
* @see UUID * @see UUID
* @see IdWorker * @see IdWorker

View File

@@ -34,7 +34,7 @@ import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
* <li>每个机器线程安全地生成序列前面加上机器的id这样就不会与其它机器的id相冲突</li> * <li>每个机器线程安全地生成序列前面加上机器的id这样就不会与其它机器的id相冲突</li>
* <li>时间戳作为序列的预留位它更像是应用启动时最开始的序列的一部分在一个时间戳里生成 4096 id 之后直接生成下一个时间戳的 id</li> * <li>时间戳作为序列的预留位它更像是应用启动时最开始的序列的一部分在一个时间戳里生成 4096 id 之后直接生成下一个时间戳的 id</li>
* </ol> * </ol>
* * </p>
* <p> * <p>
* 详情见以下介绍 * 详情见以下介绍
* <ul> * <ul>
@@ -43,7 +43,7 @@ import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
* <li><a href="https://juejin.cn/post/7264387737276203065">在开源项目中看到一个改良版的雪花算法现在它是你的了</a></li> * <li><a href="https://juejin.cn/post/7264387737276203065">在开源项目中看到一个改良版的雪花算法现在它是你的了</a></li>
* <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者阅读改良版雪花算法后提出的几个共性问题的回复</a></li> * <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者阅读改良版雪花算法后提出的几个共性问题的回复</a></li>
* </ul> * </ul>
* * </p>
*/ */
public class IdWorker { public class IdWorker {

View File

@@ -45,6 +45,7 @@ public class OptionalTools {
* <p> * <p>
* 包装类为 {@code null} 表示值的缺失转为 {@link OptionalInt} * 包装类为 {@code null} 表示值的缺失转为 {@link OptionalInt}
* {@link OptionalInt#empty()} 表示值的缺失 * {@link OptionalInt#empty()} 表示值的缺失
* </p>
* *
* @param value 包装对象 * @param value 包装对象
* @return {@link OptionalInt} 实例 * @return {@link OptionalInt} 实例
@@ -57,6 +58,7 @@ public class OptionalTools {
* {@code Optional<Integer>} 对象转为 {@link OptionalInt} 对象 * {@code Optional<Integer>} 对象转为 {@link OptionalInt} 对象
* <p> * <p>
* {@code Optional<Integer>} 将整数包装了两次改为使用 {@link OptionalInt} 包装其中的整数数据 * {@code Optional<Integer>} 将整数包装了两次改为使用 {@link OptionalInt} 包装其中的整数数据
* </p>
* *
* @param optionalObj {@code Optional<Integer>} 对象 * @param optionalObj {@code Optional<Integer>} 对象
* @return {@link OptionalInt} 实例 * @return {@link OptionalInt} 实例
@@ -70,6 +72,7 @@ public class OptionalTools {
* <p> * <p>
* 包装类为 {@code null} 表示值的缺失转为 {@link OptionalLong} * 包装类为 {@code null} 表示值的缺失转为 {@link OptionalLong}
* {@link OptionalLong#empty()} 表示值的缺失 * {@link OptionalLong#empty()} 表示值的缺失
* </p>
* *
* @param value 包装对象 * @param value 包装对象
* @return {@link OptionalLong} 实例 * @return {@link OptionalLong} 实例
@@ -82,6 +85,7 @@ public class OptionalTools {
* {@code Optional<Long>} 转为 {@link OptionalLong} * {@code Optional<Long>} 转为 {@link OptionalLong}
* <p> * <p>
* {@code Optional<Long>} 将整数包装了两次改为使用 {@link OptionalLong} 包装其中的整数数据 * {@code Optional<Long>} 将整数包装了两次改为使用 {@link OptionalLong} 包装其中的整数数据
* </p>
* *
* @param optionalObj 包装对象 * @param optionalObj 包装对象
* @return {@link OptionalLong} 实例 * @return {@link OptionalLong} 实例
@@ -95,6 +99,7 @@ public class OptionalTools {
* <p> * <p>
* 包装类为 {@code null} 表示值的缺失转为 {@link OptionalDouble} * 包装类为 {@code null} 表示值的缺失转为 {@link OptionalDouble}
* {@link OptionalDouble#empty()} 表示值的缺失 * {@link OptionalDouble#empty()} 表示值的缺失
* </p>
* *
* @param value 包装对象 * @param value 包装对象
* @return {@link OptionalDouble} 实例 * @return {@link OptionalDouble} 实例
@@ -107,6 +112,7 @@ public class OptionalTools {
* {@code Optional<Double>} 转为 {@link OptionalDouble} * {@code Optional<Double>} 转为 {@link OptionalDouble}
* <p> * <p>
* {@code Optional<Double>} 将整数包装了两次改为使用 {@link OptionalDouble} 包装其中的整数数据 * {@code Optional<Double>} 将整数包装了两次改为使用 {@link OptionalDouble} 包装其中的整数数据
* </p>
* *
* @param optionalObj 包装对象 * @param optionalObj 包装对象
* @return {@link OptionalDouble} 实例 * @return {@link OptionalDouble} 实例

View File

@@ -26,7 +26,7 @@ import java.util.concurrent.ThreadLocalRandom;
* 随机工具类 * 随机工具类
* <p> * <p>
* 建议调用方自行维护 Random 对象 * 建议调用方自行维护 Random 对象
* * </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */
public final class RandomTools { public final class RandomTools {

View File

@@ -17,40 +17,26 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Map;
import java.util.Optional; import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/** /**
* 封装一些常用的正则操作并可以缓存 {@link Pattern} 实例以复用 * 封装一些常用的正则操作并可以缓存 {@link Pattern} 实例以复用最多缓存大概 256
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*
*/ */
public final class RegexTools { public final class RegexTools {
private static final int DEFAULT_CACHE_INITIAL_CAPACITY = 64;
private static final int MAX_CACHE_SIZE = 256; private static final int MAX_CACHE_SIZE = 256;
private static final int DEFAULT_FLAG = 0; private static final Map<String, Pattern> PATTERN_CACHE
private static final LoadingCache<RegexAndFlags, Pattern> PATTERN_CACHE = CacheBuilder = new ConcurrentHashMap<>(DEFAULT_CACHE_INITIAL_CAPACITY);
.newBuilder()
.maximumSize(MAX_CACHE_SIZE)
.build(new CacheLoader<RegexAndFlags, Pattern>() {
@SuppressWarnings("null")
public Pattern load(@Nonnull RegexAndFlags regexAndFlags) {
return regexAndFlags.compilePattern();
}
});
// ================================
// #region - getPattern
// ================================
/** /**
* 获取 {@link Pattern} 实例 * 获取 {@link Pattern} 实例
@@ -60,20 +46,8 @@ public final class RegexTools {
* @return {@link Pattern} 实例 * @return {@link Pattern} 实例
*/ */
public static Pattern getPattern(final String pattern, final boolean cachePattern) { public static Pattern getPattern(final String pattern, final boolean cachePattern) {
return getPattern(pattern, DEFAULT_FLAG, cachePattern);
}
/**
* 获取 {@link Pattern} 实例
*
* @param pattern 正则表达式
* @param flags 正则表达式匹配标识
* @param cachePattern 是否缓存 {@link Pattern} 实例
* @return {@link Pattern} 实例
*/
public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) {
AssertTools.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags); return cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern);
} }
/** /**
@@ -83,29 +57,34 @@ public final class RegexTools {
* @return {@link Pattern} 实例 * @return {@link Pattern} 实例
*/ */
public static Pattern getPattern(final String pattern) { public static Pattern getPattern(final String pattern) {
return getPattern(pattern, DEFAULT_FLAG); AssertTools.checkNotNull(pattern);
return getPatternInternal(pattern);
} }
/** /**
* 获取 {@link Pattern} 实例不缓存 * 将各个正则表达式转为 {@link Pattern} 实例
* *
* @param pattern 正则表达式 * @param patterns 正则表达式
* @param flags 正则表达式匹配标识 * @param cachePattern 是否缓存 {@link Pattern} 实例
* @return {@link Pattern} 实例 * @return {@link Pattern} 实例数组
*/ */
@Nonnull public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) {
public static Pattern getPattern(final String pattern, final int flags) { AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
AssertTools.checkNotNull(pattern); return cachePattern
return getPatternInternal(pattern, flags); ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns);
} }
// ================================ /**
// #endregion - getPattern * 将各个正则表达式转为 {@link Pattern} 实例不缓存
// ================================ *
* @param patterns 正则表达式
// ================================ * @return {@link Pattern} 实例数组
// #region - matches */
// ================================ public static Pattern[] getPatterns(final String[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return getPatternsInternal(patterns);
}
/** /**
* 判断 {@code input} 是否匹配 {@code pattern} * 判断 {@code input} 是否匹配 {@code pattern}
@@ -126,9 +105,9 @@ public final class RegexTools {
* @param patterns 正则 * @param patterns 正则
* @return 判断结果 * @return 判断结果
*/ */
public static boolean matchesAny(@Nullable final CharSequence input, final Pattern[] patterns) { public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns)); AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return matchesAnyInternal(input, patterns); return matchesOneInternal(input, patterns);
} }
/** /**
@@ -153,21 +132,11 @@ public final class RegexTools {
*/ */
public static boolean matches(@Nullable final CharSequence input, final String pattern, public static boolean matches(@Nullable final CharSequence input, final String pattern,
final boolean cachePattern) { final boolean cachePattern) {
return matches(input, pattern, DEFAULT_FLAG, cachePattern); AssertTools.checkNotNull(pattern);
} Pattern p = cachePattern
? cacheAndGetPatternInternal(pattern)
/** : getPatternInternal(pattern);
* 判断 {@code input} 是否匹配 {@code pattern} return matchesInternal(input, p);
*
* @param input 输入
* @param pattern 正则表达式
* @param flags 正则表达式匹配标识
* @param cachePattern 是否缓存 {@link Pattern} 实例
* @return 判断结果
*/
public static boolean matches(@Nullable final CharSequence input, final String pattern, final int flags,
final boolean cachePattern) {
return matchesInternal(input, getPattern(pattern, flags, cachePattern));
} }
/** /**
@@ -178,29 +147,69 @@ public final class RegexTools {
* @return 判断结果 * @return 判断结果
*/ */
public static boolean matches(@Nullable final CharSequence input, final String pattern) { public static boolean matches(@Nullable final CharSequence input, final String pattern) {
return matches(input, pattern, DEFAULT_FLAG); AssertTools.checkNotNull(pattern);
return matchesInternal(input, getPatternInternal(pattern));
} }
/** /**
* 判断 {@code input} 是否匹配 {@code pattern}不缓存 {@link Pattern} 实例 * 判断 {@code input} 是否匹配 {@code patterns} 中的一个
* *
* @param input 输入 * @param input 输入
* @param pattern 正则表达式 * @param patterns 正则表达式
* @param flags 正则表达式匹配标识 * @param cachePattern 是否缓存 {@link Pattern} 实例
* @return 判断结果 * @return 判断结果
*/ */
public static boolean matches(@Nullable final CharSequence input, public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns,
final String pattern, final int flags) { final boolean cachePattern) {
return matchesInternal(input, getPattern(pattern, flags)); AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
final Pattern[] patternSet = cachePattern
? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns);
return matchesOneInternal(input, patternSet);
} }
// ================================ /**
// #endregion - matches * 判断 {@code input} 是否匹配 {@code patterns} 中的一个不缓存 {@link Pattern} 实例
// ================================ *
* @param input 输入
* @param patterns 正则表达式
* @return 判断结果
*/
public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
final Pattern[] patternSet = getPatternsInternal(patterns);
return matchesOneInternal(input, patternSet);
}
// ================================ /**
// #region - getMatcher * 判断 {@code input} 是否匹配全部正则
// ================================ *
* @param input 输入
* @param patterns 正则表达式
* @param cachePattern 是否缓存 {@link Pattern} 实例
* @return 判断结果
*/
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
final Pattern[] patternSet = cachePattern
? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns);
return matchesAllInternal(input, patternSet);
}
/**
* 判断 {@code input} 是否匹配全部正则不缓存 {@link Pattern} 实例
*
* @param input 输入
* @param patterns 正则表达式
* @return 判断结果
*/
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
final Pattern[] patternSet = getPatternsInternal(patterns);
return matchesAllInternal(input, patternSet);
}
/** /**
* 生成 Matcher * 生成 Matcher
@@ -224,21 +233,12 @@ public final class RegexTools {
* @return 结果 * @return 结果
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) { public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) {
return getMatcher(input, pattern, DEFAULT_FLAG, cachePattern); AssertTools.checkNotNull(input);
} AssertTools.checkNotNull(pattern);
final Pattern p = cachePattern
/** ? cacheAndGetPatternInternal(pattern)
* 生成 Matcher : getPatternInternal(pattern);
* return p.matcher(input);
* @param input 输入
* @param pattern 正则表达式
* @param flags 正则表达式匹配标识
* @param cachePattern 是否缓存 {@link Pattern} 实例
* @return 结果
*/
public static Matcher getMatcher(final CharSequence input,
final String pattern, final int flags, boolean cachePattern) {
return getMatcher(input, getPattern(pattern, flags, cachePattern));
} }
/** /**
@@ -249,56 +249,70 @@ public final class RegexTools {
* @return 结果 * @return 结果
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern) { public static Matcher getMatcher(final CharSequence input, final String pattern) {
return getMatcher(input, pattern, DEFAULT_FLAG);
}
/**
* 生成 Matcher不缓存 {@link Pattern} 实例
*
* @param input 输入
* @param pattern 正则表达式
* @param flags 正则表达式匹配标识
* @return 结果
*/
public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) {
AssertTools.checkNotNull(input); AssertTools.checkNotNull(input);
AssertTools.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return getPatternInternal(pattern, flags).matcher(input); return getPatternInternal(pattern).matcher(input);
} }
// ================================ // ========== internal methods ==========
// #endregion - getMatcher
// ================================
// ================================
// #region - internal methods
// ================================
/** /**
* 获取 {@link Pattern} 实例 * 获取 {@link Pattern} 实例
* *
* @param pattern 正则表达式 * @param pattern 正则表达式
* @param flags 正则表达式匹配标识
* @return {@link Pattern} 实例 * @return {@link Pattern} 实例
*/ */
@Nonnull @Nonnull
private static Pattern cacheAndGetPatternInternal(final String pattern, final int flags) { private static Pattern cacheAndGetPatternInternal(final String pattern) {
final RegexAndFlags regexAndFlags = new RegexAndFlags(pattern, flags); if (PATTERN_CACHE.size() < MAX_CACHE_SIZE) {
return PATTERN_CACHE.getUnchecked(regexAndFlags); return PATTERN_CACHE.computeIfAbsent(pattern, Pattern::compile);
}
Pattern result = PATTERN_CACHE.get(pattern);
if (result != null) {
return result;
}
return Pattern.compile(pattern);
} }
/** /**
* 获取 {@link Pattern} 实例不缓存 * 获取 {@link Pattern} 实例不缓存
* *
* @param pattern 正则表达式 * @param pattern 正则表达式
* @param flags 正则表达式匹配标识
* @return {@link Pattern} 实例 * @return {@link Pattern} 实例
*/ */
@Nonnull @Nonnull
private static Pattern getPatternInternal(final String pattern, final int flags) { private static Pattern getPatternInternal(final String pattern) {
final RegexAndFlags regexAndFlags = new RegexAndFlags(pattern, flags); Pattern result = PATTERN_CACHE.get(pattern);
return Optional.ofNullable(PATTERN_CACHE.getIfPresent(regexAndFlags)) if (result == null) {
.orElseGet(regexAndFlags::compilePattern); result = Pattern.compile(pattern);
}
return result;
}
/**
* 将各个正则表达式转为 {@link Pattern} 实例
*
* @param patterns 正则表达式
* @return {@link Pattern} 实例数组
*/
@Nonnull
private static Pattern[] cacheAndGetPatternsInternal(final String[] patterns) {
return Arrays.stream(patterns)
.map(RegexTools::cacheAndGetPatternInternal)
.toArray(Pattern[]::new);
}
/**
* 将各个正则表达式转为 {@link Pattern} 实例
*
* @param patterns 正则表达式
* @return {@link Pattern} 实例数组
*/
@Nonnull
private static Pattern[] getPatternsInternal(final String[] patterns) {
return Arrays.stream(patterns)
.map(RegexTools::getPatternInternal)
.toArray(Pattern[]::new);
} }
/** /**
@@ -319,7 +333,7 @@ public final class RegexTools {
* @param patterns 正则表达式 * @param patterns 正则表达式
* @return 判断结果 * @return 判断结果
*/ */
private static boolean matchesAnyInternal(@Nullable final CharSequence input, final Pattern[] patterns) { private static boolean matchesOneInternal(@Nullable final CharSequence input, final Pattern[] patterns) {
return input != null return input != null
&& Arrays.stream(patterns) && Arrays.stream(patterns)
.anyMatch(pattern -> pattern.matcher(input).matches()); .anyMatch(pattern -> pattern.matcher(input).matches());
@@ -338,49 +352,8 @@ public final class RegexTools {
.allMatch(pattern -> pattern.matcher(input).matches()); .allMatch(pattern -> pattern.matcher(input).matches());
} }
// ================================
// #endregion - internal methods
// ================================
private RegexTools() { private RegexTools() {
// 不允许实例化 // 不允许实例化
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
// ================================
// #region - RegexAndFlags
// ================================
private static final class RegexAndFlags {
private final String regex;
private final int flags;
private RegexAndFlags(String regex, int flags) {
this.regex = regex;
this.flags = flags;
}
private final Pattern compilePattern() {
return Pattern.compile(regex, flags);
}
@Override
public int hashCode() {
return Objects.hash(regex, flags);
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj)
return true;
if (!(obj instanceof RegexAndFlags))
return false;
RegexAndFlags other = (RegexAndFlags) obj;
return Objects.equals(regex, other.regex) && flags == other.flags;
}
}
// ================================
// #endregion - RegexAndFlags
// ================================
} }

View File

@@ -31,6 +31,7 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
* *
* <p> * <p>
* 字符串工具类 * 字符串工具类
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0
@@ -219,23 +220,6 @@ public class StringTools {
return String.valueOf(charArray); return String.valueOf(charArray);
} }
/**
* 转换为带引号的字符串
*
* @param value
* @return 带引号的字符串
* @since 1.1.0
*/
public static String toQuotedString(@Nullable String value) {
if (value == null) {
return "null";
}
if (value.isEmpty()) {
return "\"\"";
}
return "\"" + value + "\"";
}
private StringTools() { private StringTools() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

View File

@@ -18,6 +18,7 @@
* <h2>工具类</h2> * <h2>工具类</h2>
* <p> * <p>
* 包含树构建器{@link TreeBuilder}断言工具{@link AssertTools}ID 生成器{@link IdGenerator}及其它实用工具类 * 包含树构建器{@link TreeBuilder}断言工具{@link AssertTools}ID 生成器{@link IdGenerator}及其它实用工具类
* </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */

View File

@@ -558,11 +558,11 @@ class CustomUnifiedResponseFactoryTests {
public static final String SUCCESS_CODE = "0000000"; public static final String SUCCESS_CODE = "0000000";
public static final String DEFAULT_SUCCESS_MSG = "成功"; public static final String DEFAULT_SUCCESS_MSG = "成功";
public static <T> UnifiedResponse<T> success() { public static UnifiedResponse<Void> success() {
return of(SUCCESS_CODE, DEFAULT_SUCCESS_MSG); return of(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
} }
public static <T> UnifiedResponse<T> success(@Nullable String message) { public static UnifiedResponse<Void> success(@Nullable String message) {
return of(SUCCESS_CODE, message); return of(SUCCESS_CODE, message);
} }

View File

@@ -400,7 +400,7 @@ class DateTimeToolsTests {
// ================================ // ================================
// ================================ // ================================
// #region - ZoneId <--> DateTimeZone // #region - ZondId <--> DateTimeZone
// ================================ // ================================
@Test @Test
@@ -412,7 +412,7 @@ class DateTimeToolsTests {
} }
// ================================ // ================================
// #endregion - ZoneId <--> DateTimeZone // #endregion - ZondId <--> DateTimeZone
// ================================ // ================================
// ================================ // ================================

Some files were not shown because too many files have changed in this diff Show More