提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.util.collection; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollectionUtil; |
|
4 |
import cn.hutool.core.collection.IterUtil; |
|
5 |
import cn.hutool.core.util.ArrayUtil; |
|
6 |
|
|
7 |
import java.util.Collection; |
|
8 |
import java.util.function.Consumer; |
|
9 |
import java.util.function.Function; |
|
10 |
|
|
11 |
import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; |
|
12 |
|
|
13 |
/** |
|
14 |
* Array 工具类 |
|
15 |
* |
|
16 |
* @author iailab |
|
17 |
*/ |
|
18 |
public class ArrayUtils { |
|
19 |
|
|
20 |
/** |
|
21 |
* 将 object 和 newElements 合并成一个数组 |
|
22 |
* |
|
23 |
* @param object 对象 |
|
24 |
* @param newElements 数组 |
|
25 |
* @param <T> 泛型 |
|
26 |
* @return 结果数组 |
|
27 |
*/ |
|
28 |
@SafeVarargs |
|
29 |
public static <T> Consumer<T>[] append(Consumer<T> object, Consumer<T>... newElements) { |
|
30 |
if (object == null) { |
|
31 |
return newElements; |
|
32 |
} |
|
33 |
Consumer<T>[] result = ArrayUtil.newArray(Consumer.class, 1 + newElements.length); |
|
34 |
result[0] = object; |
|
35 |
System.arraycopy(newElements, 0, result, 1, newElements.length); |
|
36 |
return result; |
|
37 |
} |
|
38 |
|
|
39 |
public static <T, V> V[] toArray(Collection<T> from, Function<T, V> mapper) { |
|
40 |
return toArray(convertList(from, mapper)); |
|
41 |
} |
|
42 |
|
|
43 |
@SuppressWarnings("unchecked") |
|
44 |
public static <T> T[] toArray(Collection<T> from) { |
|
45 |
if (CollectionUtil.isEmpty(from)) { |
|
46 |
return (T[]) (new Object[0]); |
|
47 |
} |
|
48 |
return ArrayUtil.toArray(from, (Class<T>) IterUtil.getElementType(from.iterator())); |
|
49 |
} |
|
50 |
|
|
51 |
public static <T> T get(T[] array, int index) { |
|
52 |
if (null == array || index >= array.length) { |
|
53 |
return null; |
|
54 |
} |
|
55 |
return array[index]; |
|
56 |
} |
|
57 |
|
|
58 |
} |