Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d12fbe0cce
|
||
|
|
9d118ed98d
|
||
|
|
6b7555d727
|
||
|
|
baa5aafe23
|
||
|
|
2c29e40e47
|
||
|
|
3dbb686a66
|
+1
-1
@@ -22,4 +22,4 @@
|
||||
JDKVERSIONS=1.8
|
||||
GROUPSID=com.mingliqiye.utils
|
||||
ARTIFACTID=mingli-utils
|
||||
VERSIONS=3.1.4
|
||||
VERSIONS=3.2.6
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
*
|
||||
* AES 加密
|
||||
* @author MingLiPro
|
||||
*
|
||||
*/
|
||||
package com.mingliqiye.utils.aes;
|
||||
@@ -23,13 +23,14 @@
|
||||
package com.mingliqiye.utils.collection;
|
||||
|
||||
import com.mingliqiye.utils.stream.SuperStream;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 集合工具类,提供对列表和数组的常用操作方法。
|
||||
@@ -337,4 +338,10 @@ public class Collection {
|
||||
) {
|
||||
return new ArrayList<>(collection);
|
||||
}
|
||||
|
||||
public static <T> SuperStream<T> toSuperStream(
|
||||
@NotNull java.util.Collection<T> list
|
||||
) {
|
||||
return SuperStream.of(list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,9 @@ package com.mingliqiye.utils.collection;
|
||||
import com.mingliqiye.utils.functions.P1Function;
|
||||
import com.mingliqiye.utils.functions.P2Function;
|
||||
import com.mingliqiye.utils.functions.P3Function;
|
||||
import com.mingliqiye.utils.stream.SuperStream;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 集合和映射的增强遍历功能
|
||||
@@ -46,33 +44,33 @@ import java.util.function.Function;
|
||||
public class ForEach {
|
||||
|
||||
/**
|
||||
* 对给定的集合执行指定的操作,操作包含元素值和索引。
|
||||
* 根据集合类型选择最优的遍历方式以提高性能。
|
||||
* 对给定的可迭代对象执行指定的操作,操作包含元素值和索引。
|
||||
* 根据可迭代对象类型选择最优的遍历方式以提高性能。
|
||||
*
|
||||
* @param collection 要遍历的集合,可以是 List 或其他 Collection 实现
|
||||
* @param iterable 要遍历的可迭代对象
|
||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||
* @param <T> 集合中元素的类型
|
||||
* @param <T> 可迭代对象中元素的类型
|
||||
*/
|
||||
public static <T> void forEach(
|
||||
Collection<T> collection,
|
||||
Iterable<T> iterable,
|
||||
P2Function<? super T, Integer> action
|
||||
) {
|
||||
// 参数校验:如果集合或操作为空,则直接返回
|
||||
if (collection == null || action == null) {
|
||||
if (iterable == null || action == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果集合实现了 RandomAccess 接口(如 ArrayList),使用索引访问优化性能
|
||||
if (collection instanceof RandomAccess && collection instanceof List) {
|
||||
List<T> list = (List<T>) collection;
|
||||
if (iterable instanceof RandomAccess && iterable instanceof List) {
|
||||
List<T> list = (List<T>) iterable;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
action.call(list.get(i), i);
|
||||
}
|
||||
}
|
||||
// 如果是普通 List,使用迭代器遍历并手动维护索引
|
||||
else if (collection instanceof List) {
|
||||
else if (iterable instanceof List) {
|
||||
int index = 0;
|
||||
Iterator<T> it = collection.iterator();
|
||||
Iterator<T> it = iterable.iterator();
|
||||
while (it.hasNext()) {
|
||||
action.call(it.next(), index);
|
||||
index++;
|
||||
@@ -81,7 +79,7 @@ public class ForEach {
|
||||
// 其他类型的集合使用增强 for 循环,并手动维护索引
|
||||
else {
|
||||
int index = 0;
|
||||
for (T element : collection) {
|
||||
for (T element : iterable) {
|
||||
action.call(element, index);
|
||||
index++;
|
||||
}
|
||||
@@ -89,37 +87,75 @@ public class ForEach {
|
||||
}
|
||||
|
||||
/**
|
||||
* 对给定的集合执行指定的操作,仅处理元素值。
|
||||
* 根据集合是否实现 RandomAccess 接口选择最优的遍历方式。
|
||||
* 对给定的可迭代对象执行指定的操作,仅处理元素值。
|
||||
* 根据可迭代对象是否实现 RandomAccess 接口选择最优的遍历方式。
|
||||
*
|
||||
* @param collection 要遍历的集合
|
||||
* @param iterable 要遍历的可迭代对象
|
||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||
* @param <T> 集合中元素的类型
|
||||
* @param <T> 可迭代对象中元素的类型
|
||||
*/
|
||||
public static <T> void forEach(
|
||||
Collection<T> collection,
|
||||
Iterable<T> iterable,
|
||||
P1Function<? super T> action
|
||||
) {
|
||||
// 参数校验:如果集合或操作为空,则直接返回
|
||||
if (collection == null || action == null) {
|
||||
if (iterable == null || action == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果集合实现了 RandomAccess 接口,使用索引访问提升性能
|
||||
if (collection instanceof RandomAccess) {
|
||||
List<T> list = (List<T>) collection;
|
||||
if (iterable instanceof RandomAccess) {
|
||||
List<T> list = (List<T>) iterable;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
action.call(list.get(i));
|
||||
}
|
||||
}
|
||||
// 否则使用增强 for 循环进行遍历
|
||||
else {
|
||||
for (T element : collection) {
|
||||
for (T element : iterable) {
|
||||
action.call(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对给定的迭代器执行指定的操作,仅处理元素值。
|
||||
* @param iterator 要遍历的迭代器
|
||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||
* @param <T> 迭代器中元素的类型
|
||||
*/
|
||||
public static <T> void forEach(
|
||||
Iterator<T> iterator,
|
||||
P2Function<? super T, Integer> action
|
||||
) {
|
||||
if (iterator == null || action == null) {
|
||||
return;
|
||||
}
|
||||
int index = 0;
|
||||
while (iterator.hasNext()) {
|
||||
action.call(iterator.next(), index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对给定的迭代器执行指定的操作,处理元素值和索引。
|
||||
* @param iterator 要遍历的迭代器
|
||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||
* @param <T> 迭代器中元素的类型
|
||||
*/
|
||||
public static <T> void forEach(
|
||||
Iterator<T> iterator,
|
||||
P1Function<? super T> action
|
||||
) {
|
||||
if (iterator == null || action == null) {
|
||||
return;
|
||||
}
|
||||
while (iterator.hasNext()) {
|
||||
action.call(iterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对给定的映射执行指定的操作,操作包含键、值和索引。
|
||||
* 根据映射类型选择不同的遍历策略。
|
||||
|
||||
@@ -25,6 +25,7 @@ package com.mingliqiye.utils.collection;
|
||||
import com.mingliqiye.utils.functions.P1RFunction;
|
||||
import com.mingliqiye.utils.functions.P2RFunction;
|
||||
import com.mingliqiye.utils.functions.P3RFunction;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
@@ -202,8 +203,7 @@ public class ForEachBreaked {
|
||||
// 如果是 ConcurrentMap 或 LinkedHashMap,使用其内置的 forEach 方法
|
||||
else if (map instanceof ConcurrentMap || map instanceof LinkedHashMap) {
|
||||
forEach(map.entrySet(), i -> {
|
||||
if (action.call(i.getKey(), i.getValue())) return true;
|
||||
return false;
|
||||
return action.call(i.getKey(), i.getValue());
|
||||
});
|
||||
}
|
||||
// 遍历其他类型映射的条目集合
|
||||
@@ -244,6 +244,48 @@ public class ForEachBreaked {
|
||||
forEach(action, objects);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对迭代器执行指定的操作,操作包含元素值和索引
|
||||
* 当操作返回 true 时,遍历将提前终止。
|
||||
*
|
||||
* @param iterator 要遍历的迭代器
|
||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 值决定是否继续遍历
|
||||
* @param <T> 数组中元素的类型
|
||||
*/
|
||||
public static <T> void forEach(
|
||||
Iterator<T> iterator,
|
||||
P2RFunction<? super T, Integer, Boolean> action
|
||||
) {
|
||||
if (iterator == null || action == null) {
|
||||
return;
|
||||
}
|
||||
int index = 0;
|
||||
while (iterator.hasNext()) {
|
||||
if (action.call(iterator.next(), index)) return;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对迭代器执行指定的操作,操作包含元素值
|
||||
* 当操作返回 true 时,遍历将提前终止。
|
||||
*
|
||||
* @param iterator 要遍历的迭代器
|
||||
* @param action 要对每个元素执行的操作,接收元素值作为参数,返回 Boolean 值决定是否继续遍历
|
||||
* @param <T> 数组中元素的类型
|
||||
*/
|
||||
public static <T> void forEach(
|
||||
Iterator<T> iterator,
|
||||
P1RFunction<? super T, Boolean> action
|
||||
) {
|
||||
if (iterator == null || action == null) {
|
||||
return;
|
||||
}
|
||||
while (iterator.hasNext()) {
|
||||
if (action.call(iterator.next())) return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对数组执行指定的操作,仅处理元素值
|
||||
* 当操作返回 true 时,遍历将提前终止。
|
||||
@@ -272,8 +314,7 @@ public class ForEachBreaked {
|
||||
T... objects
|
||||
) {
|
||||
forEach(Lists.toList(objects), (t, i) -> {
|
||||
if (action.call(t)) return true;
|
||||
return false;
|
||||
return action.call(t);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.mingliqiye.utils.iterator;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* 范围 Range<br>
|
||||
* 类似 KT的 {@code 0..10 = Range.of(0,10)}
|
||||
* @author MingLiPro
|
||||
* @since 3.2.6
|
||||
*/
|
||||
@Getter
|
||||
public class Range implements Iterable<Integer> {
|
||||
|
||||
private final int start;
|
||||
private final int end;
|
||||
private int current;
|
||||
|
||||
/**
|
||||
* 创建一个范围 <br>
|
||||
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||
* @param start 开始 (包含)
|
||||
* @param end 完毕 (包含)
|
||||
* @see Integer
|
||||
*/
|
||||
public Range(int start, int end) {
|
||||
this.start = start;
|
||||
this.current = start;
|
||||
this.end = end + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个范围 {@code 0 - range}<br>
|
||||
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||
* @param range 完毕 (包含)
|
||||
* @see Integer
|
||||
*/
|
||||
public Range(int range) {
|
||||
this(0, range);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个范围 {@code 0 - range}<br>
|
||||
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||
* @param range 完毕 (包含)
|
||||
* @see Integer
|
||||
* @return Range 对象
|
||||
*/
|
||||
public static Range of(int range) {
|
||||
return new Range(range);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个范围 <br>
|
||||
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||
* @param start 开始 (包含)
|
||||
* @param end 完毕 (包含)
|
||||
* @see Integer
|
||||
* @return Range 对象
|
||||
*/
|
||||
public static Range of(int start, int end) {
|
||||
return new Range(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个范围 {@code 0 - range}<br>
|
||||
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||
* @param range 完毕 (包含)
|
||||
* @see Integer
|
||||
* @return Range 对象
|
||||
*/
|
||||
public static Range range(int range) {
|
||||
return new Range(range);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个范围 <br>
|
||||
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||
* @param start 开始 (包含)
|
||||
* @param end 完毕 (包含)
|
||||
* @see Integer
|
||||
* @return Range 对象
|
||||
*/
|
||||
public static Range range(int start, int end) {
|
||||
return new Range(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取迭代器
|
||||
* @return 迭代器
|
||||
*/
|
||||
@Override
|
||||
public @NotNull Iterator<Integer> iterator() {
|
||||
return new Iterator<Integer>() {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current < end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer next() {
|
||||
if (current >= end) {
|
||||
return null;
|
||||
}
|
||||
return current++;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.mingliqiye.utils.json.converters;
|
||||
|
||||
import com.alibaba.fastjson2.JSONReader;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.reader.ObjectReader;
|
||||
import com.alibaba.fastjson2.writer.ObjectWriter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class FastjsonJsonStringConverterAdapter<
|
||||
T extends JsonStringConverter<TT>,
|
||||
TT
|
||||
> {
|
||||
|
||||
private final JsonStringConverter<TT> jsonStringConverter;
|
||||
|
||||
public FastjsonJsonStringConverterAdapter(T jsonStringConverter) {
|
||||
this.jsonStringConverter = jsonStringConverter;
|
||||
}
|
||||
|
||||
public static <
|
||||
T extends JsonStringConverter<TT>,
|
||||
TT
|
||||
> FastjsonJsonStringConverterAdapter<T, TT> of(T t) {
|
||||
return new FastjsonJsonStringConverterAdapter<>(t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取FastJson对象写入器
|
||||
*
|
||||
* @return FastJson的ObjectWriter实例
|
||||
*/
|
||||
public ObjectWriter<T> getFastJsonObjectWriter() {
|
||||
return (
|
||||
JSONWriter writer,
|
||||
Object object,
|
||||
Object fieldName,
|
||||
Type fieldType,
|
||||
long features
|
||||
) -> {
|
||||
// 如果对象为null则写入null
|
||||
if (object == null) {
|
||||
writer.writeNull();
|
||||
return;
|
||||
}
|
||||
writer.writeString(jsonStringConverter.convert((TT) object));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取FastJson对象读取器
|
||||
*
|
||||
* @return FastJson的ObjectReader实例
|
||||
*/
|
||||
public ObjectReader<TT> getFastJsonObjectReader() {
|
||||
return (
|
||||
JSONReader reader,
|
||||
Type fieldType,
|
||||
Object fieldName,
|
||||
long features
|
||||
) -> {
|
||||
String value = reader.readString();
|
||||
return jsonStringConverter.deConvert(value);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.mingliqiye.utils.json.converters;
|
||||
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class GsonJsonStringConverterAdapter<
|
||||
T extends JsonStringConverter<TT>,
|
||||
TT
|
||||
> {
|
||||
|
||||
private final JsonStringConverter<TT> jsonStringConverter;
|
||||
|
||||
public GsonJsonStringConverterAdapter(T jsonStringConverter) {
|
||||
this.jsonStringConverter = jsonStringConverter;
|
||||
}
|
||||
|
||||
public static <
|
||||
T extends JsonStringConverter<TT>,
|
||||
TT
|
||||
> GsonJsonStringConverterAdapter<T, TT> of(T t) {
|
||||
return new GsonJsonStringConverterAdapter<>(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Gson类型适配器
|
||||
*
|
||||
* @return Gson的TypeAdapter实例
|
||||
*/
|
||||
public TypeAdapter<TT> getGsonTypeAdapter() {
|
||||
return new TypeAdapter<TT>() {
|
||||
@Override
|
||||
public void write(JsonWriter out, TT value) throws IOException {
|
||||
if (value == null) {
|
||||
out.nullValue();
|
||||
return;
|
||||
}
|
||||
out.value(jsonStringConverter.convert(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TT read(JsonReader in) throws IOException {
|
||||
String value = in.nextString();
|
||||
return jsonStringConverter.deConvert(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package com.mingliqiye.utils.json.converters;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JSON转换器的适配器
|
||||
* @param <T> JSON转换器
|
||||
* @param <TT> JSON转换器的泛型
|
||||
* @author MingLiPro
|
||||
*/
|
||||
public class JacksonJsonStringConverterAdapter<
|
||||
T extends JsonStringConverter<TT>,
|
||||
TT
|
||||
> {
|
||||
|
||||
private final JsonStringConverter<TT> jsonStringConverter;
|
||||
|
||||
private JacksonJsonStringConverterAdapter(T jsonStringConverter) {
|
||||
this.jsonStringConverter = jsonStringConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t JSON转换器实例
|
||||
* @return JSON转换器的适配器
|
||||
* @param <T> JSON转换器
|
||||
* @param <TT> JSON转换器的泛型
|
||||
*/
|
||||
public static <
|
||||
T extends JsonStringConverter<TT>,
|
||||
TT
|
||||
> JacksonJsonStringConverterAdapter<T, TT> of(T t) {
|
||||
return new JacksonJsonStringConverterAdapter<>(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Jackson反序列化器
|
||||
*
|
||||
* @return Jackson的JsonDeserializer实例
|
||||
*/
|
||||
public JsonDeserializer<TT> getJacksonJsonDeserializer() {
|
||||
return new JsonDeserializer<TT>() {
|
||||
@Override
|
||||
public TT deserialize(JsonParser p, DeserializationContext ctxt)
|
||||
throws IOException {
|
||||
if (p.isNaN()) return null;
|
||||
return jsonStringConverter.deConvert(p.getValueAsString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Jackson序列化器
|
||||
*
|
||||
* @return Jackson的JsonSerializer实例
|
||||
*/
|
||||
public JsonSerializer<TT> getJacksonJsonSerializer() {
|
||||
return new JsonSerializer<TT>() {
|
||||
@Override
|
||||
public void serialize(
|
||||
TT value,
|
||||
JsonGenerator gen,
|
||||
SerializerProvider serializers
|
||||
) throws IOException {
|
||||
if (value == null) {
|
||||
gen.writeNull();
|
||||
return;
|
||||
}
|
||||
gen.writeString(jsonStringConverter.convert(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 获取 Jackson 的格式化模块
|
||||
*
|
||||
* @return 格式化模块
|
||||
*/
|
||||
public Module getJacksonModule() {
|
||||
Class<TT> tClass = jsonStringConverter.getTClass();
|
||||
SimpleModule m = new SimpleModule(tClass.getSimpleName());
|
||||
m.addSerializer(tClass, getJacksonJsonSerializer());
|
||||
m.addDeserializer(tClass, getJacksonJsonDeserializer());
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,5 @@
|
||||
package com.mingliqiye.utils.json.converters;
|
||||
|
||||
import com.alibaba.fastjson2.JSONReader;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.reader.ObjectReader;
|
||||
import com.alibaba.fastjson2.writer.ObjectWriter;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* JSON转换器接口,提供对象与字符串之间的相互转换功能,并支持多种JSON库
|
||||
*
|
||||
@@ -40,118 +26,35 @@ public abstract class JsonStringConverter<T> {
|
||||
abstract T deConvert(String string);
|
||||
|
||||
/**
|
||||
* 获取Jackson反序列化器
|
||||
*
|
||||
* @return Jackson的JsonDeserializer实例
|
||||
* 获取 Fastjson 的适配器
|
||||
* @return 适配器实例
|
||||
*/
|
||||
public JsonDeserializer<T> getJacksonJsonDeserializer() {
|
||||
return new JsonDeserializer<T>() {
|
||||
@Override
|
||||
public T deserialize(JsonParser p, DeserializationContext ctxt)
|
||||
throws IOException {
|
||||
// 如果是NaN则返回null
|
||||
if (p.isNaN()) return null;
|
||||
return deConvert(p.getValueAsString());
|
||||
}
|
||||
};
|
||||
public FastjsonJsonStringConverterAdapter<
|
||||
JsonStringConverter<T>,
|
||||
T
|
||||
> getFastjsonJsonStringConverterAdapter() {
|
||||
return FastjsonJsonStringConverterAdapter.of(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Jackson序列化器
|
||||
*
|
||||
* @return Jackson的JsonSerializer实例
|
||||
* 获取 Gson 的适配器
|
||||
* @return 适配器实例
|
||||
*/
|
||||
public JsonSerializer<T> getJacksonJsonSerializer() {
|
||||
return new JsonSerializer<T>() {
|
||||
@Override
|
||||
public void serialize(
|
||||
T value,
|
||||
JsonGenerator gen,
|
||||
SerializerProvider serializers
|
||||
) throws IOException {
|
||||
// 如果值为null则写入null
|
||||
if (value == null) {
|
||||
gen.writeNull();
|
||||
return;
|
||||
}
|
||||
gen.writeString(convert(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Module getJacksonModule() {
|
||||
Class<T> tClass = getTClass();
|
||||
SimpleModule m = new SimpleModule(tClass.getSimpleName());
|
||||
m.addSerializer(tClass, getJacksonJsonSerializer());
|
||||
m.addDeserializer(tClass, getJacksonJsonDeserializer());
|
||||
return m;
|
||||
public GsonJsonStringConverterAdapter<
|
||||
JsonStringConverter<T>,
|
||||
T
|
||||
> getGsonJsonStringConverterAdapter() {
|
||||
return GsonJsonStringConverterAdapter.of(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Gson类型适配器
|
||||
*
|
||||
* @return Gson的TypeAdapter实例
|
||||
* 获取 Jackson 的适配器
|
||||
* @return 适配器实例
|
||||
*/
|
||||
public TypeAdapter<T> getGsonTypeAdapter() {
|
||||
return new TypeAdapter<T>() {
|
||||
@Override
|
||||
public void write(JsonWriter out, T value) throws IOException {
|
||||
// 如果值为null则写入null值
|
||||
if (value == null) {
|
||||
out.nullValue();
|
||||
return;
|
||||
}
|
||||
out.value(convert(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read(JsonReader in) throws IOException {
|
||||
String value = in.nextString();
|
||||
return deConvert(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* FastJson 支持
|
||||
*/
|
||||
|
||||
/**
|
||||
* 获取FastJson对象写入器
|
||||
*
|
||||
* @return FastJson的ObjectWriter实例
|
||||
*/
|
||||
public ObjectWriter<T> getFastJsonObjectWriter() {
|
||||
return (
|
||||
JSONWriter writer,
|
||||
Object object,
|
||||
Object fieldName,
|
||||
Type fieldType,
|
||||
long features
|
||||
) -> {
|
||||
// 如果对象为null则写入null
|
||||
if (object == null) {
|
||||
writer.writeNull();
|
||||
return;
|
||||
}
|
||||
writer.writeString(convert((T) object));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取FastJson对象读取器
|
||||
*
|
||||
* @return FastJson的ObjectReader实例
|
||||
*/
|
||||
public ObjectReader<T> getFastJsonObjectReader() {
|
||||
return (
|
||||
JSONReader reader,
|
||||
Type fieldType,
|
||||
Object fieldName,
|
||||
long features
|
||||
) -> {
|
||||
String value = reader.readString();
|
||||
return deConvert(value);
|
||||
};
|
||||
public JacksonJsonStringConverterAdapter<
|
||||
JsonStringConverter<T>,
|
||||
T
|
||||
> getJacksonJsonStringConverterAdapter() {
|
||||
return JacksonJsonStringConverterAdapter.of(this);
|
||||
}
|
||||
}
|
||||
|
||||
+31
-16
@@ -2,6 +2,8 @@ package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.mingliqiye.utils.json.GsonJsonApi;
|
||||
import com.mingliqiye.utils.json.JsonApi;
|
||||
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter;
|
||||
import com.mingliqiye.utils.json.converters.JsonStringConverter;
|
||||
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter;
|
||||
@@ -13,14 +15,15 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.gson.GsonBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@ConditionalOnClass(Gson.class)
|
||||
@AutoConfiguration
|
||||
@AutoConfigureAfter(
|
||||
name = {
|
||||
"org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration",
|
||||
"com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration",
|
||||
}
|
||||
)
|
||||
public class GsonAutoConfiguration {
|
||||
@@ -34,25 +37,37 @@ public class GsonAutoConfiguration {
|
||||
new DateTimeJsonConverter();
|
||||
JsonStringConverter<UUID> uuidJsonStringConverter =
|
||||
new UUIDJsonStringConverter();
|
||||
return gsonBuilder
|
||||
.registerTypeAdapter(
|
||||
uuidJsonStringConverter.getTClass(),
|
||||
uuidJsonStringConverter.getGsonTypeAdapter()
|
||||
)
|
||||
.registerTypeAdapter(
|
||||
dateTimeJsonConverter.getTClass(),
|
||||
dateTimeJsonConverter.getGsonTypeAdapter()
|
||||
);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean
|
||||
public GsonBuilder gsonBuilder() {
|
||||
try {
|
||||
return addTypeAdapter(new GsonBuilder());
|
||||
return gsonBuilder
|
||||
.registerTypeAdapter(
|
||||
uuidJsonStringConverter.getTClass(),
|
||||
dateTimeJsonConverter
|
||||
.getGsonJsonStringConverterAdapter()
|
||||
.getGsonTypeAdapter()
|
||||
)
|
||||
.registerTypeAdapter(
|
||||
dateTimeJsonConverter.getTClass(),
|
||||
dateTimeJsonConverter
|
||||
.getGsonJsonStringConverterAdapter()
|
||||
.getGsonTypeAdapter()
|
||||
);
|
||||
} finally {
|
||||
log.info("MingliUtils GsonBuilder TypeAdapter add");
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GsonBuilderCustomizer mingliGsonCustomizer() {
|
||||
return GsonAutoConfiguration::addTypeAdapter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JsonApi jsonApi(Gson gson) {
|
||||
log.info(
|
||||
"MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created."
|
||||
);
|
||||
return new GsonJsonApi(gson);
|
||||
}
|
||||
}
|
||||
|
||||
+25
-2
@@ -1,6 +1,8 @@
|
||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.mingliqiye.utils.json.JacksonJsonApi;
|
||||
import com.mingliqiye.utils.json.JsonApi;
|
||||
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter;
|
||||
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter;
|
||||
import org.slf4j.Logger;
|
||||
@@ -8,6 +10,9 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@ConditionalOnClass(ObjectMapper.class)
|
||||
@AutoConfiguration
|
||||
@@ -29,7 +34,25 @@ public class JacksonAutoConfiguration {
|
||||
|
||||
public static ObjectMapper addModules(ObjectMapper objectMapper) {
|
||||
return objectMapper
|
||||
.registerModule(new UUIDJsonStringConverter().getJacksonModule())
|
||||
.registerModule(new DateTimeJsonConverter().getJacksonModule());
|
||||
.registerModule(
|
||||
new DateTimeJsonConverter()
|
||||
.getJacksonJsonStringConverterAdapter()
|
||||
.getJacksonModule()
|
||||
)
|
||||
.registerModule(
|
||||
new UUIDJsonStringConverter()
|
||||
.getJacksonJsonStringConverterAdapter()
|
||||
.getJacksonModule()
|
||||
);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean
|
||||
public JsonApi jsonApi(ObjectMapper objectMapper) {
|
||||
log.info(
|
||||
"MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created."
|
||||
);
|
||||
return new JacksonJsonApi(objectMapper);
|
||||
}
|
||||
}
|
||||
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.mingliqiye.utils.json.GsonJsonApi;
|
||||
import com.mingliqiye.utils.json.JacksonJsonApi;
|
||||
import com.mingliqiye.utils.json.JsonApi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@AutoConfiguration
|
||||
@AutoConfigureAfter(
|
||||
name = {
|
||||
"com.mingliqiye.utils.springboot.autoconfigure.GsonAutoConfiguration",
|
||||
"com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration",
|
||||
}
|
||||
)
|
||||
public class JsonApiAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(
|
||||
"MingliUtils-JsonApiAutoConfiguration"
|
||||
);
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(ObjectMapper.class)
|
||||
public JsonApi jacksonJsonApi(ObjectMapper objectMapper) {
|
||||
log.info(
|
||||
"MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created."
|
||||
);
|
||||
return new JacksonJsonApi(objectMapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(Gson.class)
|
||||
public JsonApi gsonJsonApi(Gson gson) {
|
||||
log.info(
|
||||
"MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created."
|
||||
);
|
||||
return new GsonJsonApi(gson);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.mingliqiye.utils.stream;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class InputStreanWrapper extends InputStream implements AutoCloseable {
|
||||
|
||||
@Getter
|
||||
@@ -72,9 +75,9 @@ public class InputStreanWrapper extends InputStream implements AutoCloseable {
|
||||
* jdk8 兼容实现 jdk9+ <br>
|
||||
* 请使用 InputStream.transferTo()
|
||||
*
|
||||
* @param outputStream
|
||||
* @param outputStream 输出流
|
||||
* @return 转换的字节数
|
||||
* @throws IOException
|
||||
* @throws IOException IO错误
|
||||
*/
|
||||
public long transferToOutputStream(OutputStream outputStream)
|
||||
throws IOException {
|
||||
|
||||
@@ -295,8 +295,9 @@ public class SuperStream<T> implements Stream<T> {
|
||||
*
|
||||
* @param predicate 谓词函数
|
||||
* @return SuperStream 实例
|
||||
* @since 3.2.5
|
||||
*/
|
||||
public SuperStream<T> filter(Function<T, Boolean> predicate) {
|
||||
public SuperStream<T> filterBoolean(Function<T, Boolean> predicate) {
|
||||
return new SuperStream<>(stream.filter(predicate::apply));
|
||||
}
|
||||
|
||||
@@ -1430,6 +1431,8 @@ public class SuperStream<T> implements Stream<T> {
|
||||
* 返回一个收集器,将元素收集到 List 中。
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param <K> Map.Key
|
||||
* @param <V> Map.Value
|
||||
* @return 收集器
|
||||
*/
|
||||
public static <T extends Map.Entry<K, V>, K, V> Collector<
|
||||
|
||||
-1
@@ -23,4 +23,3 @@
|
||||
com.mingliqiye.utils.springboot.autoconfigure.AutoConfiguration
|
||||
com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration
|
||||
com.mingliqiye.utils.springboot.autoconfigure.GsonAutoConfiguration
|
||||
com.mingliqiye.utils.springboot.autoconfigure.JsonApiAutoConfiguration
|
||||
|
||||
Reference in New Issue
Block a user