feat(utils): 新增 Base64Utils 工具类和 ForEach 增强遍历工具- 新增 Base64Utils工具类,提供 Base64 编码和解码功能- 新增 ForEach 工具类,增强集合和映射的遍历功能
Some checks failed
Gitea Actions Build / Build (push) Has been cancelled

- 优化 Collection 工具类中的 sublist 方法
This commit is contained in:
Armamem0t 2025-07-30 14:39:50 +08:00
parent f2f989b7a3
commit d58a1063c1
Signed by: minglipro
GPG Key ID: 5F355A77B22AA93B
3 changed files with 373 additions and 1 deletions

View File

@ -0,0 +1,172 @@
package com.mingliqiye.utils.base64;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
/**
* Base64工具类提供对字节数组文件和字符串的Base64编码与解码功能
*/
public class Base64Utils {
// Base64编码器实例
private static final Base64.Encoder BASE_64_ENCODER = Base64.getEncoder();
// Base64解码器实例
private static final Base64.Decoder BASE_64_DECODER = Base64.getDecoder();
/**
* 对字节数组进行Base64编码
*
* @param bytes 待编码的字节数组
* @return 编码后的Base64字符串
*/
public static String encode(byte[] bytes) {
return BASE_64_ENCODER.encodeToString(bytes);
}
/**
* 对文件内容进行Base64编码
*
* @param file 待编码的文件对象
* @return 编码后的Base64字符串
* @throws RuntimeException 如果读取文件时发生IO异常
*/
public static String encode(File file) {
try {
byte[] bytes = java.nio.file.Files.readAllBytes(file.toPath());
return encode(bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 根据文件路径对文件内容进行Base64编码
*
* @param filePath 文件路径
* @return 编码后的Base64字符串
*/
public static String encode(String filePath) {
return encode(new File(filePath));
}
/**
* 安全地对文件内容进行Base64编码出错时返回null
*
* @param file 待编码的文件对象
* @return 编码后的Base64字符串出错时返回null
*/
public static String encodeSafe(File file) {
try {
return encode(file);
} catch (Exception e) {
return null;
}
}
/**
* 安全地根据文件路径对文件内容进行Base64编码出错时返回null
*
* @param filePath 文件路径
* @return 编码后的Base64字符串出错时返回null
*/
public static String encodeSafe(String filePath) {
try {
return encode(filePath);
} catch (Exception e) {
return null;
}
}
/**
* 对Base64字符串进行解码
*
* @param base64 待解码的Base64字符串
* @return 解码后的字节数组
*/
public static byte[] decode(String base64) {
return BASE_64_DECODER.decode(base64);
}
/**
* 安全地对Base64字符串进行解码出错时返回null
*
* @param base64 待解码的Base64字符串
* @return 解码后的字节数组出错时返回null
*/
public static byte[] decodeSafe(String base64) {
try {
return decode(base64);
} catch (Exception e) {
return null;
}
}
/**
* 将Base64字符串解码并写入指定文件
*
* @param base64 待解码的Base64字符串
* @param file 目标文件对象
* @throws RuntimeException 如果写入文件时发生IO异常
*/
public static void decodeToFile(String base64, File file) {
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] bytes = decode(base64);
fos.write(bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 将Base64字符串解码并写入指定路径的文件
*
* @param base64 待解码的Base64字符串
* @param filePath 目标文件路径
*/
public static void decodeToFile(String base64, String filePath) {
decodeToFile(base64, new File(filePath));
}
/**
* 安全地将Base64字符串解码并写入指定文件出错时返回false
*
* @param base64 待解码的Base64字符串
* @param file 目标文件对象
* @return 成功写入返回true否则返回false
*/
public static boolean decodeToFileSafe(String base64, File file) {
try {
decodeToFile(base64, file);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 安全地将Base64字符串解码并写入指定路径的文件出错时返回false
*
* @param base64 待解码的Base64字符串
* @param filePath 目标文件路径
* @return 成功写入返回true否则返回false
*/
public static boolean decodeToFileSafe(String base64, String filePath) {
return decodeToFileSafe(base64, new File(filePath));
}
/**
* 对字节数组中指定范围的数据进行Base64编码
*
* @param bytes 源字节数组
* @param offset 起始偏移量
* @param length 要编码的数据长度
* @return 编码后的Base64字符串
*/
public static String encodeBytes(byte[] bytes, int offset, int length) {
byte[] data = new byte[length];
System.arraycopy(bytes, offset, data, 0, length);
return encode(data);
}
}

View File

@ -160,7 +160,6 @@ public class Collection {
// 调整边界 // 调整边界
fromIndex = Math.max(0, fromIndex); fromIndex = Math.max(0, fromIndex);
toIndex = Math.min(size, toIndex); toIndex = Math.min(size, toIndex);
if (fromIndex >= toIndex) { if (fromIndex >= toIndex) {
return Collections.emptyList(); return Collections.emptyList();
} }

View File

@ -0,0 +1,201 @@
package com.mingliqiye.utils.collection;
import java.util.*;
import java.util.Collection;
import java.util.concurrent.ConcurrentMap;
/**
* ListsAMaps 工具类提供对集合和映射的增强遍历功能
* 包含多个重载的 forEach 方法支持带索引的遍历操作
*/
public class ForEach {
/**
* 对给定的集合执行指定的操作操作包含元素值和索引
* 根据集合类型选择最优的遍历方式以提高性能
*
* @param collection 要遍历的集合可以是 List 或其他 Collection 实现
* @param action 要对每个元素执行的操作接收元素值和索引作为参数
* @param <T> 集合中元素的类型
*/
public static <T> void forEach(
Collection<T> collection,
ForEach.Consumer<? super T> action
) {
// 参数校验如果集合或操作为空则直接返回
if (collection == null || action == null) {
return;
}
// 如果集合实现了 RandomAccess 接口 ArrayList使用索引访问优化性能
if (collection instanceof RandomAccess && collection instanceof List) {
List<T> list = (List<T>) collection;
for (int i = 0; i < list.size(); i++) {
action.call(list.get(i), i);
}
}
// 如果是普通 List使用迭代器遍历并手动维护索引
else if (collection instanceof List) {
int index = 0;
Iterator<T> it = collection.iterator();
while (it.hasNext()) {
action.call(it.next(), index);
index++;
}
}
// 其他类型的集合使用增强 for 循环并手动维护索引
else {
int index = 0;
for (T element : collection) {
action.call(element, index);
index++;
}
}
}
/**
* 对给定的集合执行指定的操作仅处理元素值
* 根据集合是否实现 RandomAccess 接口选择最优的遍历方式
*
* @param collection 要遍历的集合
* @param action 要对每个元素执行的操作只接收元素值作为参数
* @param <T> 集合中元素的类型
*/
public static <T> void forEach(
Collection<T> collection,
java.util.function.Consumer<? super T> action
) {
// 参数校验如果集合或操作为空则直接返回
if (collection == null || action == null) {
return;
}
// 如果集合实现了 RandomAccess 接口使用索引访问提升性能
if (collection instanceof RandomAccess) {
List<T> list = (List<T>) collection;
for (int i = 0; i < list.size(); i++) {
action.accept(list.get(i));
}
}
// 否则使用增强 for 循环进行遍历
else {
for (T element : collection) {
action.accept(element);
}
}
}
/**
* 对给定的映射执行指定的操作操作包含键值和索引
* 根据映射类型选择不同的遍历策略
*
* @param map 要遍历的映射
* @param action 要对每个键值对执行的操作接收键值和索引作为参数
* @param <K> 映射中键的类型
* @param <V> 映射中值的类型
*/
public static <K, V> void forEach(
Map<K, V> map,
BiConsumer<? super K, ? super V> action
) {
// 参数校验如果映射或操作为空则直接返回
if (map == null || action == null) {
return;
}
// 遍历 TreeMap 的条目集合并传递索引
if (map instanceof TreeMap) {
int index = 0;
for (Map.Entry<K, V> entry : map.entrySet()) {
action.call(entry.getKey(), entry.getValue(), index);
index++;
}
}
// 遍历 ConcurrentMap LinkedHashMap 的条目集合并传递索引
else if (map instanceof ConcurrentMap || map instanceof LinkedHashMap) {
int index = 0;
for (Map.Entry<K, V> entry : map.entrySet()) {
action.call(entry.getKey(), entry.getValue(), index);
index++;
}
}
// 遍历其他类型映射的条目集合并传递索引
else {
int index = 0;
for (Map.Entry<K, V> entry : map.entrySet()) {
action.call(entry.getKey(), entry.getValue(), index);
index++;
}
}
}
/**
* 对给定的映射执行指定的操作仅处理键和值
* 根据映射类型选择不同的遍历策略
*
* @param map 要遍历的映射
* @param action 要对每个键值对执行的操作接收键和值作为参数
* @param <K> 映射中键的类型
* @param <V> 映射中值的类型
*/
public static <K, V> void forEach(
Map<K, V> map,
java.util.function.BiConsumer<? super K, ? super V> action
) {
// 参数校验如果映射或操作为空则直接返回
if (map == null || action == null) {
return;
}
// 遍历 TreeMap 的条目集合
if (map instanceof TreeMap) {
for (Map.Entry<K, V> entry : map.entrySet()) {
action.accept(entry.getKey(), entry.getValue());
}
}
// 如果是 ConcurrentMap LinkedHashMap使用其内置的 forEach 方法
else if (map instanceof ConcurrentMap || map instanceof LinkedHashMap) {
map.forEach(action);
}
// 遍历其他类型映射的条目集合
else {
for (Map.Entry<K, V> entry : map.entrySet()) {
action.accept(entry.getKey(), entry.getValue());
}
}
}
/**
* 自定义消费者接口用于接收元素值和索引
*
* @param <T> 元素类型
*/
@FunctionalInterface
public interface Consumer<T> {
/**
* 执行消费操作
*
* @param value 元素值
* @param index 元素在集合中的索引
*/
void call(T value, int index);
}
/**
* 自定义二元消费者接口用于接收键值和索引
*
* @param <K> 键类型
* @param <V> 值类型
*/
@FunctionalInterface
public interface BiConsumer<K, V> {
/**
* 执行消费操作
*
* @param key
* @param value
* @param index 键值对在映射中的索引
*/
void call(K key, V value, int index);
}
}