001 /*
002 * Copyright 2004 Jonathan M. Lehr
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
010 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
011 * governing permissions and limitations under the License.
012 *
013 */
014
015 // begin Kuali Foundation modification
016 package org.kuali.rice.kns.web.struts.pojo;
017 // end Kuali Foundation modification
018
019 import java.lang.reflect.Array;
020
021 /**
022 * begin Kuali Foundation modification
023 * This class sets the value of an element of an array of primitives at the supplied index.
024 * end Kuali Foundation modification
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 */
027 public class ArrayUtils {
028
029 /**
030 * Sets the value of an element of an array of primitives at the supplied index.
031 *
032 * @param array An array.
033 * @param type The component type of the array.
034 * @param index An array index.
035 */
036 public static void setArrayValue(Object array, Class type, Object value, int index) {
037 if (!type.isPrimitive())
038 Array.set(array, index, value);
039 else if (type.isAssignableFrom(Boolean.TYPE))
040 Array.setBoolean(array, index, (Boolean) value);
041 else if (type.isAssignableFrom(Character.TYPE))
042 Array.setChar(array, index, (Character) value);
043 else if (type.isAssignableFrom(Byte.TYPE))
044 Array.setByte(array, index, (Byte) value);
045 else if (type.isAssignableFrom(Integer.TYPE))
046 Array.setInt(array, index, (Integer) value);
047 else if (type.isAssignableFrom(Short.TYPE))
048 Array.setShort(array, index, (Short) value);
049 else if (type.isAssignableFrom(Long.TYPE))
050 Array.setLong(array, index, (Long) value);
051 else if (type.isAssignableFrom(Float.TYPE))
052 Array.setFloat(array, index, (Float) value);
053 else if (type.isAssignableFrom(Double.TYPE))
054 Array.setDouble(array, index, (Double) value);
055 }
056
057 public static Object toObject(Object value) {
058 if (!value.getClass().isArray())
059 return value;
060
061 Class type = value.getClass().getComponentType();
062 if (Array.getLength(value) == 0)
063 return null;
064 if (!type.isPrimitive())
065 return Array.get(value, 0);
066 if (boolean.class.isAssignableFrom(type))
067 return Array.getBoolean(value, 0);
068 if (char.class.isAssignableFrom(type))
069 return Array.getChar(value, 0);
070 if (byte.class.isAssignableFrom(type))
071 return Array.getByte(value, 0);
072 if (int.class.isAssignableFrom(type))
073 return Array.getInt(value, 0);
074 if (long.class.isAssignableFrom(type))
075 return Array.getLong(value, 0);
076 if (short.class.isAssignableFrom(type))
077 return Array.getShort(value, 0);
078 if (double.class.isAssignableFrom(type))
079 return Array.getDouble(value, 0);
080 if (float.class.isAssignableFrom(type))
081 return Array.getFloat(value, 0);
082
083 return null;
084 }
085
086 public static Object toString(Object array, Class type) {
087 if (boolean.class.isAssignableFrom(type))
088 return Boolean.toString(((boolean[]) array)[0]);
089 if (char.class.isAssignableFrom(type))
090 return Character.toString(((char[]) array)[0]);
091 if (byte.class.isAssignableFrom(type))
092 return Byte.toString(((byte[]) array)[0]);
093 if (int.class.isAssignableFrom(type))
094 return Integer.toString(((int[]) array)[0]);
095 if (long.class.isAssignableFrom(type))
096 return Long.toString(((long[]) array)[0]);
097 if (short.class.isAssignableFrom(type))
098 return Short.toString(((short[]) array)[0]);
099 if (double.class.isAssignableFrom(type))
100 return Double.toString(((double[]) array)[0]);
101 if (float.class.isAssignableFrom(type))
102 return Float.toString(((float[]) array)[0]);
103
104 return ((String[]) array)[0];
105 }
106 }