Coverage Report - org.mule.util.ArrayUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayUtils
79%
26/33
79%
22/28
8.5
 
 1  
 /*
 2  
  * $Id: ArrayUtils.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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  
 
 15  
 // @ThreadSafe
 16  0
 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 toString(Object array, int maxElements)
 24  
     {
 25  
         String result;
 26  
 
 27  14
         Class componentType = array.getClass().getComponentType();
 28  16
         if (Object.class.isAssignableFrom(componentType))
 29  
         {
 30  4
             result = ArrayUtils.toString((ArrayUtils.subarray((Object[]) array, 0, maxElements)));
 31  
         }
 32  10
         else if (componentType.equals(Boolean.TYPE))
 33  
         {
 34  0
             result = ArrayUtils.toString((ArrayUtils.subarray((boolean[]) array, 0, maxElements)));
 35  
         }
 36  10
         else if (componentType.equals(Byte.TYPE))
 37  
         {
 38  6
             result = ArrayUtils.toString((ArrayUtils.subarray((byte[]) array, 0, maxElements)));
 39  
         }
 40  4
         else if (componentType.equals(Character.TYPE))
 41  
         {
 42  0
             result = ArrayUtils.toString((ArrayUtils.subarray((char[]) array, 0, maxElements)));
 43  
         }
 44  4
         else if (componentType.equals(Short.TYPE))
 45  
         {
 46  0
             result = ArrayUtils.toString((ArrayUtils.subarray((short[]) array, 0, maxElements)));
 47  
         }
 48  4
         else if (componentType.equals(Integer.TYPE))
 49  
         {
 50  0
             result = ArrayUtils.toString((ArrayUtils.subarray((int[]) array, 0, maxElements)));
 51  
         }
 52  4
         else if (componentType.equals(Long.TYPE))
 53  
         {
 54  2
             result = ArrayUtils.toString((ArrayUtils.subarray((long[]) array, 0, maxElements)));
 55  
         }
 56  2
         else if (componentType.equals(Float.TYPE))
 57  
         {
 58  0
             result = ArrayUtils.toString((ArrayUtils.subarray((float[]) array, 0, maxElements)));
 59  
         }
 60  2
         else if (componentType.equals(Double.TYPE))
 61  
         {
 62  2
             result = ArrayUtils.toString((ArrayUtils.subarray((double[]) array, 0, maxElements)));
 63  
         }
 64  
         else
 65  
         {
 66  0
             throw new IllegalArgumentException("Unknown array component type: " + componentType.getName());
 67  
         }
 68  
 
 69  14
         if (Array.getLength(array) > maxElements)
 70  
         {
 71  4
             StringBuffer buf = new StringBuffer(result);
 72  4
             buf.insert(buf.length() - 1, " [..]");
 73  4
             result = buf.toString();
 74  
         }
 75  
 
 76  14
         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[] toArrayOfComponentType(Object[] objects, Class clazz)
 98  
     {
 99  84
         if (objects == null || objects.getClass().getComponentType().equals(clazz))
 100  
         {
 101  4
             return objects;
 102  
         }
 103  
 
 104  80
         if (clazz == null)
 105  
         {
 106  2
             throw new IllegalArgumentException("Array target class must not be null");
 107  
         }
 108  
 
 109  78
         Object[] result = (Object[]) Array.newInstance(clazz, objects.length);
 110  78
         System.arraycopy(objects, 0, result, 0, objects.length);
 111  76
         return result;
 112  
     }
 113  
 
 114  
 }