1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import java.lang.reflect.Array;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashSet;
17
18
19 public class ArrayUtils extends org.apache.commons.lang.ArrayUtils
20 {
21
22
23
24
25
26 public static String toString(Object array, int maxElements)
27 {
28 String result;
29
30 Class componentType = array.getClass().getComponentType();
31 if (Object.class.isAssignableFrom(componentType))
32 {
33 result = ArrayUtils.toString((ArrayUtils.subarray((Object[]) array, 0, maxElements)));
34 }
35 else if (componentType.equals(Boolean.TYPE))
36 {
37 result = ArrayUtils.toString((ArrayUtils.subarray((boolean[]) array, 0, maxElements)));
38 }
39 else if (componentType.equals(Byte.TYPE))
40 {
41 result = ArrayUtils.toString((ArrayUtils.subarray((byte[]) array, 0, maxElements)));
42 }
43 else if (componentType.equals(Character.TYPE))
44 {
45 result = ArrayUtils.toString((ArrayUtils.subarray((char[]) array, 0, maxElements)));
46 }
47 else if (componentType.equals(Short.TYPE))
48 {
49 result = ArrayUtils.toString((ArrayUtils.subarray((short[]) array, 0, maxElements)));
50 }
51 else if (componentType.equals(Integer.TYPE))
52 {
53 result = ArrayUtils.toString((ArrayUtils.subarray((int[]) array, 0, maxElements)));
54 }
55 else if (componentType.equals(Long.TYPE))
56 {
57 result = ArrayUtils.toString((ArrayUtils.subarray((long[]) array, 0, maxElements)));
58 }
59 else if (componentType.equals(Float.TYPE))
60 {
61 result = ArrayUtils.toString((ArrayUtils.subarray((float[]) array, 0, maxElements)));
62 }
63 else if (componentType.equals(Double.TYPE))
64 {
65 result = ArrayUtils.toString((ArrayUtils.subarray((double[]) array, 0, maxElements)));
66 }
67 else
68 {
69 throw new IllegalArgumentException("Unknown array service type: " + componentType.getName());
70 }
71
72 if (Array.getLength(array) > maxElements)
73 {
74 StringBuffer buf = new StringBuffer(result);
75 buf.insert(buf.length() - 1, " [..]");
76 result = buf.toString();
77 }
78
79 return result;
80
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public static Object[] toArrayOfComponentType(Object[] objects, Class clazz)
101 {
102 if (objects == null || objects.getClass().getComponentType().equals(clazz))
103 {
104 return objects;
105 }
106
107 if (clazz == null)
108 {
109 throw new IllegalArgumentException("Array target class must not be null");
110 }
111
112 Object[] result = (Object[]) Array.newInstance(clazz, objects.length);
113 System.arraycopy(objects, 0, result, 0, objects.length);
114 return result;
115 }
116
117 public static Object[] setDifference(Object[] a, Object[] b)
118 {
119 Collection aCollecn = new HashSet(Arrays.asList(a));
120 Collection bCollecn = Arrays.asList(b);
121 aCollecn.removeAll(bCollecn);
122 return aCollecn.toArray();
123 }
124
125 public static String[] setDifference(String[] a, String[] b)
126 {
127 Object[] ugly = setDifference((Object[])a, b);
128 String[] copy = new String[ugly.length];
129 System.arraycopy(ugly, 0, copy, 0, ugly.length);
130 return copy;
131 }
132
133 }