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