This commit is contained in:
Looly 2023-09-01 11:30:48 +08:00
parent a8b6263acf
commit 56abd26590
7 changed files with 84 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.exception.ExceptionUtil;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrJoiner;
import org.dromara.hutool.core.text.StrUtil;
@ -553,6 +554,23 @@ public class ArrayUtil extends PrimitiveArrayUtil {
return ArrayWrapper.of(array).setOrAppend(index, value).getRaw();
}
/**
* 将元素值设置为数组的某个位置当index小于数组的长度时替换指定位置的值否则追加{@code null}{@code 0}直到到达index后设置值
*
* @param <A> 数组类型
* @param array 已有数组
* @param index 位置大于等于长度则追加否则替换
* @param value 新值
* @return 新数组或原有数组
* @since 6.0.0
*/
public static <A> A setOrPadding(final A array, final int index, final Object value) {
if (index == 0 && isEmpty(array)) {
return ofArray(value, null == array ? null : array.getClass().getComponentType());
}
return ArrayWrapper.of(array).setOrPadding(index, value).getRaw();
}
/**
* 从数组中的指定位置开始按顺序使用新元素替换旧元素<br>
* <ul>

View File

@ -2,8 +2,10 @@ package org.dromara.hutool.core.array;
import org.dromara.hutool.core.collection.iter.ArrayIter;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.func.Wrapper;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.reflect.ClassUtil;
import org.dromara.hutool.core.util.ObjUtil;
import java.lang.reflect.Array;
@ -266,6 +268,46 @@ public class ArrayWrapper<A, E> implements Wrapper<A>, Iterable<E> {
}
// endregion
/**
* 将元素值设置为数组的某个位置当index小于数组的长度时替换指定位置的值否则追加{@code null}{@code 0}直到到达index后设置值
*
* @param index 位置
* @param value 新元素或新数组
* @return this
* @since 6.0.0
*/
@SuppressWarnings("unchecked")
public ArrayWrapper<A, E> setOrPadding(final int index, final E value) {
return setOrPadding(index, value, (E) ClassUtil.getDefaultValue(this.componentType));
}
/**
* 将元素值设置为数组的某个位置当index小于数组的长度时替换指定位置的值否则追加{@code paddingElement}直到到达index后设置值
*
* @param index 位置
* @param value 新元素或新数组
* @param paddingElement 填充
* @return this
* @since 6.0.0
*/
public ArrayWrapper<A, E> setOrPadding(final int index, final E value, final E paddingElement) {
if (index < this.length) {
Array.set(array, index, value);
} else {
// issue#3286, 增加安全检查最多增加2倍
if(index > (length + 1) * 2) {
throw new HutoolException("Index is too large:", index);
}
for (int i = length; i < index; i++) {
append(paddingElement);
}
append(value);
}
return this;
}
/**
* 将元素值设置为数组的某个位置当给定的index大于等于数组长度则追加
*
@ -314,7 +356,7 @@ public class ArrayWrapper<A, E> implements Wrapper<A>, Iterable<E> {
* @return 新数组
*/
public ArrayWrapper<A, E> insert(final int index, final E element) {
return insertArray(index, ArrayUtil.ofArray(element));
return insertArray(index, ArrayUtil.ofArray(element, this.componentType));
}
/**

View File

@ -15,6 +15,7 @@ package org.dromara.hutool.core.bean;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.text.StrUtil;

View File

@ -337,11 +337,10 @@ public class BeanUtil {
if (bean instanceof Map) {
((Map) bean).put(fieldNameOrIndex, value);
} else if (bean instanceof List) {
// 相对于5.x逻辑变更与数组逻辑保持一致
ListUtil.setOrAppend((List) bean, Convert.toInt(fieldNameOrIndex), value);
ListUtil.setOrPadding((List) bean, Convert.toInt(fieldNameOrIndex), value);
} else if (ArrayUtil.isArray(bean)) {
// issue#3008追加产生新数组此处返回新数组
return ArrayUtil.setOrAppend(bean, Convert.toInt(fieldNameOrIndex), value);
return ArrayUtil.setOrPadding(bean, Convert.toInt(fieldNameOrIndex), value);
} else {
// 普通Bean对象
FieldUtil.setFieldValue(bean, fieldNameOrIndex, value);

View File

@ -270,7 +270,7 @@ public class FieldUtil {
* @throws HutoolException 包装IllegalAccessException异常
*/
public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws HutoolException {
Assert.notNull(obj);
Assert.notNull(obj, "Object must be not null !");
Assert.notBlank(fieldName);
final Field field = getField((obj instanceof Class) ? (Class<?>) obj : obj.getClass(), fieldName);

View File

@ -13,6 +13,7 @@
package org.dromara.hutool.core.array;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.junit.jupiter.api.Assertions;
@ -551,6 +552,14 @@ public class ArrayUtilTest {
Assertions.assertArrayEquals(new int[]{2}, o);
}
@Test
void setOrPaddingTest(){
final String[] arr = new String[0];
final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good");
Console.log(newArr);
Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr);
}
@Test
public void getAnyTest() {
final String[] a = {"a", "b", "c", "d", "e"};

View File

@ -148,6 +148,15 @@ public class BeanPathTest {
Assertions.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
}
@Test
public void putTest() {
final Map<String, Object> map = new HashMap<>();
BeanPath beanPath = BeanPath.of("list[1].name");
beanPath.set(map, "张三");
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
}
@Test
public void putByPathTest() {
final Dict dict = new Dict();