mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
setOrPadding增加重载,可选限制index大小
This commit is contained in:
parent
65ba22d34b
commit
dcf66c8d93
@ -303,11 +303,25 @@ public class ArrayWrapper<A, E> implements Wrapper<A>, Iterable<E> {
|
|||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public ArrayWrapper<A, E> setOrPadding(final int index, final E value, final E paddingElement) {
|
public ArrayWrapper<A, E> setOrPadding(final int index, final E value, final E paddingElement) {
|
||||||
|
return setOrPadding(index, value, paddingElement, (this.length + 1) * 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将元素值设置为数组的某个位置,当index小于数组的长度时,替换指定位置的值,否则追加{@code paddingElement}直到到达index后,设置值
|
||||||
|
*
|
||||||
|
* @param index 位置
|
||||||
|
* @param value 新元素或新数组
|
||||||
|
* @param paddingElement 填充
|
||||||
|
* @param indexLimit 索引限制
|
||||||
|
* @return this
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public ArrayWrapper<A, E> setOrPadding(final int index, final E value, final E paddingElement, final int indexLimit) {
|
||||||
if (index < this.length) {
|
if (index < this.length) {
|
||||||
Array.set(array, index, value);
|
Array.set(array, index, value);
|
||||||
} else {
|
} else {
|
||||||
// issue#3286, 增加安全检查,最多增加10倍
|
// issue#3286, 增加安全检查,最多增加10倍
|
||||||
Validator.checkIndexLimit(index, this.length);
|
Validator.checkIndexLimit(index, indexLimit);
|
||||||
|
|
||||||
for (int i = length; i < index; i++) {
|
for (int i = length; i < index; i++) {
|
||||||
append(paddingElement);
|
append(paddingElement);
|
||||||
|
@ -473,13 +473,31 @@ public class ListUtil {
|
|||||||
* @since 5.8.4
|
* @since 5.8.4
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> setOrPadding(final List<T> list, final int index, final T element, final T paddingElement) {
|
public static <T> List<T> setOrPadding(final List<T> list, final int index, final T element, final T paddingElement) {
|
||||||
|
return setOrPadding(list, index, element, paddingElement, (list.size() + 1) * 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在指定位置设置元素。当index小于List的长度时,替换指定位置的值,否则追加{@code paddingElement}直到到达index后,设置值
|
||||||
|
*
|
||||||
|
* @param <T> 元素类型
|
||||||
|
* @param list List列表
|
||||||
|
* @param index 位置
|
||||||
|
* @param element 新元素
|
||||||
|
* @param paddingElement 填充的值
|
||||||
|
* @param indexLimit 最大索引限制
|
||||||
|
* @return 原List
|
||||||
|
* @since 5.8.28
|
||||||
|
*/
|
||||||
|
public static <T> List<T> setOrPadding(final List<T> list, final int index, final T element, final T paddingElement, final int indexLimit) {
|
||||||
Assert.notNull(list, "List must be not null !");
|
Assert.notNull(list, "List must be not null !");
|
||||||
final int size = list.size();
|
final int size = list.size();
|
||||||
if (index < size) {
|
if (index < size) {
|
||||||
list.set(index, element);
|
list.set(index, element);
|
||||||
} else {
|
} else {
|
||||||
// issue#3286, 增加安全检查,最多增加10倍
|
if(indexLimit > 0){
|
||||||
Validator.checkIndexLimit(index, size);
|
// issue#3286, 增加安全检查
|
||||||
|
Validator.checkIndexLimit(index, indexLimit);
|
||||||
|
}
|
||||||
for (int i = size; i < index; i++) {
|
for (int i = size; i < index; i++) {
|
||||||
list.add(paddingElement);
|
list.add(paddingElement);
|
||||||
}
|
}
|
||||||
|
@ -1247,7 +1247,7 @@ public class Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查给定的index是否超出长度限制,默认检查超出倍数(10倍),此方法主要用于内部,检查包括:
|
* 检查给定的index是否超出长度限制:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>数组调用setOrPadding时,最多允许padding的长度</li>
|
* <li>数组调用setOrPadding时,最多允许padding的长度</li>
|
||||||
* <li>List调用setOrPadding时,最多允许padding的长度</li>
|
* <li>List调用setOrPadding时,最多允许padding的长度</li>
|
||||||
@ -1255,13 +1255,13 @@ public class Validator {
|
|||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @param index 索引
|
* @param index 索引
|
||||||
* @param size 数组、列表长度
|
* @param limit 限制大小
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public static void checkIndexLimit(final int index, final int size) {
|
public static void checkIndexLimit(final int index, final int limit) {
|
||||||
// issue#3286, 增加安全检查,最多增加10倍
|
// issue#3286, 增加安全检查,最多增加10倍
|
||||||
if (index > (size + 1) * 10) {
|
if (index > limit) {
|
||||||
throw new ValidateException("Index [{}] is too large for size: [{}]", index, size);
|
throw new ValidateException("Index [{}] is too large for limit: [{}]", index, limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -455,7 +455,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
// issue#3286, 如果用户指定的index太大,容易造成Java heap space错误。
|
// issue#3286, 如果用户指定的index太大,容易造成Java heap space错误。
|
||||||
if (!config.isIgnoreNullValue()) {
|
if (!config.isIgnoreNullValue()) {
|
||||||
// issue#3286, 增加安全检查,最多增加10倍
|
// issue#3286, 增加安全检查,最多增加10倍
|
||||||
Validator.checkIndexLimit(index, this.size());
|
Validator.checkIndexLimit(index, (this.size() + 1) * 10);
|
||||||
while (index != this.size()) {
|
while (index != this.size()) {
|
||||||
// 非末尾,则填充null
|
// 非末尾,则填充null
|
||||||
this.add(null);
|
this.add(null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user