refactor(mingli-utils):重构并添加多个实用工具类- 重构 AutoConfiguration 类,优化启动时打印逻辑
All checks were successful
Gitea Actions Build / Build (push) Successful in 53s

- 新增 OsPath 类,提供跨平台路径操作支持
- 新增 VersionUtils 类,实现版本号比较功能- 新增 P1R1Function 接口,简化函数式编程
- 优化 Base64Utils、Lists、NetWorkUtil、StringUtil 和 SystemUtil 类
- 更新项目版本号至 2.0.4
This commit is contained in:
Armamem0t 2025-09-05 17:56:00 +08:00
parent 0d15cbbcc0
commit 49c3b76250
Signed by: minglipro
GPG Key ID: 5F355A77B22AA93B
12 changed files with 16252 additions and 41 deletions

View File

@ -1,4 +1,4 @@
JDKVERSIONS=1.8
GROUPSID=com.mingliqiye.utils
ARTIFACTID=mingli-utils
VERSIONS=2.0.0
VERSIONS=2.0.4

View File

@ -169,4 +169,8 @@ public class Base64Utils {
System.arraycopy(bytes, offset, data, 0, length);
return encode(data);
}
public static String encodeBytes(byte[] bytes) {
return encodeBytes(bytes, 0, bytes.length);
}
}

View File

@ -0,0 +1,23 @@
package com.mingliqiye.utils.callback;
/**
* PRFunction接口表示一个接收参数P并返回结果R的函数式接口
* <p>
* 该接口使用@表明它是一个函数式接口
* 只包含一个抽象方法FunctionalInterface注解标记call可用于Lambda表达式和方法引用
* </p>
*
* @author MingLiPro
* @param <P> 函数接收的参数类型
* @param <R> 函数返回的结果类型
*/
@FunctionalInterface
public interface P1R1Function<P, R> {
/**
* 执行函数调用
*
* @param p 输入参数
* @return 函数执行结果
*/
R call(P p);
}

View File

