001: /*
002: * $Id: ArrayUtils.java 10529 2008-01-25 05:58:36Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.util;
012:
013: import java.lang.reflect.Array;
014: import java.util.Arrays;
015: import java.util.Collection;
016: import java.util.HashSet;
017:
018: // @ThreadSafe
019: public class ArrayUtils extends org.apache.commons.lang.ArrayUtils {
020:
021: /**
022: * Like {@link #toString(Object)} but considers at most <code>maxElements</code>
023: * values; overflow is indicated by an appended "[..]" ellipsis.
024: */
025: public static String toString(Object array, int maxElements) {
026: String result;
027:
028: Class componentType = array.getClass().getComponentType();
029: if (Object.class.isAssignableFrom(componentType)) {
030: result = ArrayUtils.toString((ArrayUtils.subarray(
031: (Object[]) array, 0, maxElements)));
032: } else if (componentType.equals(Boolean.TYPE)) {
033: result = ArrayUtils.toString((ArrayUtils.subarray(
034: (boolean[]) array, 0, maxElements)));
035: } else if (componentType.equals(Byte.TYPE)) {
036: result = ArrayUtils.toString((ArrayUtils.subarray(
037: (byte[]) array, 0, maxElements)));
038: } else if (componentType.equals(Character.TYPE)) {
039: result = ArrayUtils.toString((ArrayUtils.subarray(
040: (char[]) array, 0, maxElements)));
041: } else if (componentType.equals(Short.TYPE)) {
042: result = ArrayUtils.toString((ArrayUtils.subarray(
043: (short[]) array, 0, maxElements)));
044: } else if (componentType.equals(Integer.TYPE)) {
045: result = ArrayUtils.toString((ArrayUtils.subarray(
046: (int[]) array, 0, maxElements)));
047: } else if (componentType.equals(Long.TYPE)) {
048: result = ArrayUtils.toString((ArrayUtils.subarray(
049: (long[]) array, 0, maxElements)));
050: } else if (componentType.equals(Float.TYPE)) {
051: result = ArrayUtils.toString((ArrayUtils.subarray(
052: (float[]) array, 0, maxElements)));
053: } else if (componentType.equals(Double.TYPE)) {
054: result = ArrayUtils.toString((ArrayUtils.subarray(
055: (double[]) array, 0, maxElements)));
056: } else {
057: throw new IllegalArgumentException(
058: "Unknown array service type: "
059: + componentType.getName());
060: }
061:
062: if (Array.getLength(array) > maxElements) {
063: StringBuffer buf = new StringBuffer(result);
064: buf.insert(buf.length() - 1, " [..]");
065: result = buf.toString();
066: }
067:
068: return result;
069:
070: }
071:
072: /**
073: * Creates a copy of the given array, but with the given <code>Class</code> as
074: * element type. Useful for arrays of objects that implement multiple interfaces
075: * and a "typed view" onto these objects is required.
076: *
077: * @param objects the array of objects
078: * @param clazz the desired service type of the new array
079: * @return <code>null</code> when objects is <code>null</code>, or a new
080: * array containing the elements of the source array which is typed to
081: * the given <code>clazz</code> parameter. If <code>clazz</code> is
082: * already the service type of the source array, the source array is
083: * returned (i.e. no copy is created).
084: * @throws IllegalArgumentException if the <code>clazz</code> argument is
085: * <code>null</code>.
086: * @throws ArrayStoreException if the elements in <code>objects</code> cannot
087: * be cast to <code>clazz</code>.
088: */
089: public static Object[] toArrayOfComponentType(Object[] objects,
090: Class clazz) {
091: if (objects == null
092: || objects.getClass().getComponentType().equals(clazz)) {
093: return objects;
094: }
095:
096: if (clazz == null) {
097: throw new IllegalArgumentException(
098: "Array target class must not be null");
099: }
100:
101: Object[] result = (Object[]) Array.newInstance(clazz,
102: objects.length);
103: System.arraycopy(objects, 0, result, 0, objects.length);
104: return result;
105: }
106:
107: public static Object[] setDifference(Object[] a, Object[] b) {
108: Collection aCollecn = new HashSet(Arrays.asList(a));
109: Collection bCollecn = Arrays.asList(b);
110: aCollecn.removeAll(bCollecn);
111: return aCollecn.toArray();
112: }
113:
114: public static String[] setDifference(String[] a, String[] b) {
115: Object[] ugly = setDifference((Object[]) a, b);
116: String[] copy = new String[ugly.length];
117: System.arraycopy(ugly, 0, copy, 0, ugly.length);
118: return copy;
119: }
120:
121: }
|