From 887afc8984f13a6c2dff7e8d4229023786dc978b Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 29 Jun 2023 17:43:49 +0800 Subject: [PATCH] fix bug --- .../core/convert/RegisterConverter.java | 4 +- .../core/convert/impl/PairConverter.java | 22 ++-- .../core/convert/impl/TripleConverter.java | 102 ++++++++++++++++++ .../dromara/hutool/json/IssueI7GPGXTest.java | 11 +- 4 files changed, 126 insertions(+), 13 deletions(-) create mode 100755 hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/TripleConverter.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/convert/RegisterConverter.java b/hutool-core/src/main/java/org/dromara/hutool/core/convert/RegisterConverter.java index 0de77c32b..d12378389 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/convert/RegisterConverter.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/convert/RegisterConverter.java @@ -16,6 +16,7 @@ import org.dromara.hutool.core.convert.impl.*; import org.dromara.hutool.core.date.DateTime; import org.dromara.hutool.core.lang.Opt; import org.dromara.hutool.core.lang.tuple.Pair; +import org.dromara.hutool.core.lang.tuple.Triple; import org.dromara.hutool.core.map.SafeConcurrentHashMap; import org.dromara.hutool.core.reflect.TypeUtil; @@ -216,6 +217,7 @@ public class RegisterConverter implements Converter, Serializable { defaultConverterMap.put(StackTraceElement.class, new StackTraceElementConverter());// since 4.5.2 defaultConverterMap.put(Optional.class, new OptionalConverter());// since 5.0.0 defaultConverterMap.put(Opt.class, new OptConverter());// since 5.7.16 - defaultConverterMap.put(Pair.class, PairConverter.INSTANCE);// since 5.7.16 + defaultConverterMap.put(Pair.class, PairConverter.INSTANCE);// since 6.0.0 + defaultConverterMap.put(Triple.class, TripleConverter.INSTANCE);// since 6.0.0 } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/PairConverter.java b/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/PairConverter.java index 13b061678..6befa9814 100755 --- a/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/PairConverter.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/PairConverter.java @@ -32,7 +32,7 @@ import java.util.Map; *
  • {@link Map}
  • *
  • {@link Map.Entry}
  • *
  • 带分隔符的字符串,支持分隔符{@code :}、{@code =}、{@code ,}
  • - *
  • Bean,包含{@code getKey}和{@code getValue}方法
  • + *
  • Bean,包含{@code getLeft}和{@code getRight}方法
  • * * * @author looly @@ -49,23 +49,23 @@ public class PairConverter implements Converter { if (targetType instanceof TypeReference) { targetType = ((TypeReference) targetType).getType(); } - final Type keyType = TypeUtil.getTypeArgument(targetType, 0); - final Type valueType = TypeUtil.getTypeArgument(targetType, 1); + final Type leftType = TypeUtil.getTypeArgument(targetType, 0); + final Type rightType = TypeUtil.getTypeArgument(targetType, 1); - return convert(keyType, valueType, value); + return convert(leftType, rightType, value); } /** * 转换对象为指定键值类型的指定类型Map * - * @param keyType 键类型 - * @param valueType 值类型 + * @param leftType 键类型 + * @param rightType 值类型 * @param value 被转换的值 * @return 转换后的Map * @throws ConvertException 转换异常或不支持的类型 */ @SuppressWarnings("rawtypes") - public Pair convert(final Type keyType, final Type valueType, final Object value) + public Pair convert(final Type leftType, final Type rightType, final Object value) throws ConvertException { Map map = null; if (value instanceof Map.Entry) { @@ -79,12 +79,12 @@ public class PairConverter implements Converter { } else if (value instanceof CharSequence) { final CharSequence str = (CharSequence) value; map = strToMap(str); - } else if (BeanUtil.isWritableBean(value.getClass())) { + } else if (BeanUtil.isReadableBean(value.getClass())) { map = BeanUtil.beanToMap(value); } if (null != map) { - return mapToPair(keyType, valueType, map); + return mapToPair(leftType, rightType, map); } throw new ConvertException("Unsupported to map from [{}] of type: {}", value, value.getClass().getName()); @@ -109,12 +109,12 @@ public class PairConverter implements Converter { } /** - * Map转Entry + * Map转Pair * * @param keyType 键类型 * @param valueType 值类型 * @param map 被转换的map - * @return Entry + * @return Pair */ @SuppressWarnings("rawtypes") private static Pair mapToPair(final Type keyType, final Type valueType, final Map map) { diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/TripleConverter.java b/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/TripleConverter.java new file mode 100755 index 000000000..8d7f851e5 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/TripleConverter.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.core.convert.impl; + +import org.dromara.hutool.core.bean.BeanUtil; +import org.dromara.hutool.core.convert.CompositeConverter; +import org.dromara.hutool.core.convert.ConvertException; +import org.dromara.hutool.core.convert.Converter; +import org.dromara.hutool.core.lang.tuple.Pair; +import org.dromara.hutool.core.lang.tuple.Triple; +import org.dromara.hutool.core.reflect.TypeReference; +import org.dromara.hutool.core.reflect.TypeUtil; + +import java.lang.reflect.Type; +import java.util.Map; + +/** + * {@link Triple} 转换器,支持以下类型转为Triple: + * + * + * @author looly + * @since 6.0.0 + */ +public class TripleConverter implements Converter { + + /** + * 单例 + */ + public static final TripleConverter INSTANCE = new TripleConverter(); + + @Override + public Object convert(Type targetType, final Object value) throws ConvertException { + if (targetType instanceof TypeReference) { + targetType = ((TypeReference) targetType).getType(); + } + final Type leftType = TypeUtil.getTypeArgument(targetType, 0); + final Type middileType = TypeUtil.getTypeArgument(targetType, 1); + final Type rightType = TypeUtil.getTypeArgument(targetType, 2); + + return convert(leftType, middileType, rightType, value); + } + + /** + * 转换对象为指定键值类型的指定类型Map + * + * @param leftType 键类型 + * @param middleType 中值类型 + * @param rightType 值类型 + * @param value 被转换的值 + * @return 转换后的Map + * @throws ConvertException 转换异常或不支持的类型 + */ + @SuppressWarnings("rawtypes") + public Triple convert(final Type leftType, final Type middleType, final Type rightType, final Object value) + throws ConvertException { + Map map = null; + if (BeanUtil.isReadableBean(value.getClass())) { + map = BeanUtil.beanToMap(value); + } + + if (null != map) { + return mapToTriple(leftType, middleType, rightType, map); + } + + throw new ConvertException("Unsupported to map from [{}] of type: {}", value, value.getClass().getName()); + } + + /** + * Map转Entry + * + * @param leftType 键类型 + * @param rightType 值类型 + * @param map 被转换的map + * @return Entry + */ + @SuppressWarnings("rawtypes") + private static Triple mapToTriple(final Type leftType, final Type middleType, final Type rightType, final Map map) { + + final Object left = map.get("left"); + final Object middle = map.get("middle"); + final Object right = map.get("right"); + + final CompositeConverter convert = CompositeConverter.getInstance(); + return Triple.of( + TypeUtil.isUnknown(leftType) ? left : convert.convert(leftType, left), + TypeUtil.isUnknown(middleType) ? middle : convert.convert(middleType, middle), + TypeUtil.isUnknown(rightType) ? right : convert.convert(rightType, right) + ); + } +} diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/IssueI7GPGXTest.java b/hutool-json/src/test/java/org/dromara/hutool/json/IssueI7GPGXTest.java index a9497f01a..7de290e71 100755 --- a/hutool-json/src/test/java/org/dromara/hutool/json/IssueI7GPGXTest.java +++ b/hutool-json/src/test/java/org/dromara/hutool/json/IssueI7GPGXTest.java @@ -13,6 +13,7 @@ package org.dromara.hutool.json; import org.dromara.hutool.core.lang.tuple.Pair; +import org.dromara.hutool.core.lang.tuple.Triple; import org.dromara.hutool.core.reflect.TypeReference; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -20,9 +21,17 @@ import org.junit.jupiter.api.Test; public class IssueI7GPGXTest { @Test public void pairToBeanTest() { - final Pair hutoolPair = new Pair<>("test1", true); + final Pair hutoolPair = Pair.of("test1", true); final String a = JSONUtil.toJsonStr(hutoolPair); final Pair pair = JSONUtil.toBean(a, new TypeReference>() {}); Assertions.assertEquals(hutoolPair, pair); } + + @Test + void tripleToBeanTest() { + final Triple hutoolTriple = Triple.of("aaa", 123, true); + final String a = JSONUtil.toJsonStr(hutoolTriple); + final Triple pair = JSONUtil.toBean(a, new TypeReference>() {}); + Assertions.assertEquals(hutoolTriple, pair); + } }