fix method

This commit is contained in:
Looly 2021-06-16 02:32:12 +08:00
parent ca2952543a
commit 26064f146c
6 changed files with 36 additions and 128 deletions

View File

@ -1141,6 +1141,9 @@ public class CollUtil {
}
Collection<T> collection2 = ObjectUtil.clone(collection);
if (isEmpty(collection2)) {
return collection2;
}
try {
collection2.clear();
} catch (UnsupportedOperationException e) {
@ -1176,24 +1179,6 @@ public class CollUtil {
return edit(collection, t -> filter.accept(t) ? t : null);
}
/**
* 过滤<br>
* 过滤过程通过传入的Filter实现来过滤返回需要的元素内容这个Filter实现可以实现以下功能
*
* <pre>
* 1过滤出需要的对象{@link Filter#accept(Object)}方法返回true的对象将被加入结果集合中
* </pre>
*
* @param <T> 集合元素类型
* @param list 集合
* @param filter 过滤器
* @return 过滤后的数组
* @since 4.1.8
*/
public static <T> List<T> filterNew(List<T> list, Filter<T> filter) {
return ListUtil.editNew(list, t -> filter.accept(t) ? t : null);
}
/**
* 去掉集合中的多个元素此方法直接修改原集合
*

View File

@ -2,7 +2,6 @@ package cn.hutool.core.collection;
import cn.hutool.core.comparator.PinyinComparator;
import cn.hutool.core.comparator.PropertyComparator;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Matcher;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
@ -423,59 +422,6 @@ public class ListUtil {
return result;
}
/**
* 编辑列表<br>
* 过滤过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
*
* <pre>
* 1过滤出需要的对象如果返回null表示这个元素对象抛弃
* 2修改元素对象返回集合中为修改后的对象
* </pre>
* 注意此方法会修改原List
*
* @param <T> 集合元素类型
* @param list 集合
* @param editor 编辑器接口
* @return 过滤后的数组
* @since 4.1.8
*/
public static <T> List<T> editNew(List<T> list, Editor<T> editor) {
return (List<T>) CollUtil.edit(list, editor);
}
/**
* 编辑列表<br>
* 过滤过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
*
* <pre>
* 1过滤出需要的对象如果返回null表示这个元素对象抛弃
* 2修改元素对象返回集合中为修改后的对象
* </pre>
* 注意此方法会修改原List
*
* @param <T> 集合元素类型
* @param list 集合
* @param editor 编辑器接口
* @return 过滤后的数组
* @since 4.1.8
*/
public static <T> List<T> edit(List<T> list, Editor<T> editor) {
if (null == list || null == editor) {
return list;
}
final int size = list.size();
T ele;
for (int i = 0; i < size; i++) {
ele = list.get(i);
ele = editor.edit(ele);
if(null != ele){
list.set(i, ele);
}
}
return list;
}
/**
* 获取匹配规则定义中匹配到元素的最后位置<br>
* 此方法对于某些无序集合的位置信息以转换为数组后的位置为准

View File

@ -609,11 +609,11 @@ public class MapUtil {
// ----------------------------------------------------------------------------------------------- filter
/**
* 过滤<br>
* 过滤过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
* 编辑Map<br>
* 编辑过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
*
* <pre>
* 1过滤出需要的对象如果返回null表示这个元素对象抛弃
* 1过滤出需要的对象如果返回{@code null}表示这个元素对象抛弃
* 2修改元素对象返回集合中为修改后的对象
* </pre>
*
@ -621,19 +621,24 @@ public class MapUtil {
* @param <V> Value类型
* @param map Map
* @param editor 编辑器接口
* @return 过滤后的Map
* @return 编辑后的Map
*/
public static <K, V> Map<K, V> edit(Map<K, V> map, Editor<Entry<K, V>> editor) {
if (null == map || null == editor) {
return map;
}
final Map<K, V> map2 = ObjectUtil.clone(map);
Map<K, V> map2 = ObjectUtil.clone(map);
if (isEmpty(map2)) {
return map2;
}
try {
map2.clear();
} catch (UnsupportedOperationException e) {
// 克隆后的对象不支持清空说明为不可变集合对象使用默认的ArrayList保存结果
map2 = new HashMap<>();
}
map2.clear();
Entry<K, V> modified;
for (Entry<K, V> entry : map.entrySet()) {
modified = editor.edit(entry);

View File

@ -575,21 +575,26 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
/**
* 过滤<br>
* 过滤过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
* 编辑数组<br>
* 编辑过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
*
* <pre>
* 1过滤出需要的对象如果返回null表示这个元素对象抛弃
* 1过滤出需要的对象如果返回{@code null}表示这个元素对象抛弃
* 2修改元素对象返回集合中为修改后的对象
* </pre>
* <p>
*
* @param <T> 数组元素类型
* @param array 数组
* @param editor 编辑器接口
* @return 过滤后的数组
* @since 5.3.3
*/
public static <T> T[] filter(T[] array, Editor<T> editor) {
ArrayList<T> list = new ArrayList<>(array.length);
public static <T> T[] edit(T[] array, Editor<T> editor) {
if (null == editor) {
return array;
}
final ArrayList<T> list = new ArrayList<>(array.length);
T modified;
for (T t : array) {
modified = editor.edit(t);
@ -597,28 +602,8 @@ public class ArrayUtil extends PrimitiveArrayUtil {
list.add(modified);
}
}
return list.toArray(Arrays.copyOf(array, list.size()));
}
/**
* 编辑数组<br>
* 编辑过程通过传入的Editor实现来返回需要的元素内容这个Editor实现可以实现以下功能
*
* <pre>
* 1修改元素对象返回集合中为修改后的对象
* </pre>
* <p>
* 注意此方法会修改原数组
*
* @param <T> 数组元素类型
* @param array 数组
* @param editor 编辑器接口
* @since 5.3.3
*/
public static <T> void edit(T[] array, Editor<T> editor) {
for (int i = 0; i < array.length; i++) {
array[i] = editor.edit(array[i]);
}
final T[] result = newArray(array.getClass().getComponentType(), list.size());
return list.toArray(result);
}
/**
@ -636,18 +621,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.2.1
*/
public static <T> T[] filter(T[] array, Filter<T> filter) {
if (null == filter) {
return array;
}
final ArrayList<T> list = new ArrayList<>(array.length);
for (T t : array) {
if (filter.accept(t)) {
list.add(t);
}
}
final T[] result = newArray(array.getClass().getComponentType(), list.size());
return list.toArray(result);
return edit(array, t -> filter.accept(t) ? t : null);
}
/**
@ -659,7 +633,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.2.2
*/
public static <T> T[] removeNull(T[] array) {
return filter(array, (Editor<T>) t -> {
return edit(array, t -> {
// 返回null便不加入集合
return t;
});
@ -697,7 +671,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.2.1
*/
public static String[] nullToEmpty(String[] array) {
return filter(array, (Editor<String>) t -> null == t ? StrUtil.EMPTY : t);
return edit(array, t -> null == t ? StrUtil.EMPTY : t);
}
/**

View File

@ -40,7 +40,7 @@ public class ListUtilTest {
@Test
public void editTest(){
List<String> a = ListUtil.toLinkedList("1", "2", "3");
final List<String> filter = ListUtil.edit(a, str -> "edit" + str);
final List<String> filter = (List<String>) CollUtil.edit(a, str -> "edit" + str);
Assert.assertEquals("edit1", filter.get(0));
Assert.assertEquals("edit2", filter.get(1));
Assert.assertEquals("edit3", filter.get(2));

View File

@ -1,8 +1,6 @@
package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Filter;
import org.junit.Assert;
import org.junit.Test;
@ -79,23 +77,23 @@ public class ArrayUtilTest {
}
@Test
public void filterTest() {
public void filterEditTest() {
Integer[] a = {1, 2, 3, 4, 5, 6};
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t : null);
Integer[] filter = ArrayUtil.edit(a, t -> (t % 2 == 0) ? t : null);
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
}
@Test
public void filterTestForFilter() {
Integer[] a = {1, 2, 3, 4, 5, 6};
Integer[] filter = ArrayUtil.filter(a, (Filter<Integer>) t -> t % 2 == 0);
Integer[] filter = ArrayUtil.filter(a, t -> t % 2 == 0);
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
}
@Test
public void filterTestForEditor() {
public void editTest() {
Integer[] a = {1, 2, 3, 4, 5, 6};
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t * 10 : t);
Integer[] filter = ArrayUtil.edit(a, t -> (t % 2 == 0) ? t * 10 : t);
Assert.assertArrayEquals(filter, new Integer[]{1, 20, 3, 40, 5, 60});
}