mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
remove pair
This commit is contained in:
parent
1661ec42bd
commit
eaa80f55a6
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
@ -10,7 +11,6 @@ import cn.hutool.core.util.CharUtil;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -271,7 +271,7 @@ public class BeanPath implements Serializable{
|
||||
}
|
||||
|
||||
// 不可变List
|
||||
this.patternParts = Collections.unmodifiableList(localPatternParts);
|
||||
this.patternParts = ListUtil.view(localPatternParts);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,7 @@ package cn.hutool.core.classloader;
|
||||
import cn.hutool.core.convert.BasicType;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.WeakConcurrentMap;
|
||||
import cn.hutool.core.text.CharPool;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
@ -50,7 +50,7 @@ public class ClassLoaderUtil {
|
||||
* 原始类型名和其class对应表,例如:int =》 int.class
|
||||
*/
|
||||
private static final Map<String, Class<?>> PRIMITIVE_TYPE_NAME_MAP = new ConcurrentHashMap<>(32);
|
||||
private static final WeakConcurrentMap<Pair<String, ClassLoader>, Class<?>> CLASS_CACHE = new WeakConcurrentMap<>();
|
||||
private static final WeakConcurrentMap<Map.Entry<String, ClassLoader>, Class<?>> CLASS_CACHE = new WeakConcurrentMap<>();
|
||||
|
||||
static {
|
||||
final List<Class<?>> primitiveTypes = new ArrayList<>(32);
|
||||
@ -202,7 +202,7 @@ public class ClassLoaderUtil {
|
||||
if (clazz == null) {
|
||||
final String finalName = name;
|
||||
final ClassLoader finalClassLoader = classLoader;
|
||||
clazz = CLASS_CACHE.computeIfAbsent(Pair.of(name, classLoader), (key)-> doLoadClass(finalName, finalClassLoader, isInitialized));
|
||||
clazz = CLASS_CACHE.computeIfAbsent(MapUtil.entry(name, classLoader), (key)-> doLoadClass(finalName, finalClassLoader, isInitialized));
|
||||
}
|
||||
return (Class<T>) clazz;
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package cn.hutool.core.date.chinese;
|
||||
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.TableMap;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 节假日(农历)封装
|
||||
@ -15,72 +16,72 @@ public class LunarFestival {
|
||||
|
||||
//农历节日 *表示放假日
|
||||
// 来自:https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E4%BC%A0%E7%BB%9F%E8%8A%82%E6%97%A5/396100
|
||||
private static final TableMap<Pair<Integer, Integer>, String> L_FTV = new TableMap<>(16);
|
||||
private static final TableMap<Map.Entry<Integer, Integer>, String> L_FTV = new TableMap<>(16);
|
||||
|
||||
static {
|
||||
// 节日
|
||||
L_FTV.put(new Pair<>(1, 1), "春节");
|
||||
L_FTV.put(new Pair<>(1, 2), "犬日");
|
||||
L_FTV.put(new Pair<>(1, 3), "猪日");
|
||||
L_FTV.put(new Pair<>(1, 4), "羊日");
|
||||
L_FTV.put(new Pair<>(1, 5), "牛日 破五日");
|
||||
L_FTV.put(new Pair<>(1, 6), "马日 送穷日");
|
||||
L_FTV.put(new Pair<>(1, 7), "人日 人胜节");
|
||||
L_FTV.put(new Pair<>(1, 8), "谷日 八仙日");
|
||||
L_FTV.put(new Pair<>(1, 9), "天日 九皇会");
|
||||
L_FTV.put(new Pair<>(1, 10), "地日 石头生日");
|
||||
L_FTV.put(new Pair<>(1, 12), "火日 老鼠娶媳妇日");
|
||||
L_FTV.put(new Pair<>(1, 13), "上(试)灯日 关公升天日");
|
||||
L_FTV.put(new Pair<>(1, 15), "元宵节 上元节");
|
||||
L_FTV.put(new Pair<>(1, 18), "落灯日");
|
||||
L_FTV.put(MapUtil.entry(1, 1), "春节");
|
||||
L_FTV.put(MapUtil.entry(1, 2), "犬日");
|
||||
L_FTV.put(MapUtil.entry(1, 3), "猪日");
|
||||
L_FTV.put(MapUtil.entry(1, 4), "羊日");
|
||||
L_FTV.put(MapUtil.entry(1, 5), "牛日 破五日");
|
||||
L_FTV.put(MapUtil.entry(1, 6), "马日 送穷日");
|
||||
L_FTV.put(MapUtil.entry(1, 7), "人日 人胜节");
|
||||
L_FTV.put(MapUtil.entry(1, 8), "谷日 八仙日");
|
||||
L_FTV.put(MapUtil.entry(1, 9), "天日 九皇会");
|
||||
L_FTV.put(MapUtil.entry(1, 10), "地日 石头生日");
|
||||
L_FTV.put(MapUtil.entry(1, 12), "火日 老鼠娶媳妇日");
|
||||
L_FTV.put(MapUtil.entry(1, 13), "上(试)灯日 关公升天日");
|
||||
L_FTV.put(MapUtil.entry(1, 15), "元宵节 上元节");
|
||||
L_FTV.put(MapUtil.entry(1, 18), "落灯日");
|
||||
|
||||
// 二月
|
||||
L_FTV.put(new Pair<>(2, 1), "中和节 太阳生日");
|
||||
L_FTV.put(new Pair<>(2, 2), "龙抬头");
|
||||
L_FTV.put(new Pair<>(2, 12), "花朝节");
|
||||
L_FTV.put(new Pair<>(2, 19), "观世音圣诞");
|
||||
L_FTV.put(MapUtil.entry(2, 1), "中和节 太阳生日");
|
||||
L_FTV.put(MapUtil.entry(2, 2), "龙抬头");
|
||||
L_FTV.put(MapUtil.entry(2, 12), "花朝节");
|
||||
L_FTV.put(MapUtil.entry(2, 19), "观世音圣诞");
|
||||
|
||||
// 三月
|
||||
L_FTV.put(new Pair<>(3, 3), "上巳节");
|
||||
L_FTV.put(MapUtil.entry(3, 3), "上巳节");
|
||||
|
||||
// 四月
|
||||
L_FTV.put(new Pair<>(4, 1), "祭雹神");
|
||||
L_FTV.put(new Pair<>(4, 4), "文殊菩萨诞辰");
|
||||
L_FTV.put(new Pair<>(4, 8), "佛诞节");
|
||||
L_FTV.put(MapUtil.entry(4, 1), "祭雹神");
|
||||
L_FTV.put(MapUtil.entry(4, 4), "文殊菩萨诞辰");
|
||||
L_FTV.put(MapUtil.entry(4, 8), "佛诞节");
|
||||
|
||||
// 五月
|
||||
L_FTV.put(new Pair<>(5, 5), "端午节 端阳节");
|
||||
L_FTV.put(MapUtil.entry(5, 5), "端午节 端阳节");
|
||||
|
||||
// 六月
|
||||
L_FTV.put(new Pair<>(6, 6), "晒衣节 姑姑节");
|
||||
L_FTV.put(new Pair<>(6, 6), "天贶节");
|
||||
L_FTV.put(new Pair<>(6, 24), "彝族火把节");
|
||||
L_FTV.put(MapUtil.entry(6, 6), "晒衣节 姑姑节");
|
||||
L_FTV.put(MapUtil.entry(6, 6), "天贶节");
|
||||
L_FTV.put(MapUtil.entry(6, 24), "彝族火把节");
|
||||
|
||||
// 七月
|
||||
L_FTV.put(new Pair<>(7, 7), "七夕");
|
||||
L_FTV.put(new Pair<>(7, 14), "鬼节(南方)");
|
||||
L_FTV.put(new Pair<>(7, 15), "中元节");
|
||||
L_FTV.put(new Pair<>(7, 15), "盂兰盆节 中元节");
|
||||
L_FTV.put(new Pair<>(7, 30), "地藏节");
|
||||
L_FTV.put(MapUtil.entry(7, 7), "七夕");
|
||||
L_FTV.put(MapUtil.entry(7, 14), "鬼节(南方)");
|
||||
L_FTV.put(MapUtil.entry(7, 15), "中元节");
|
||||
L_FTV.put(MapUtil.entry(7, 15), "盂兰盆节 中元节");
|
||||
L_FTV.put(MapUtil.entry(7, 30), "地藏节");
|
||||
|
||||
// 八月
|
||||
L_FTV.put(new Pair<>(8, 15), "中秋节");
|
||||
L_FTV.put(MapUtil.entry(8, 15), "中秋节");
|
||||
|
||||
// 九月
|
||||
L_FTV.put(new Pair<>(9, 9), "重阳节");
|
||||
L_FTV.put(MapUtil.entry(9, 9), "重阳节");
|
||||
|
||||
// 十月
|
||||
L_FTV.put(new Pair<>(10, 1), "祭祖节");
|
||||
L_FTV.put(new Pair<>(10, 15), "下元节");
|
||||
L_FTV.put(MapUtil.entry(10, 1), "祭祖节");
|
||||
L_FTV.put(MapUtil.entry(10, 15), "下元节");
|
||||
|
||||
// 十一月
|
||||
L_FTV.put(new Pair<>(11, 17), "阿弥陀佛圣诞");
|
||||
L_FTV.put(MapUtil.entry(11, 17), "阿弥陀佛圣诞");
|
||||
|
||||
// 腊月
|
||||
L_FTV.put(new Pair<>(12, 8), "腊八节");
|
||||
L_FTV.put(new Pair<>(12, 16), "尾牙");
|
||||
L_FTV.put(new Pair<>(12, 23), "小年");
|
||||
L_FTV.put(new Pair<>(12, 30), "除夕");
|
||||
L_FTV.put(MapUtil.entry(12, 8), "腊八节");
|
||||
L_FTV.put(MapUtil.entry(12, 16), "尾牙");
|
||||
L_FTV.put(MapUtil.entry(12, 23), "小年");
|
||||
L_FTV.put(MapUtil.entry(12, 30), "除夕");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,6 +111,6 @@ public class LunarFestival {
|
||||
* @return 获得农历节日
|
||||
*/
|
||||
public static List<String> getFestivals(final int month, final int day) {
|
||||
return L_FTV.getValues(new Pair<>(month, day));
|
||||
return L_FTV.getValues(MapUtil.entry(month, day));
|
||||
}
|
||||
}
|
||||
|
@ -1,87 +0,0 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import cn.hutool.core.clone.CloneSupport;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 键值对对象,只能在构造时传入键值
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author looly
|
||||
* @since 4.1.5
|
||||
*/
|
||||
public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected K key;
|
||||
protected V value;
|
||||
|
||||
/**
|
||||
* 构建{@code Pair}对象
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return {@code Pair}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <K, V> Pair<K, V> of(final K key, final V value) {
|
||||
return new Pair<>(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public Pair(final K key, final V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取键
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
* @return 值
|
||||
*/
|
||||
public V getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pair [key=" + key + ", value=" + value + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o instanceof Pair) {
|
||||
final Pair<?, ?> pair = (Pair<?, ?>) o;
|
||||
return Objects.equals(getKey(), pair.getKey()) &&
|
||||
Objects.equals(getValue(), pair.getValue());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
//copy from 1.8 HashMap.Node
|
||||
return Objects.hashCode(key) ^ Objects.hashCode(value);
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package cn.hutool.core.lang.mutable;
|
||||
|
||||
import cn.hutool.core.map.AbsEntry;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 可变键和值的{@link Map.Entry}实现,可以修改键和值
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author looly
|
||||
*/
|
||||
public class MutableEntry<K, V> extends AbsEntry<K, V> implements Mutable<Map.Entry<K, V>>, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected K key;
|
||||
protected V value;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public MutableEntry(final K key, final V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取键
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public V getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键
|
||||
*
|
||||
* @param key 新键
|
||||
* @return old key
|
||||
*/
|
||||
public K setKey(final K key) {
|
||||
final K oldKey = this.key;
|
||||
this.key = key;
|
||||
return oldKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置值
|
||||
*
|
||||
* @param value 新值
|
||||
* @return old value
|
||||
*/
|
||||
@Override
|
||||
public V setValue(final V value) {
|
||||
final V oldValue = this.value;
|
||||
this.value = value;
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map.Entry<K, V> get() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(final Map.Entry<K, V> pair) {
|
||||
this.key = pair.getKey();
|
||||
this.value = pair.getValue();
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package cn.hutool.core.lang.mutable;
|
||||
|
||||
import cn.hutool.core.lang.Pair;
|
||||
|
||||
/**
|
||||
* 可变{@link Pair}实现,可以修改键和值
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @since 5.7.16
|
||||
*/
|
||||
public class MutablePair<K, V> extends Pair<K, V> implements Mutable<Pair<K, V>>{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public MutablePair(final K key, final V value) {
|
||||
super(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键
|
||||
*
|
||||
* @param key 新键
|
||||
* @return this
|
||||
*/
|
||||
public MutablePair<K, V> setKey(final K key) {
|
||||
this.key = key;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置值
|
||||
*
|
||||
* @param value 新值
|
||||
* @return this
|
||||
*/
|
||||
public MutablePair<K, V> setValue(final V value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<K, V> get() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(final Pair<K, V> pair) {
|
||||
this.key = pair.getKey();
|
||||
this.value = pair.getValue();
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.SetUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
import cn.hutool.core.lang.func.LambdaUtil;
|
||||
import cn.hutool.core.lang.getter.BasicTypeGetter;
|
||||
@ -61,16 +60,15 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定的Pair数组创建Dict对象
|
||||
* 根据给定的Entry数组创建Dict对象
|
||||
*
|
||||
* @param pairs 键值对
|
||||
* @return Dict
|
||||
* @since 5.4.1
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static Dict of(final Pair<String, Object>... pairs) {
|
||||
public static Dict ofEntries(final Map.Entry<String, Object>... pairs) {
|
||||
final Dict dict = create();
|
||||
for (final Pair<String, Object> pair : pairs) {
|
||||
for (final Map.Entry<String, Object> pair : pairs) {
|
||||
dict.put(pair.getKey(), pair.getValue());
|
||||
}
|
||||
return dict;
|
||||
|
@ -1412,7 +1412,7 @@ public class MapUtil {
|
||||
*/
|
||||
public static <K, V> Map.Entry<K, V> entry(final K key, final V value, final boolean isImmutable) {
|
||||
return isImmutable ?
|
||||
new AbstractMap.SimpleEntry<>(key, value) :
|
||||
new AbstractMap.SimpleImmutableEntry<>(key, value);
|
||||
new AbstractMap.SimpleImmutableEntry<>(key, value) :
|
||||
new AbstractMap.SimpleEntry<>(key, value);
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,16 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import cn.hutool.core.lang.test.bean.UserInfoDict;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import cn.hutool.core.lang.test.bean.UserInfoDict;
|
||||
|
||||
/**
|
||||
* {@link BeanPath} 单元测试
|
||||
*
|
||||
@ -103,8 +102,19 @@ public class BeanPathTest {
|
||||
@Test
|
||||
public void getMapTest () {
|
||||
final BeanPath pattern = BeanPath.create("userInfo[id, photoPath]");
|
||||
@SuppressWarnings("unchecked") final Map<String, Object> result = (Map<String, Object>)pattern.get(tempMap);
|
||||
@SuppressWarnings("unchecked")
|
||||
final Map<String, Object> result = (Map<String, Object>)pattern.get(tempMap);
|
||||
Assert.assertEquals(1, result.get("id"));
|
||||
Assert.assertEquals("yx.mm.com", result.get("photoPath"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getKeyWithDotTest () {
|
||||
Map<String, Object> dataMap = new HashMap<>(16);
|
||||
dataMap.put("aa", "value0");
|
||||
dataMap.put("aa.bb.cc", "value111111");// key 是类名 格式 带 ' . '
|
||||
|
||||
final BeanPath pattern = BeanPath.create("'aa.bb.cc'");
|
||||
Assert.assertEquals("value111111", pattern.get(dataMap));
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class CaseInsensitiveMapTest {
|
||||
|
||||
@Test
|
||||
@ -25,8 +26,8 @@ public class CaseInsensitiveMapTest {
|
||||
@Test
|
||||
public void mergeTest(){
|
||||
//https://github.com/dromara/hutool/issues/2086
|
||||
final Pair<String, String> b = new Pair<>("a", "value");
|
||||
final Pair<String, String> a = new Pair<>("A", "value");
|
||||
final Map.Entry<String, String> b = MapUtil.entry("a", "value");
|
||||
final Map.Entry<String, String> a = MapUtil.entry("A", "value");
|
||||
final CaseInsensitiveMap<Object, Object> map = new CaseInsensitiveMap<>();
|
||||
map.merge(b.getKey(), b.getValue(), (A, B) -> A);
|
||||
map.merge(a.getKey(), a.getValue(), (A, B) -> A);
|
||||
|
@ -3,7 +3,7 @@ package cn.hutool.json;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.MutablePair;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
|
||||
import cn.hutool.core.map.CaseInsensitiveTreeMap;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
@ -140,7 +140,7 @@ public final class InternalJSONUtil {
|
||||
* @param value 值
|
||||
* @return JSONObject
|
||||
*/
|
||||
static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Filter<MutablePair<String, Object>> filter) {
|
||||
static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Filter<MutableEntry<String, Object>> filter) {
|
||||
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
||||
final int last = path.length - 1;
|
||||
JSONObject target = jsonObject;
|
||||
|
@ -5,7 +5,7 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutableObj;
|
||||
import cn.hutool.core.lang.mutable.MutablePair;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.text.StrJoiner;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.json.serialize.JSONWriter;
|
||||
@ -416,10 +416,10 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
* @return 替换的值,即之前的值
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public Object set(final int index, Object element, final Filter<MutablePair<Integer, Object>> filter) {
|
||||
public Object set(final int index, Object element, final Filter<MutableEntry<Integer, Object>> filter) {
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != filter) {
|
||||
final MutablePair<Integer, Object> pair = new MutablePair<>(index, element);
|
||||
final MutableEntry<Integer, Object> pair = new MutableEntry<>(index, element);
|
||||
if (filter.accept(pair)) {
|
||||
// 使用修改后的值
|
||||
element = pair.getValue();
|
||||
@ -515,7 +515,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
* @return JSON字符串
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public String toJSONString(final int indentFactor, final Filter<MutablePair<Integer, Object>> filter) {
|
||||
public String toJSONString(final int indentFactor, final Filter<MutableEntry<Integer, Object>> filter) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0, filter).toString();
|
||||
@ -539,12 +539,12 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
* @throws JSONException JSON相关异常
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutablePair<Integer, Object>> filter) throws JSONException {
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutableEntry<Integer, Object>> filter) throws JSONException {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
|
||||
.beginArray();
|
||||
|
||||
CollUtil.forEach(this, (value, index) -> {
|
||||
final MutablePair<Integer, Object> pair = new MutablePair<>(index, value);
|
||||
final MutableEntry<Integer, Object> pair = new MutableEntry<>(index, value);
|
||||
if (null == filter || filter.accept(pair)) {
|
||||
jsonWriter.writeValue(pair.getValue());
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package cn.hutool.json;
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.MutablePair;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.MapWrapper;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
@ -126,7 +126,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONObject(final Object source, final JSONConfig config, final Filter<MutablePair<String, Object>> filter) {
|
||||
public JSONObject(final Object source, final JSONConfig config, final Filter<MutableEntry<String, Object>> filter) {
|
||||
this(DEFAULT_CAPACITY, config);
|
||||
ObjectMapper.of(source).map(this, filter);
|
||||
}
|
||||
@ -233,7 +233,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONObject set(final String key, final Object value, final Filter<MutablePair<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
|
||||
public JSONObject set(final String key, final Object value, final Filter<MutableEntry<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
|
||||
put(key, value, filter, checkDuplicate);
|
||||
return this;
|
||||
}
|
||||
@ -260,7 +260,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @throws JSONException 值是无穷数字、键重复抛出异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONObject setOnce(final String key, final Object value, final Filter<MutablePair<String, Object>> filter) throws JSONException {
|
||||
public JSONObject setOnce(final String key, final Object value, final Filter<MutableEntry<String, Object>> filter) throws JSONException {
|
||||
return set(key, value, filter, true);
|
||||
}
|
||||
|
||||
@ -378,7 +378,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @return JSON字符串
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public String toJSONString(final int indentFactor, final Filter<MutablePair<String, Object>> filter) {
|
||||
public String toJSONString(final int indentFactor, final Filter<MutableEntry<String, Object>> filter) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0, filter).toString();
|
||||
@ -402,12 +402,12 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @throws JSONException JSON相关异常
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutablePair<String, Object>> filter) throws JSONException {
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutableEntry<String, Object>> filter) throws JSONException {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
|
||||
.beginObj();
|
||||
this.forEach((key, value) -> {
|
||||
if (null != filter) {
|
||||
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
|
||||
final MutableEntry<String, Object> pair = new MutableEntry<>(key, value);
|
||||
if (filter.accept(pair)) {
|
||||
// 使用修改后的键值对
|
||||
jsonWriter.writeField(pair.getKey(), pair.getValue());
|
||||
@ -439,14 +439,14 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private Object put(String key, Object value, final Filter<MutablePair<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
|
||||
private Object put(String key, Object value, final Filter<MutableEntry<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
|
||||
if (null == key) {
|
||||
return this;
|
||||
}
|
||||
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != filter) {
|
||||
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
|
||||
final MutableEntry<String, Object> pair = new MutableEntry<>(key, value);
|
||||
if (filter.accept(pair)) {
|
||||
// 使用修改后的键值对
|
||||
key = pair.getKey();
|
||||
|
@ -2,7 +2,7 @@ package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutablePair;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
|
||||
/**
|
||||
* JSON字符串解析器
|
||||
@ -41,7 +41,7 @@ public class JSONParser {
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
*/
|
||||
public void parseTo(final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
|
||||
public void parseTo(final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
final JSONTokener tokener = this.tokener;
|
||||
|
||||
char c;
|
||||
|
@ -6,7 +6,7 @@ import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutablePair;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.reflect.TypeUtil;
|
||||
@ -67,7 +67,7 @@ public class ObjectMapper {
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void map(final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
|
||||
public void map(final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
final Object source = this.source;
|
||||
if (null == source) {
|
||||
return;
|
||||
@ -179,7 +179,7 @@ public class ObjectMapper {
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @since 5.3.1
|
||||
*/
|
||||
private static void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
|
||||
private static void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
final Enumeration<String> keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
final String key = keys.nextElement();
|
||||
@ -196,7 +196,7 @@ public class ObjectMapper {
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
*/
|
||||
private static void mapFromStr(final CharSequence source, final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
|
||||
private static void mapFromStr(final CharSequence source, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
final String jsonStr = StrUtil.trim(source);
|
||||
if (StrUtil.startWith(jsonStr, '<')) {
|
||||
// 可能为XML
|
||||
@ -226,7 +226,7 @@ public class ObjectMapper {
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作
|
||||
*/
|
||||
private static void mapFromTokener(final JSONTokener x, final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
|
||||
private static void mapFromTokener(final JSONTokener x, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
JSONParser.of(x).parseTo(jsonObject, filter);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user