Merge branches 'dev' and 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-commons into dev

feature/net-util
ZhouXY108 2023-07-19 00:32:13 +08:00
commit 2086f22e63
3 changed files with 88 additions and 1 deletions

View File

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

View File

@ -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
*
* <p>
* RequestParam pageNumber size
* 使 {@link PagingParams#of(Long pageNumber, Integer size)} PagingParams ,
*
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
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 + "]";
}
}

View File

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