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