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.core.web.struts.pojo;
017:
018: // end Kuali Foundation modification
019:
020: import java.lang.reflect.Array;
021:
022: /**
023: * begin Kuali Foundation modification
024: * This class sets the value of an element of an array of primitives at the supplied index.
025: * end Kuali Foundation modification
026: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
027: */
028: public class ArrayUtils {
029:
030: /**
031: * Sets the value of an element of an array of primitives at the supplied index.
032: *
033: * @param array An array.
034: * @param type The component type of the array.
035: * @param index An array index.
036: */
037: public static void setArrayValue(Object array, Class type,
038: Object value, int index) {
039: if (!type.isPrimitive())
040: Array.set(array, index, value);
041: else if (type.isAssignableFrom(Boolean.TYPE))
042: Array.setBoolean(array, index, ((Boolean) value)
043: .booleanValue());
044: else if (type.isAssignableFrom(Character.TYPE))
045: Array
046: .setChar(array, index, ((Character) value)
047: .charValue());
048: else if (type.isAssignableFrom(Character.TYPE))
049: Array.setByte(array, index, ((Byte) value).byteValue());
050: else if (type.isAssignableFrom(Integer.TYPE))
051: Array.setInt(array, index, ((Integer) value).intValue());
052: else if (type.isAssignableFrom(Character.TYPE))
053: Array.setShort(array, index, ((Short) value).shortValue());
054: else if (type.isAssignableFrom(Character.TYPE))
055: Array.setLong(array, index, ((Long) value).longValue());
056: else if (type.isAssignableFrom(Float.TYPE))
057: Array.setFloat(array, index, ((Float) value).floatValue());
058: else if (type.isAssignableFrom(Character.TYPE))
059: Array.setDouble(array, index, ((Double) value)
060: .doubleValue());
061: }
062:
063: public static Object toObject(Object value) {
064: if (!value.getClass().isArray())
065: return value;
066:
067: Class type = value.getClass().getComponentType();
068: if (Array.getLength(value) == 0)
069: return null;
070: if (!type.isPrimitive())
071: return Array.get(value, 0);
072: if (char.class.isAssignableFrom(type))
073: return new Character(Array.getChar(value, 0));
074: if (byte.class.isAssignableFrom(type))
075: return new Byte(Array.getByte(value, 0));
076: if (int.class.isAssignableFrom(type))
077: return new Integer(Array.getInt(value, 0));
078: if (long.class.isAssignableFrom(type))
079: return new Long(Array.getLong(value, 0));
080: if (short.class.isAssignableFrom(type))
081: return new Short(Array.getShort(value, 0));
082: if (double.class.isAssignableFrom(type))
083: return new Double(Array.getDouble(value, 0));
084: if (float.class.isAssignableFrom(type))
085: return new Float(Array.getFloat(value, 0));
086:
087: return null;
088: }
089:
090: public static Object toString(Object array, Class type) {
091: if (boolean.class.isAssignableFrom(type))
092: return Boolean.toString(((boolean[]) array)[0]);
093: if (char.class.isAssignableFrom(type))
094: return Character.toString(((char[]) array)[0]);
095: if (byte.class.isAssignableFrom(type))
096: return Byte.toString(((byte[]) array)[0]);
097: if (int.class.isAssignableFrom(type))
098: return Integer.toString(((int[]) array)[0]);
099: if (long.class.isAssignableFrom(type))
100: return Long.toString(((long[]) array)[0]);
101: if (short.class.isAssignableFrom(type))
102: return Short.toString(((short[]) array)[0]);
103: if (double.class.isAssignableFrom(type))
104: return Double.toString(((double[]) array)[0]);
105: if (float.class.isAssignableFrom(type))
106: return Float.toString(((float[]) array)[0]);
107:
108: return ((String[]) array)[0];
109: }
110: }
|