@ -1,9 +1,10 @@
package com.mingliqiye.utils.collection;
import java.util.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* Lists工具类提供了一系列创建List实现的便捷方法
*
@ -247,6 +248,13 @@ public class Lists {
return newList.toArray(new String[0]);
}
/**
* 将列表转换为数组
*
* @param ts 要转换的列表
* @param <T> 数组元素的类型
* @return 包含列表中所有元素的数组如果列表为null则返回null
*/
@Nullable
public static <T> T[] toArray(List<T> ts) {
if (ts == null) {
@ -258,4 +266,161 @@ public class Lists {
});
return items;
}
/**
* 将数组转换为列表
*
* @param ts 要转换的数组
* @param <T> 列表元素的类型
* @return 包含数组中所有元素的列表如果数组为null则返回null
*/
// 原始的方法 - 用于引用类型
@Nullable
public static <T> List<T> toList(T[] ts) {
return ts == null ? null : new ArrayList<>(Arrays.asList(ts));
}
/**
* 将int数组转换为Integer列表
*
* @param array 要转换的int数组
* @return 包含数组中所有元素的Integer列表如果数组为null则返回null
*/
@Nullable
public static List<Integer> toList(int[] array) {
if (array == null) {
return null;
}
List<Integer> list = new ArrayList<>(array.length);
for (int value : array) {
list.add(value);
}
return list;
}
/**
* 将long数组转换为Long列表
*
* @param array 要转换的long数组
* @return 包含数组中所有元素的Long列表如果数组为null则返回null
*/
@Nullable
public static List<Long> toList(long[] array) {
if (array == null) {
return null;
}
List<Long> list = new ArrayList<>(array.length);
for (long value : array) {
list.add(value);
}
return list;
}
/**
* 将double数组转换为Double列表
*
* @param array 要转换的double数组
* @return 包含数组中所有元素的Double列表如果数组为null则返回null
*/
@Nullable
public static List<Double> toList(double[] array) {
if (array == null) {
return null;
}
List<Double> list = new ArrayList<>(array.length);
for (double value : array) {
list.add(value);
}
return list;
}
/**
* 将float数组转换为Float列表
*
* @param array 要转换的float数组
* @return 包含数组中所有元素的Float列表如果数组为null则返回null
*/
@Nullable
public static List<Float> toList(float[] array) {
if (array == null) {
return null;
}
List<Float> list = new ArrayList<>(array.length);
for (float value : array) {
list.add(value);
}
return list;
}
/**
* 将boolean数组转换为Boolean列表
*
* @param array 要转换的boolean数组
* @return 包含数组中所有元素的Boolean列表如果数组为null则返回null
*/
@Nullable
public static List<Boolean> toList(boolean[] array) {
if (array == null) {
return null;
}
List<Boolean> list = new ArrayList<>(array.length);
for (boolean value : array) {
list.add(value);
}
return list;
}
/**
* 将char数组转换为Character列表
*
* @param array 要转换的char数组
* @return 包含数组中所有元素的Character列表如果数组为null则返回null
*/
@Nullable
public static List<Character> toList(char[] array) {
if (array == null) {
return null;
}
List<Character> list = new ArrayList<>(array.length);
for (char value : array) {
list.add(value);
}
return list;
}
/**
* 将byte数组转换为Byte列表
*
* @param array 要转换的byte数组
* @return 包含数组中所有元素的Byte列表如果数组为null则返回null
*/
@Nullable
public static List<Byte> toList(byte[] array) {
if (array == null) {
return null;
}
List<Byte> list = new ArrayList<>(array.length);
for (byte value : array) {
list.add(value);
}
return list;
}
/**
* 将short数组转换为Short列表
*
* @param array 要转换的short数组
* @return 包含数组中所有元素的Short列表如果数组为null则返回null
*/
@Nullable
public static List<Short> toList(short[] array) {
if (array == null) {
return null;
}
List<Short> list = new ArrayList<>(array.length);
for (short value : array) {
list.add(value);
}
return list;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,9 @@ import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.LongByReference;
/**
* @author MingLiPro
*/
public interface WinKernel32 extends Library {
static WinKernel32 load() {
return Native.load("kernel32", WinKernel32.class);

View File

@ -1,10 +0,0 @@
package com.mingliqiye.utils.network;
public class NetWorkUtil {
public static void main(String[] args) {
System.out.println(NetworkEndpoint.of("127.0.0.1", 25565));
System.out.println(NetworkEndpoint.of("127.0.0.1:25565"));
System.out.println(NetworkEndpoint.of("127.0.0.1:25565"));
}
}

View File

@ -0,0 +1,194 @@
package com.mingliqiye.utils.path;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
import org.jetbrains.annotations.NotNull;
public class OsPath implements Path {
private final Path path;
private OsPath(Path path) {
this.path = path;
}
public static OsPath of(String path) {
return of(Paths.get(path));
}
public static OsPath of(Path path) {
return new OsPath(path);
}
@Override
public @NotNull FileSystem getFileSystem() {
return path.getFileSystem();
}
@Override
public boolean isAbsolute() {
return path.isAbsolute();
}
@Override
public Path getRoot() {
return path.getRoot();
}
@Override
public Path getFileName() {
return path.getFileName();
}
@Override
public Path getParent() {
Path a = path.getParent();
if (a == null) {
a = path.toAbsolutePath().getParent();
}
return a;
}
@Override
public int getNameCount() {
return path.getNameCount();
}
@Override
public @NotNull Path getName(int index) {
return path.getName(index);
}
@Override
public @NotNull Path subpath(int beginIndex, int endIndex) {
return path.subpath(beginIndex, endIndex);
}
@Override
public boolean startsWith(@NotNull Path other) {
return path.startsWith(other);
}
@Override
public boolean startsWith(@NotNull String other) {
return path.startsWith(other);
}
@Override
public boolean endsWith(@NotNull Path other) {
return path.endsWith(other);
}
@Override
public boolean endsWith(@NotNull String other) {
return path.endsWith(other);
}
@Override
public @NotNull Path normalize() {
return path.normalize();
}
@Override
public @NotNull Path resolve(@NotNull Path other) {
return path.resolve(other);
}
@Override
public @NotNull Path resolve(@NotNull String other) {
return path.resolve(other);
}
@Override
public @NotNull Path resolveSibling(@NotNull Path other) {
return path.resolveSibling(other);
}
@Override
public @NotNull Path resolveSibling(@NotNull String other) {
return path.resolveSibling(other);
}
@Override
public @NotNull Path relativize(@NotNull Path other) {
return path.relativize(other);
}
@Override
public @NotNull URI toUri() {
return path.toUri();
}
@Override
public @NotNull Path toAbsolutePath() {
return path.toAbsolutePath();
}
@Override
public @NotNull Path toRealPath(@NotNull LinkOption... options)
throws IOException {
return path.toRealPath(options);
}
@Override
public @NotNull File toFile() {
return path.toFile();
}
@Override
public @NotNull WatchKey register(
@NotNull WatchService watcher,
WatchEvent.@NotNull Kind<?>[] events,
@NotNull WatchEvent.Modifier... modifiers
) throws IOException {
return path.register(watcher, events, modifiers);
}
@Override
public @NotNull WatchKey register(
@NotNull WatchService watcher,
WatchEvent.@NotNull Kind<?>... events
) throws IOException {
return path.register(watcher, events);
}
@Override
public @NotNull Iterator<@NotNull Path> iterator() {
return path.iterator();
}
@Override
public int compareTo(@NotNull Path other) {
return path.compareTo(other);
}
@Override
public boolean equals(Object other) {
return path.equals(other);
}
@Override
public int hashCode() {
return path.hashCode();
}
@Override
public @NotNull String toString() {
return path.toString();
}
@Override
public void forEach(Consumer<? super Path> action) {
path.forEach(action);
}
@Override
public Spliterator<Path> spliterator() {
return path.spliterator();
}
}

View File

@ -10,7 +10,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
@ -23,7 +22,7 @@ import java.io.InputStream;
@ComponentScan({ SpringBeanUtil.PACKAGE_NAME })
public class AutoConfiguration {
private static String banner =
private static final String banner =
"---------------------------------------------------------\n" +
"| $$\\ $$\\ $$\\ $$\\ $$\\ $$$$$$$$\\ $$$$$$\\ |\n" +
"| $$$\\ $$$ |$$ | $$ | $$ |\\__$$ __|$$ __$$\\ |\n" +
@ -33,6 +32,7 @@ public class AutoConfiguration {
"| $$ |\\$ /$$ |$$ | $$ | $$ | $$ | $$\\ $$ | |\n" +
"| $$ | \\_/ $$ |$$$$$$$$\\\\$$$$$$ | $$ | \\$$$$$$ | |\n" +
"| \\__| \\__|\\________|\\______/ \\__| \\______/ |\n";
private static String banner2;
private boolean isloadObjMapper;
public AutoConfiguration() throws IOException {
@ -48,9 +48,9 @@ public class AutoConfiguration {
"Jackson ObjectMapper not found in classpath. Jackson serialization features will be disabled."
);
}
init();
}
@PostConstruct
public void init() {
if (isloadObjMapper) {
log.info("init ObjectMapper");
@ -66,6 +66,7 @@ public class AutoConfiguration {
}
public void print() throws IOException {
banner2 = banner;
InputStream inputStream = AutoConfiguration.class.getResourceAsStream(
"/META-INF/meta-data"
);
@ -87,14 +88,14 @@ public class AutoConfiguration {
for (int ia = 0; ia < spacesNeeded; ia++) {
da.append(" ");
}
banner += da + "|\n";
banner2 += da + "|\n";
} else {
banner += content.substring(0, targetLength) + "|\n";
banner2 += content.substring(0, targetLength) + "|\n";
}
}
});
System.out.printf(
banner,
banner2,
DateTime.now().format(Formatter.STANDARD_DATETIME_MILLISECOUND7)
);
System.out.println(

View File

@ -1,5 +1,6 @@
package com.mingliqiye.utils.string;
import com.mingliqiye.utils.callback.P1R1Function;
import com.mingliqiye.utils.collection.Lists;
import java.text.MessageFormat;
import java.util.ArrayList;
@ -136,7 +137,7 @@ public class StringUtil {
public static <P> String join(
String separator,
List<P> list,
PRFunction<P, String> fun
P1R1Function<P, String> fun
) {
// 处理空列表情况
if (list == null || list.isEmpty()) {
@ -189,25 +190,4 @@ public class StringUtil {
public static StringBuilder stringBuilder(int i) {
return new StringBuilder(i);
}
/**
* PRFunction接口表示一个接收参数P并返回结果R的函数式接口
* <p>
* 该接口使用@FunctionalInterface注解标记表明它是一个函数式接口
* 只包含一个抽象方法call可用于Lambda表达式和方法引用
* </p>
*
* @param <P> 函数接收的参数类型
* @param <R> 函数返回的结果类型
*/
@FunctionalInterface
public interface PRFunction<P, R> {
/**
* 执行函数调用
*
* @param p 输入参数
* @return 函数执行结果
*/
R call(P p);
}
}

View File

@ -50,7 +50,9 @@ public class SystemUtil {
return (
osName.startsWith("Linux") ||
osName.startsWith("AIX") ||
osName.startsWith("SunOS")
osName.startsWith("SunOS") ||
osName.startsWith("Mac OS X") ||
osName.startsWith("FreeBSD")
);
}

View File

@ -0,0 +1,80 @@
package com.mingliqiye.utils.version;
import java.util.Objects;
/**
* 版本工具类提供版本比较相关的方法
*/
public class VersionUtils {
/**
* 判断当前版本是否比另一个版本新
*
* @param now 当前版本号字符串格式如"1.0.0"
* @param other 要比较的版本号字符串格式如"2.0.0"
* @return 如果当前版本更新则返回true否则返回false
*/
public static boolean isNew(String now, String other) {
String[] currentParts = now.split("\\.");
String[] minimumParts = other.split("\\.");
for (
int i = 0;
i < Math.max(currentParts.length, minimumParts.length);
i++
) {
int currentNum = i < currentParts.length
? Integer.parseInt(currentParts[i])
: 0;
int minimumNum = i < minimumParts.length
? Integer.parseInt(minimumParts[i])
: 0;
if (currentNum < minimumNum) {
return true;
} else if (currentNum > minimumNum) {
return false;
}
}
return false;
}
/**
* 判断当前版本是否比另一个版本旧
*
* @param now 当前版本号字符串格式如"1.0.0"
* @param other 要比较的版本号字符串格式如"2.0.0"
* @return 如果当前版本更旧则返回true否则返回false
*/
public static boolean isOld(String now, String other) {
return !equals(now, other) && !isNew(now, other);
}
/**
* 判断两个版本号是否相等
*
* @param now 当前版本号字符串格式如"1.0.0"
* @param other 要比较的版本号字符串格式如"2.0.0"
* @return 如果两个版本号相等则返回true否则返回false
*/
public static boolean equals(String now, String other) {
return Objects.equals(now, other);
}
/**
* 比较两个版本号的大小关系
*
* @param now 当前版本号字符串格式如"1.0.0"
* @param other 要比较的版本号字符串格式如"2.0.0"
* @return 如果当前版本更新返回2如果更旧返回0如果相等返回1
*/
public static byte is(String now, String other) {
if (isNew(now, other)) {
return 2;
} else if (isOld(now, other)) {
return 0;
} else {
return 1;
}
}
}