mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-21 14:26:56 +00:00
chore[orm]: simplify MapCodec
This commit is contained in:
@@ -13,6 +13,7 @@ import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 基础类型作为key的map解析器 (key 默认不能为null)
|
||||
*
|
||||
* @Author:lqh
|
||||
* @Date:2024/6/17 13:55
|
||||
*/
|
||||
@@ -21,16 +22,18 @@ public class MapCodec<K, V> implements Codec<Map<K, V>> {
|
||||
private final Class<Map<K, V>> encoderClass;
|
||||
private final Codec<K> keyCodec;
|
||||
private final Codec<V> valueCodec;
|
||||
private final Function<String, K> keyDecodeFunction;
|
||||
|
||||
MapCodec(final Class<Map<K, V>> encoderClass, final Codec<K> keyCodec, final Codec<V> valueCodec) {
|
||||
@SuppressWarnings("unchecked")
|
||||
MapCodec(Class<Map<K, V>> encoderClass, Codec<K> keyCodec, Codec<V> valueCodec) {
|
||||
this.encoderClass = encoderClass;
|
||||
this.keyCodec = keyCodec;
|
||||
this.valueCodec = valueCodec;
|
||||
this.keyDecodeFunction = (Function<String, K>) MapKeyCodecEnum.keyDecode(keyCodec.getEncoderClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public void encode(final BsonWriter writer, final Map<K, V> map, final EncoderContext encoderContext) {
|
||||
public void encode(BsonWriter writer, Map<K, V> map, EncoderContext encoderContext) {
|
||||
writer.writeStartDocument();
|
||||
for (var entry : map.entrySet()) {
|
||||
var key = entry.getKey();
|
||||
@@ -46,12 +49,10 @@ public class MapCodec<K, V> implements Codec<Map<K, V>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public Map<K, V> decode(final BsonReader reader, final DecoderContext context) {
|
||||
reader.readStartDocument();
|
||||
public Map<K, V> decode(BsonReader reader, DecoderContext context) {
|
||||
var map = new HashMap<K, V>();
|
||||
reader.readStartDocument();
|
||||
while (BsonType.END_OF_DOCUMENT != reader.readBsonType()) {
|
||||
var keyDecodeFunction = (Function<String, K>) MapKeyCodecEnum.keyDecode(keyCodec.getEncoderClass());
|
||||
K key = keyDecodeFunction.apply(reader.readName());
|
||||
V value = null;
|
||||
if (BsonType.NULL == reader.getCurrentBsonType()) {
|
||||
|
||||
@@ -15,7 +15,7 @@ import java.util.Map;
|
||||
public class MapCodecProvider implements PropertyCodecProvider {
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) {
|
||||
public <T> Codec<T> get(TypeWithTypeParameters<T> type, PropertyCodecRegistry registry) {
|
||||
if (!Map.class.isAssignableFrom(type.getType())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ public enum MapKeyCodecEnum {
|
||||
|
||||
;
|
||||
|
||||
private final Class<?> keyType;
|
||||
public final Class<?> keyType;
|
||||
|
||||
private final Function<String, ?> keyDecodeFunction;
|
||||
public final Function<String, ?> keyDecodeFunction;
|
||||
|
||||
|
||||
MapKeyCodecEnum(Class<?> keyType, Function<String, ?> keyDecodeFunction) {
|
||||
@@ -41,17 +41,16 @@ public enum MapKeyCodecEnum {
|
||||
this.keyDecodeFunction = keyDecodeFunction;
|
||||
}
|
||||
|
||||
public static final Map<Class<?>, Function<String, ?>> keyDecodeMap = Arrays.stream(values()).collect(Collectors.toMap(MapKeyCodecEnum::getKeyType, MapKeyCodecEnum::getKeyDecodeFunction));
|
||||
|
||||
|
||||
public Class<?> getKeyType() {
|
||||
public Class<?> keyType() {
|
||||
return keyType;
|
||||
}
|
||||
|
||||
public Function<String, ?> getKeyDecodeFunction() {
|
||||
public Function<String, ?> keyDecodeFunction() {
|
||||
return keyDecodeFunction;
|
||||
}
|
||||
|
||||
public static final Map<Class<?>, Function<String, ?>> keyDecodeMap = Arrays.stream(values()).collect(Collectors.toMap(MapKeyCodecEnum::keyType, MapKeyCodecEnum::keyDecodeFunction));
|
||||
|
||||
public static boolean containsKeyDecode(Class<?> keyClass) {
|
||||
return keyDecodeMap.containsKey(keyClass);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user