cn.hippo4j.starter.toolkit.ArrayUtil Maven / Gradle / Ivy
package cn.hippo4j.starter.toolkit;
import java.lang.reflect.Array;
/**
* Array util.
*
* @author chen.ma
* @date 2021/7/5 21:54
*/
public class ArrayUtil {
public static T[] addAll(final T[] array1, @SuppressWarnings("unchecked") final T... array2) {
if (array1 == null) {
return clone(array2);
} else if (array2 == null) {
return clone(array1);
}
final Class> type1 = array1.getClass().getComponentType();
@SuppressWarnings("unchecked") final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
System.arraycopy(array1, 0, joinedArray, 0, array1.length);
try {
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
} catch (final ArrayStoreException ase) {
final Class> type2 = array2.getClass().getComponentType();
if (!type1.isAssignableFrom(type2)) {
throw new IllegalArgumentException("Cannot store " + type2.getName() + " in an array of "
+ type1.getName(), ase);
}
throw ase;
}
return joinedArray;
}
public static T[] clone(final T[] array) {
if (array == null) {
return null;
}
return array.clone();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy