一、前言
关于esper源码包中的com.espertech.esper.util.CollectionUtil集合工具类,对数组对象追加expandAddElement、整型数组复制追加addValue、字符串数组内容查找findItem定位、字符串数组复制排序copySortArray、排序比较两数组是否相当sortCompare、类集转换为字符串处理等操作。
二、源码说明
package com.espertech.esper.util;@b@@b@import java.lang.reflect.Array;@b@import java.util.Arrays;@b@import java.util.Iterator;@b@import java.util.Set;@b@@b@public class CollectionUtil<T>@b@{@b@ public static Object expandAddElement(Object array, Object[] elementsToAdd)@b@ {@b@ Class cl = array.getClass();@b@ if (!(cl.isArray())) return null;@b@ int length = Array.getLength(array);@b@ int newLength = length + elementsToAdd.length;@b@ Class componentType = array.getClass().getComponentType();@b@ Object newArray = Array.newInstance(componentType, newLength);@b@ System.arraycopy(array, 0, newArray, 0, length);@b@ for (int i = 0; i < elementsToAdd.length; ++i)@b@ Array.set(newArray, length + i, elementsToAdd[i]);@b@@b@ return newArray;@b@ }@b@@b@ public static int[] addValue(int[] ints, int i) {@b@ int[] copy = new int[ints.length + 1];@b@ System.arraycopy(ints, 0, copy, 0, ints.length);@b@ copy[ints.length] = i;@b@ return copy;@b@ }@b@@b@ public static int findItem(String[] items, String item) {@b@ for (int i = 0; i < items.length; ++i)@b@ if (items[i].equals(item))@b@ return i;@b@@b@@b@ return -1;@b@ }@b@@b@ public static int[] intArray(Set<Integer> set)@b@ {@b@ if (set == null)@b@ {@b@ return new int[0];@b@ }@b@ int[] result = new int[set.size()];@b@ int index = 0;@b@ for (Integer value : set)@b@ result[(index++)] = value.intValue();@b@@b@ return result;@b@ }@b@@b@ public static String[] copySortArray(String[] values) {@b@ if (values == null)@b@ return null;@b@@b@ String[] copy = new String[values.length];@b@ System.arraycopy(values, 0, copy, 0, values.length);@b@ Arrays.sort(copy);@b@ return copy;@b@ }@b@@b@ public static boolean sortCompare(String[] valuesOne, String[] valuesTwo) {@b@ if (valuesOne == null)@b@ return (valuesTwo == null);@b@@b@ if (valuesTwo == null)@b@ return false;@b@@b@ String[] copyOne = copySortArray(valuesOne);@b@ String[] copyTwo = copySortArray(valuesTwo);@b@ return Arrays.equals(copyOne, copyTwo);@b@ }@b@@b@ public static <T> String toString(Set<T> set)@b@ {@b@ if (set == null)@b@ {@b@ return "null";@b@ }@b@ if (set.isEmpty())@b@ {@b@ return "";@b@ }@b@ StringBuilder buf = new StringBuilder();@b@ String delimiter = "";@b@ Iterator i$ = set.iterator();@b@ while (true) { Object t;@b@ while (true) { if (!(i$.hasNext())) break label81; t = i$.next();@b@@b@ if (t != null)@b@ break;@b@ }@b@@b@ buf.append(delimiter);@b@ buf.append(t);@b@ delimiter = ", ";@b@ }@b@ label81: return buf.toString();@b@ }@b@@b@ public static boolean compare(String[] otherIndexProps, String[] thisIndexProps) {@b@ if ((otherIndexProps != null) && (thisIndexProps != null))@b@ return Arrays.equals(otherIndexProps, thisIndexProps);@b@@b@ return ((otherIndexProps == null) && (thisIndexProps == null));@b@ }@b@}