Coverage Report - org.kuali.rice.core.util.ArrayUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayUtils
0%
0/62
0%
0/56
13
 
 1  
 /*
 2  
  * Copyright 2004 Jonathan M. Lehr
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  * http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 10  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 11  
  * governing permissions and limitations under the License.
 12  
  * 
 13  
  */
 14  
 
 15  
 // begin Kuali Foundation modification
 16  
 package org.kuali.rice.core.util;
 17  
 // end Kuali Foundation modification
 18  
 
 19  
 import java.lang.reflect.Array;
 20  
 
 21  
 /**
 22  
  * begin Kuali Foundation modification
 23  
  * This class sets the value of an element of an array of primitives at the supplied index.
 24  
  * end Kuali Foundation modification
 25  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 26  
  */
 27  
 public final class ArrayUtils {
 28  
         
 29  0
         private ArrayUtils() {
 30  0
                 throw new UnsupportedOperationException("do not call");
 31  
         }
 32  
 
 33  
     /**
 34  
      * Sets the value of an element of an array of primitives at the supplied index.
 35  
      * 
 36  
      * @param array An array.
 37  
      * @param type The component type of the array.
 38  
      * @param index An array index.
 39  
      */
 40  
     public static void setArrayValue(Object array, Class type, Object value, int index) {
 41  0
         if (!type.isPrimitive())
 42  0
             Array.set(array, index, value);
 43  0
         else if (type.isAssignableFrom(Boolean.TYPE))
 44  0
             Array.setBoolean(array, index, (Boolean) value);
 45  0
         else if (type.isAssignableFrom(Character.TYPE))
 46  0
             Array.setChar(array, index, (Character) value);
 47  0
         else if (type.isAssignableFrom(Byte.TYPE))
 48  0
             Array.setByte(array, index, (Byte) value);
 49  0
         else if (type.isAssignableFrom(Integer.TYPE))
 50  0
             Array.setInt(array, index, (Integer) value);
 51  0
         else if (type.isAssignableFrom(Short.TYPE))
 52  0
             Array.setShort(array, index, (Short) value);
 53  0
         else if (type.isAssignableFrom(Long.TYPE))
 54  0
             Array.setLong(array, index, (Long) value);
 55  0
         else if (type.isAssignableFrom(Float.TYPE))
 56  0
             Array.setFloat(array, index, (Float) value);
 57  0
         else if (type.isAssignableFrom(Double.TYPE))
 58  0
             Array.setDouble(array, index, (Double) value);
 59  0
     }
 60  
 
 61  
     public static Object toObject(Object value) {
 62  0
         if (!value.getClass().isArray())
 63  0
             return value;
 64  
 
 65  0
         Class type = value.getClass().getComponentType();
 66  0
         if (Array.getLength(value) == 0)
 67  0
             return null;
 68  0
         if (!type.isPrimitive())
 69  0
             return Array.get(value, 0);
 70  0
         if (boolean.class.isAssignableFrom(type))
 71  0
             return Array.getBoolean(value, 0);
 72  0
         if (char.class.isAssignableFrom(type))
 73  0
             return new Character(Array.getChar(value, 0));
 74  0
         if (byte.class.isAssignableFrom(type))
 75  0
             return new Byte(Array.getByte(value, 0));
 76  0
         if (int.class.isAssignableFrom(type))
 77  0
             return new Integer(Array.getInt(value, 0));
 78  0
         if (long.class.isAssignableFrom(type))
 79  0
             return new Long(Array.getLong(value, 0));
 80  0
         if (short.class.isAssignableFrom(type))
 81  0
             return new Short(Array.getShort(value, 0));
 82  0
         if (double.class.isAssignableFrom(type))
 83  0
             return new Double(Array.getDouble(value, 0));
 84  0
         if (float.class.isAssignableFrom(type))
 85  0
             return new Float(Array.getFloat(value, 0));
 86  
 
 87  0
         return null;
 88  
     }
 89  
 
 90  
     public static Object toString(Object array, Class type) {
 91  0
         if (boolean.class.isAssignableFrom(type))
 92  0
             return Boolean.toString(((boolean[]) array)[0]);
 93  0
         if (char.class.isAssignableFrom(type))
 94  0
             return Character.toString(((char[]) array)[0]);
 95  0
         if (byte.class.isAssignableFrom(type))
 96  0
             return Byte.toString(((byte[]) array)[0]);
 97  0
         if (int.class.isAssignableFrom(type))
 98  0
             return Integer.toString(((int[]) array)[0]);
 99  0
         if (long.class.isAssignableFrom(type))
 100  0
             return Long.toString(((long[]) array)[0]);
 101  0
         if (short.class.isAssignableFrom(type))
 102  0
             return Short.toString(((short[]) array)[0]);
 103  0
         if (double.class.isAssignableFrom(type))
 104  0
             return Double.toString(((double[]) array)[0]);
 105  0
         if (float.class.isAssignableFrom(type))
 106  0
             return Float.toString(((float[]) array)[0]);
 107  
 
 108  0
         return ((String[]) array)[0];
 109  
     }
 110  
 }