Coverage Report - org.kuali.rice.kns.web.struts.pojo.ArrayUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayUtils
0%
0/59
0%
0/54
16
 
 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.kns.web.struts.pojo;
 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  0
 public class ArrayUtils {
 28  
 
 29  
     /**
 30  
      * Sets the value of an element of an array of primitives at the supplied index.
 31  
      * 
 32  
      * @param array An array.
 33  
      * @param type The component type of the array.
 34  
      * @param index An array index.
 35  
      */
 36  
     public static void setArrayValue(Object array, Class type, Object value, int index) {
 37  0
         if (!type.isPrimitive())
 38  0
             Array.set(array, index, value);
 39  0
         else if (type.isAssignableFrom(Boolean.TYPE))
 40  0
             Array.setBoolean(array, index, ((Boolean) value).booleanValue());
 41  0
         else if (type.isAssignableFrom(Character.TYPE))
 42  0
             Array.setChar(array, index, ((Character) value).charValue());
 43  0
         else if (type.isAssignableFrom(Character.TYPE))
 44  0
             Array.setByte(array, index, ((Byte) value).byteValue());
 45  0
         else if (type.isAssignableFrom(Integer.TYPE))
 46  0
             Array.setInt(array, index, ((Integer) value).intValue());
 47  0
         else if (type.isAssignableFrom(Character.TYPE))
 48  0
             Array.setShort(array, index, ((Short) value).shortValue());
 49  0
         else if (type.isAssignableFrom(Character.TYPE))
 50  0
             Array.setLong(array, index, ((Long) value).longValue());
 51  0
         else if (type.isAssignableFrom(Float.TYPE))
 52  0
             Array.setFloat(array, index, ((Float) value).floatValue());
 53  0
         else if (type.isAssignableFrom(Character.TYPE))
 54  0
             Array.setDouble(array, index, ((Double) value).doubleValue());
 55  0
     }
 56  
 
 57  
     public static Object toObject(Object value) {
 58  0
         if (!value.getClass().isArray())
 59  0
             return value;
 60  
 
 61  0
         Class type = value.getClass().getComponentType();
 62  0
         if (Array.getLength(value) == 0)
 63  0
             return null;
 64  0
         if (!type.isPrimitive())
 65  0
             return Array.get(value, 0);
 66  0
         if (char.class.isAssignableFrom(type))
 67  0
             return new Character(Array.getChar(value, 0));
 68  0
         if (byte.class.isAssignableFrom(type))
 69  0
             return new Byte(Array.getByte(value, 0));
 70  0
         if (int.class.isAssignableFrom(type))
 71  0
             return new Integer(Array.getInt(value, 0));
 72  0
         if (long.class.isAssignableFrom(type))
 73  0
             return new Long(Array.getLong(value, 0));
 74  0
         if (short.class.isAssignableFrom(type))
 75  0
             return new Short(Array.getShort(value, 0));
 76  0
         if (double.class.isAssignableFrom(type))
 77  0
             return new Double(Array.getDouble(value, 0));
 78  0
         if (float.class.isAssignableFrom(type))
 79  0
             return new Float(Array.getFloat(value, 0));
 80  
 
 81  0
         return null;
 82  
     }
 83  
 
 84  
     public static Object toString(Object array, Class type) {
 85  0
         if (boolean.class.isAssignableFrom(type))
 86  0
             return Boolean.toString(((boolean[]) array)[0]);
 87  0
         if (char.class.isAssignableFrom(type))
 88  0
             return Character.toString(((char[]) array)[0]);
 89  0
         if (byte.class.isAssignableFrom(type))
 90  0
             return Byte.toString(((byte[]) array)[0]);
 91  0
         if (int.class.isAssignableFrom(type))
 92  0
             return Integer.toString(((int[]) array)[0]);
 93  0
         if (long.class.isAssignableFrom(type))
 94  0
             return Long.toString(((long[]) array)[0]);
 95  0
         if (short.class.isAssignableFrom(type))
 96  0
             return Short.toString(((short[]) array)[0]);
 97  0
         if (double.class.isAssignableFrom(type))
 98  0
             return Double.toString(((double[]) array)[0]);
 99  0
         if (float.class.isAssignableFrom(type))
 100  0
             return Float.toString(((float[]) array)[0]);
 101  
 
 102  0
         return ((String[]) array)[0];
 103  
     }
 104  
 }