Coverage Report - org.mule.util.ArrayUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayUtils
63%
26/41
77%
20/26
4.75
 
 1  
 /*
 2  
  * $Id: ArrayUtils.java 10529 2008-01-25 05:58:36Z dfeist $
 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 CPAL v1.0
 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;
 14  
 import java.util.Arrays;
 15  
 import java.util.Collection;
 16  
 import java.util.HashSet;
 17  
 
 18  
 // @ThreadSafe
 19  0
 public class ArrayUtils extends org.apache.commons.lang.ArrayUtils
 20  
 {
 21  
 
 22  
     /**
 23  
      * Like {@link #toString(Object)} but considers at most <code>maxElements</code>
 24  
      * values; overflow is indicated by an appended "[..]" ellipsis.
 25  
      */
 26  
     public static String toString(Object array, int maxElements)
 27  
     {
 28  
         String result;
 29  
 
 30  56
         Class componentType = array.getClass().getComponentType();
 31  56
         if (Object.class.isAssignableFrom(componentType))
 32  
         {
 33  46
             result = ArrayUtils.toString((ArrayUtils.subarray((Object[]) array, 0, maxElements)));
 34  
         }
 35  10
         else if (componentType.equals(Boolean.TYPE))
 36  
         {
 37  0
             result = ArrayUtils.toString((ArrayUtils.subarray((boolean[]) array, 0, maxElements)));
 38  
         }
 39  10
         else if (componentType.equals(Byte.TYPE))
 40  
         {
 41  6
             result = ArrayUtils.toString((ArrayUtils.subarray((byte[]) array, 0, maxElements)));
 42  
         }
 43  4
         else if (componentType.equals(Character.TYPE))
 44  
         {
 45  0
             result = ArrayUtils.toString((ArrayUtils.subarray((char[]) array, 0, maxElements)));
 46  
         }
 47  4
         else if (componentType.equals(Short.TYPE))
 48  
         {
 49  0
             result = ArrayUtils.toString((ArrayUtils.subarray((short[]) array, 0, maxElements)));
 50  
         }
 51  4
         else if (componentType.equals(Integer.TYPE))
 52  
         {
 53  0
             result = ArrayUtils.toString((ArrayUtils.subarray((int[]) array, 0, maxElements)));
 54  
         }
 55  4
         else if (componentType.equals(Long.TYPE))
 56  
         {
 57  2
             result = ArrayUtils.toString((ArrayUtils.subarray((long[]) array, 0, maxElements)));
 58  
         }
 59  2
         else if (componentType.equals(Float.TYPE))
 60  
         {
 61  0
             result = ArrayUtils.toString((ArrayUtils.subarray((float[]) array, 0, maxElements)));
 62  
         }
 63  2
         else if (componentType.equals(Double.TYPE))
 64  
         {
 65  2
             result = ArrayUtils.toString((ArrayUtils.subarray((double[]) array, 0, maxElements)));
 66  
         }
 67  
         else
 68  
         {
 69  0
             throw new IllegalArgumentException("Unknown array service type: " + componentType.getName());
 70  
         }
 71  
 
 72  56
         if (Array.getLength(array) > maxElements)
 73  
         {
 74  4
             StringBuffer buf = new StringBuffer(result);
 75  4
             buf.insert(buf.length() - 1, " [..]");
 76  4
             result = buf.toString();
 77  
         }
 78  
 
 79  56
         return result;
 80  
 
 81  
     }
 82  
 
 83  
     /**
 84  
      * Creates a copy of the given array, but with the given <code>Class</code> as
 85  
      * element type. Useful for arrays of objects that implement multiple interfaces
 86  
      * and a "typed view" onto these objects is required.
 87  
      * 
 88  
      * @param objects the array of objects
 89  
      * @param clazz the desired service type of the new array
 90  
      * @return <code>null</code> when objects is <code>null</code>, or a new
 91  
      *         array containing the elements of the source array which is typed to
 92  
      *         the given <code>clazz</code> parameter. If <code>clazz</code> is
 93  
      *         already the service type of the source array, the source array is
 94  
      *         returned (i.e. no copy is created).
 95  
      * @throws IllegalArgumentException if the <code>clazz</code> argument is
 96  
      *             <code>null</code>.
 97  
      * @throws ArrayStoreException if the elements in <code>objects</code> cannot
 98  
      *             be cast to <code>clazz</code>.
 99  
      */
 100  
     public static Object[] toArrayOfComponentType(Object[] objects, Class clazz)
 101  
     {
 102  470
         if (objects == null || objects.getClass().getComponentType().equals(clazz))
 103  
         {
 104  4
             return objects;
 105  
         }
 106  
 
 107  466
         if (clazz == null)
 108  
         {
 109  2
             throw new IllegalArgumentException("Array target class must not be null");
 110  
         }
 111  
 
 112  464
         Object[] result = (Object[]) Array.newInstance(clazz, objects.length);
 113  464
         System.arraycopy(objects, 0, result, 0, objects.length);
 114  462
         return result;
 115  
     }
 116  
 
 117  
     public static Object[] setDifference(Object[] a, Object[] b)
 118  
     {
 119  0
         Collection aCollecn = new HashSet(Arrays.asList(a));
 120  0
         Collection bCollecn = Arrays.asList(b);
 121  0
         aCollecn.removeAll(bCollecn);
 122  0
         return aCollecn.toArray();
 123  
     }
 124  
 
 125  
     public static String[] setDifference(String[] a, String[] b)
 126  
     {
 127  0
         Object[] ugly = setDifference((Object[])a, b);
 128  0
         String[] copy = new String[ugly.length];
 129  0
         System.arraycopy(ugly, 0, copy, 0, ugly.length);
 130  0
         return copy;
 131  
     }
 132  
     
 133  
 }