From 7208aa5aae9a22a7ee10a3fdb94e1d97c19a0873 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 18 Jul 2023 15:29:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/RandomUtil.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java new file mode 100644 index 0000000..9375728 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java @@ -0,0 +1,18 @@ +package xyz.zhouxy.plusone.commons.util; + +import java.security.SecureRandom; + +public final class RandomUtil { + private RandomUtil() { + throw new IllegalStateException("Utility class"); + } + + public static String secureRandomStr(char[] sourceCharacters, int length) { + SecureRandom random = new SecureRandom(); + char[] result = new char[length]; + for (int i = 0; i < length; i++) { + result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)]; + } + return String.valueOf(result); + } +} From 87ef8b4612ecc7720e1947966fa4b5437de65d83 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 18 Jul 2023 18:02:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=81=E7=9A=84?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java b/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java index e40a988..76f73f7 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java @@ -49,7 +49,8 @@ public class IdGenerator { public static SnowflakeIdGenerator getSnowflakeIdGenerator(long workerId, long datacenterId) { SnowflakeIdGenerator generator = snowflakePool.get(workerId, datacenterId); if (generator == null) { - synchronized (IdGenerator.class) { + // 其它地方需注意,对 snowflakePool 的操作,也都锁 snowflakePool 对象。 + synchronized (snowflakePool) { generator = snowflakePool.get(workerId, datacenterId); if (generator == null) { generator = new SnowflakeIdGenerator(workerId, datacenterId); From b5ec1d4c5d5058d3fc9a357e6373dde8282787bd Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 18 Jul 2023 18:25:15 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20PagingParams=20?= =?UTF-8?q?=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/PagingParams.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/PagingParams.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingParams.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingParams.java new file mode 100644 index 0000000..a636ad1 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingParams.java @@ -0,0 +1,68 @@ +/* + * Copyright 2022-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package xyz.zhouxy.plusone.commons.util; + +import javax.annotation.Nullable; + +import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; + +/** + * PageInfo + * + *

+ * 推荐的实现是,请求处理方法用两个 RequestParam 分别接收请求中的 pageNumber 和 size, + * 应用服务使用 {@link PagingParams#of(Long pageNumber, Integer size)} 构建 PagingParams 对象, + * 用于查询数据库。 + *

+ * + * @author ZhouXY + */ +public class PagingParams { + private final long pageNumber; + private final int size; + private final long offset; + + private PagingParams(long pageNumber, int size) { + this.pageNumber = pageNumber; + this.size = size; + this.offset = (pageNumber - 1) * size; + } + + @StaticFactoryMethod(PagingParams.class) + public static PagingParams of(@Nullable Long pageNumber, @Nullable Integer size) { + pageNumber = pageNumber == null ? 1 : pageNumber; + size = size == null ? 15 : size; + return new PagingParams(pageNumber, size); + } + + public long getPageNumber() { + return pageNumber; + } + + public int getSize() { + return size; + } + + public long getOffset() { + return offset; + } + + @Override + public String toString() { + return "PageInfo [pageNumber=" + pageNumber + ", size=" + size + "]"; + } +}