KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > ArrayUtils


1 /*
2  * $Id: ArrayUtils.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.lang.reflect.Array JavaDoc;
14
15 // @ThreadSafe
16
public class ArrayUtils extends org.apache.commons.lang.ArrayUtils
17 {
18
19     /**
20      * Like {@link #toString(Object)} but considers at most <code>maxElements</code>
21      * values; overflow is indicated by an appended "[..]" ellipsis.
22      */

23     public static String JavaDoc toString(Object JavaDoc array, int maxElements)
24     {
25         String JavaDoc result;
26
27         Class JavaDoc componentType = array.getClass().getComponentType();
28         if (componentType.equals(Object JavaDoc.class))
29         {
30             result = ArrayUtils.toString((ArrayUtils.subarray((Object JavaDoc[])array, 0, maxElements)));
31         }
32         else if (componentType.equals(Boolean.TYPE))
33         {
34             result = ArrayUtils.toString((ArrayUtils.subarray((boolean[])array, 0, maxElements)));
35         }
36         else if (componentType.equals(Byte.TYPE))
37         {
38             result = ArrayUtils.toString((ArrayUtils.subarray((byte[])array, 0, maxElements)));
39         }
40         else if (componentType.equals(Character.TYPE))
41         {
42             result = ArrayUtils.toString((ArrayUtils.subarray((char[])array, 0, maxElements)));
43         }
44         else if (componentType.equals(Short.TYPE))
45         {
46             result = ArrayUtils.toString((ArrayUtils.subarray((short[])array, 0, maxElements)));
47         }
48         else if (componentType.equals(Integer.TYPE))
49         {
50             result = ArrayUtils.toString((ArrayUtils.subarray((int[])array, 0, maxElements)));
51         }
52         else if (componentType.equals(Long.TYPE))
53         {
54             result = ArrayUtils.toString((ArrayUtils.subarray((long[])array, 0, maxElements)));
55         }
56         else if (componentType.equals(Float.TYPE))
57         {
58             result = ArrayUtils.toString((ArrayUtils.subarray((float[])array, 0, maxElements)));
59         }
60         else if (componentType.equals(Double.TYPE))
61         {
62             result = ArrayUtils.toString((ArrayUtils.subarray((double[])array, 0, maxElements)));
63         }
64         else
65         {
66             throw new IllegalArgumentException JavaDoc("Unknown array component type: " + componentType.getName());
67         }
68
69         if (Array.getLength(array) > maxElements)
70         {
71             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(result);
72             buf.insert(buf.length() - 1, " [..]");
73             result = buf.toString();
74         }
75
76         return result;
77
78     }
79
80     /**
81      * Creates a copy of the given array, but with the given <code>Class</code> as
82      * element type. Useful for arrays of objects that implement multiple interfaces
83      * and a "typed view" onto these objects is required.
84      *
85      * @param objects the array of objects
86      * @param clazz the desired component type of the new array
87      * @return <code>null</code> when objects is <code>null</code>, or a new
88      * array containing the elements of the source array which is typed to
89      * the given <code>clazz</code> parameter. If <code>clazz</code> is
90      * already the component type of the source array, the source array is
91      * returned (i.e. no copy is created).
92      * @throws IllegalArgumentException if the <code>clazz</code> argument is
93      * <code>null</code>.
94      * @throws ArrayStoreException if the elements in <code>objects</code> cannot
95      * be cast to <code>clazz</code>.
96      */

97     public static Object JavaDoc[] toArrayOfComponentType(Object JavaDoc[] objects, Class JavaDoc clazz)
98     {
99         if (objects == null || objects.getClass().getComponentType().equals(clazz))
100         {
101             return objects;
102         }
103
104         if (clazz == null)
105         {
106             throw new IllegalArgumentException JavaDoc("Array target class must not be null");
107         }
108
109         Object JavaDoc[] result = (Object JavaDoc[])Array.newInstance(clazz, objects.length);
110         System.arraycopy(objects, 0, result, 0, objects.length);
111         return result;
112     }
113
114 }
115
Popular Tags