]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ValueUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / ValueUtils.java
index 0c0dc0dc6b6dcd32c99fb152bca3de68f61fabf6..fd41134d16339e0f0cf1cd65264bf1e639993e22 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.utils.datastructures;\r
-\r
-import java.io.DataInput;\r
-import java.io.DataOutput;\r
-import java.io.IOException;\r
-import java.lang.reflect.Array;\r
-import java.security.InvalidParameterException;\r
-import java.util.Arrays;\r
-import java.util.Calendar;\r
-\r
-/**\r
- * Object type casting tools.\r
- * \r
- * @author Toni Kalajainen\r
- * @author Hannu Niemistö\r
- */\r
-public class ValueUtils {\r
-\r
-    /**\r
-     * Convert double value to a number class.\r
-     * \r
-     * @param value\r
-     * @param numberClass\r
-     * @return\r
-     * @throws ClassCastException\r
-     */\r
-    public static Number doubleToNumberClass(double value, Class<? extends Number> numberClass)\r
-    throws ClassCastException\r
-    {\r
-        if (numberClass==Integer.class)\r
-            return (int)value;\r
-        if (numberClass==Byte.class)\r
-            return (byte)value;\r
-        if (numberClass==Float.class)\r
-            return (float)value;\r
-        if (numberClass==Short.class)\r
-            return (short)value;\r
-        if (numberClass==Long.class)\r
-            return (long)value;\r
-        if (numberClass==Double.class)\r
-            return (double)value;\r
-        throw new ClassCastException("Cannot convert to "+numberClass.getName());\r
-    }\r
-\r
-    /**\r
-     * Possible object types:\r
-     * \r
-     */\r
-\r
-    static final Boolean True = new Boolean(true);\r
-    static final Boolean False = new Boolean(false);\r
-\r
-    public static void serialize(DataOutput stream, Object obj) throws IOException {\r
-        try {\r
-            Class<?> clazz = obj.getClass();\r
-            if(clazz.isArray()) {\r
-                if(obj instanceof double[]) {\r
-                    double[] array = (double[])obj;\r
-                    stream.writeByte(16);\r
-                    stream.writeInt(array.length);\r
-                    for(double v : array)\r
-                        stream.writeDouble(v);\r
-                }\r
-                else if(obj instanceof float[]) {\r
-                    float[] array = (float[])obj;\r
-                    stream.writeByte(15);\r
-                    stream.writeInt(array.length);\r
-                    for(float v : array)\r
-                        stream.writeFloat(v);\r
-                }\r
-                else if(obj instanceof int[]) {\r
-                    int[] array = (int[])obj;\r
-                    stream.writeByte(13);\r
-                    stream.writeInt(array.length);\r
-                    for(int v : array)\r
-                        stream.writeInt(v);\r
-                }\r
-                else if(obj instanceof String[]) {\r
-                    String[] array = (String[])obj;\r
-                    stream.writeByte(17);\r
-                    stream.writeInt(array.length);\r
-                    for(String v : array)\r
-                        stream.writeUTF(v);\r
-                }\r
-                else if(obj instanceof boolean[]) {\r
-                    boolean[] array = (boolean[])obj;\r
-                    stream.writeByte(11);\r
-                    stream.writeInt(array.length);\r
-                    for(boolean v : array)\r
-                        stream.writeByte(v ? (byte)1 : (byte)0);\r
-                }\r
-                else if(obj instanceof byte[]) {\r
-                    byte[] array = (byte[])obj;\r
-                    stream.writeByte(12);\r
-                    stream.writeInt(array.length);\r
-                    for(byte v : array)\r
-                        stream.writeByte(v);\r
-                }\r
-                else if(obj instanceof long[]) {\r
-                    long[] array = (long[])obj;\r
-                    stream.writeByte(14);\r
-                    stream.writeInt(array.length);\r
-                    for(long v : array)\r
-                        stream.writeLong(v);\r
-                }\r
-                else {\r
-                    System.out.println("ValueUtils.serialize failed (invalid type).");\r
-                    throw new InvalidParameterException();\r
-                }\r
-            }\r
-            else {\r
-                if(clazz == Double.class) {\r
-                    stream.writeByte(6);\r
-                    stream.writeDouble((Double)obj);\r
-                }\r
-                else if(clazz == Float.class) {\r
-                    stream.writeByte(5);\r
-                    stream.writeDouble((Float)obj);\r
-                }\r
-                else if(clazz == Integer.class) {\r
-                    stream.writeByte(3);\r
-                    stream.writeInt((Integer)obj);\r
-                }\r
-                else if(clazz == String.class) {\r
-                    stream.writeByte(7);\r
-                    stream.writeUTF((String)obj);\r
-                }\r
-                else if(clazz == Boolean.class) {\r
-                    stream.writeByte((Boolean)obj ? 1 : 0);\r
-                }\r
-                else if(clazz == Byte.class) {\r
-                    stream.writeByte(2);\r
-                    stream.writeByte((Byte)obj);\r
-                }\r
-                else if(clazz == Long.class) {\r
-                    stream.writeByte(4);\r
-                    stream.writeLong((Long)obj);\r
-                }\r
-                else {\r
-                    System.out.println("ValueUtils.serialize failed (invalid type).");\r
-                    throw new InvalidParameterException();\r
-                }\r
-            }\r
-        } catch(IOException e) {\r
-            System.out.println("ValueUtils.serialize failed (write failure).");\r
-            e.printStackTrace();\r
-            throw e;\r
-        }\r
-    }\r
-\r
-    public static Object deserialize(DataInput stream) throws IOException {\r
-        try {\r
-            byte typeCode = stream.readByte();\r
-            switch(typeCode) {\r
-                case 0: return False;\r
-                case 1: return True;\r
-                case 2: return stream.readByte();\r
-                case 3: return stream.readInt();\r
-                case 4: return stream.readLong();\r
-                case 5: return stream.readFloat();\r
-                case 6: return stream.readDouble();\r
-                case 7: return stream.readUTF();\r
-\r
-                case 11: {\r
-                    int length = stream.readInt();\r
-                    boolean[] value = new boolean[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readByte() != 0;\r
-                    return value;\r
-                }\r
-                case 12: {\r
-                    int length = stream.readInt();\r
-                    byte[] value = new byte[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readByte();\r
-                    return value;\r
-                }\r
-                case 13: {\r
-                    int length = stream.readInt();\r
-                    int[] value = new int[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readInt();\r
-                    return value;\r
-                }\r
-                case 14: {\r
-                    int length = stream.readInt();\r
-                    long[] value = new long[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readLong();\r
-                    return value;\r
-                }\r
-                case 15: {\r
-                    int length = stream.readInt();\r
-                    float[] value = new float[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readFloat();\r
-                    return value;\r
-                }\r
-                case 16: {\r
-                    int length = stream.readInt();\r
-                    double[] value = new double[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readDouble();\r
-                    return value;\r
-                }\r
-                case 17: {\r
-                    int length = stream.readInt();\r
-                    String[] value = new String[length];\r
-                    for(int i=0;i<length;++i)\r
-                        value[i] = stream.readUTF();\r
-                    return value;\r
-                }\r
-\r
-            }\r
-\r
-            System.out.println("ValueUtils.deserialize failed (invalid type code).");\r
-            throw new IOException("Invalid value data.");\r
-        } catch(IOException e) {\r
-            System.out.println("ValueUtils.deserialize failed (reading failure).");\r
-            e.printStackTrace();\r
-            throw e;\r
-        }\r
-    }\r
-\r
-\r
-\r
-\r
-\r
-\r
-    /**\r
-     * is object an array type\r
-     * @param obj the object\r
-     * @return true if it is object array (Integer[], Short[], ect)\r
-     */\r
-    public static boolean isArray(Object obj) {\r
-        return obj.getClass().isArray();\r
-    }\r
-\r
-    /**\r
-     * Get the array length\r
-     * @param obj object\r
-     * @return the length\r
-     */\r
-    public static int getArrayLength(Object obj) {\r
-        return Array.getLength(obj);\r
-    }\r
-\r
-    /**\r
-     * Type casts an array to double.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static double[] toDoubleArray(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            double scalar = toDoubleScalar(obj);\r
-            return new double[] {scalar};\r
-        }\r
-        int len = getArrayLength(obj);\r
-        double[] result = new double[len];\r
-        // TODO add support for int[] double[] float[], ect..\r
-        if (obj instanceof Calendar[])\r
-        {\r
-            Calendar[] array = (Calendar[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].getTimeInMillis();\r
-            return result;\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].doubleValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].doubleValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            return (double[])obj;\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].doubleValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].doubleValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].doubleValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].doubleValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                try {\r
-                    result[i] = new Double(array[i]);\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-                return result;\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i].booleanValue()?1:0);\r
-            return result;\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i]?1:0);\r
-            return result;\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-    /**\r
-     * Type casts an object to long.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static double toDoubleScalar(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            if (obj instanceof Number)\r
-                return ((Number)obj).doubleValue();\r
-            if (obj instanceof Calendar)\r
-                return ((Calendar) obj).getTimeInMillis();\r
-            if (obj instanceof String)\r
-            {\r
-                String str = (String) obj;\r
-                try {\r
-                    Double d = new Double(str);\r
-                    return d.doubleValue();\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-            }\r
-            if (obj instanceof Boolean) {\r
-                return (((Boolean)obj).booleanValue())?1:0;\r
-            }\r
-            throw new TypeCastException(obj.getClass());\r
-        }\r
-        int len = getArrayLength(obj);\r
-        if (len!=1)\r
-            throw new TypeCastException("Expected length of array is 1");\r
-\r
-        if (obj instanceof double[])\r
-        {\r
-            return ((double[])obj)[0];\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Calendar[])\r
-        {\r
-            Calendar[] array = (Calendar[]) obj;\r
-            return array[0].getTimeInMillis();\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            return array[0].doubleValue();\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            return array[0].doubleValue();\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            return array[0].doubleValue();\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            return array[0].doubleValue();\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            return array[0].doubleValue();\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            return array[0].doubleValue();\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            try {\r
-                return new Double(array[0]);\r
-            } catch (NumberFormatException e) {\r
-                throw new TypeCastException(obj.getClass());\r
-            }\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            return (array[0].booleanValue()?1:0);\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            return (array[0]?1:0);\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-\r
-    /**\r
-     * Type casts an array to float.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static float[] toFloatArray(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            float scalar = toFloatScalar(obj);\r
-            return new float[] {scalar};\r
-        }\r
-        int len = getArrayLength(obj);\r
-        float[] result = new float[len];\r
-        // TODO add support for int[] float[] float[], ect..\r
-        if (obj instanceof Calendar[])\r
-        {\r
-            Calendar[] array = (Calendar[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].getTimeInMillis();\r
-            return result;\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].floatValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].floatValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            return (float[])obj;\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].floatValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            double[] array = (double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (float)array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].floatValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].floatValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].floatValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                try {\r
-                    result[i] = new Float(array[i]);\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-                return result;\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i].booleanValue()?1:0);\r
-            return result;\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i]?1:0);\r
-            return result;\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-    /**\r
-     * Type casts an object to float.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static float toFloatScalar(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            if (obj instanceof Number)\r
-                return ((Number)obj).floatValue();\r
-            if (obj instanceof Calendar)\r
-                return ((Calendar) obj).getTimeInMillis();\r
-            if (obj instanceof String)\r
-            {\r
-                String str = (String) obj;\r
-                try {\r
-                    Float d = new Float(str);\r
-                    return d.floatValue();\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-            }\r
-            if (obj instanceof Boolean) {\r
-                return (((Boolean)obj).booleanValue())?1:0;\r
-            }\r
-            throw new TypeCastException(obj.getClass());\r
-        }\r
-        int len = getArrayLength(obj);\r
-        if (len!=1)\r
-            throw new TypeCastException("Expected length of array is 1");\r
-\r
-        if (obj instanceof float[])\r
-        {\r
-            return ((float[])obj)[0];\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Calendar[])\r
-        {\r
-            Calendar[] array = (Calendar[]) obj;\r
-            return array[0].getTimeInMillis();\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            return array[0].floatValue();\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            return array[0].floatValue();\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            return array[0].floatValue();\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            double[] array = (double[]) obj;\r
-            return (float)array[0];\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            return array[0].floatValue();\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            return array[0].floatValue();\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            return array[0].floatValue();\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            try {\r
-                return new Float(array[0]);\r
-            } catch (NumberFormatException e) {\r
-                throw new TypeCastException(obj.getClass());\r
-            }\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            return (array[0].booleanValue()?1:0);\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            return (array[0]?1:0);\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-\r
-    /**\r
-     * Type casts an object to long.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static long toLongScalar(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            if (obj instanceof Number)\r
-                return ((Number)obj).longValue();\r
-            if (obj instanceof Calendar)\r
-                return ((Calendar) obj).getTimeInMillis();\r
-            if (obj instanceof String)\r
-            {\r
-                String str = (String) obj;\r
-                try {\r
-                    Long d = new Long(str);\r
-                    return d.longValue();\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-            }\r
-            if (obj instanceof Boolean) {\r
-                return (((Boolean)obj).booleanValue())?1:0;\r
-            }\r
-            throw new TypeCastException(obj.getClass());\r
-        }\r
-        int len = getArrayLength(obj);\r
-        if (len!=1)\r
-            throw new TypeCastException("Expected length of array is 1");\r
-\r
-        if (obj instanceof double[])\r
-        {\r
-            return ((long[])obj)[0];\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Calendar[])\r
-        {\r
-            Calendar[] array = (Calendar[]) obj;\r
-            return array[0].getTimeInMillis();\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            return array[0].longValue();\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            return array[0].longValue();\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            return array[0].longValue();\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            return (long)array[0];\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            return array[0].longValue();\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            return array[0].longValue();\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            return array[0].longValue();\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            try {\r
-                return new Long(array[0]);\r
-            } catch (NumberFormatException e) {\r
-                throw new TypeCastException(obj.getClass());\r
-            }\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            return (array[0].booleanValue()?1:0);\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            return (array[0]?1:0);\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-    /**\r
-     * Type casts an array to long.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static long[] toLongArray(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            long scalar = toLongScalar(obj);\r
-            return new long[] {scalar};\r
-        }\r
-        int len = getArrayLength(obj);\r
-        long[] result = new long[len];\r
-        // TODO add support for int[] long[] float[], ect..\r
-        if (obj instanceof Calendar[])\r
-        {\r
-            Calendar[] array = (Calendar[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].getTimeInMillis();\r
-            return result;\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].longValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].longValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            return (long[])obj;\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].longValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (long)array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].longValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            double[] array = (double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (long)array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].longValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].longValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                try {\r
-                    result[i] = new Long(array[i]);\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-                return result;\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i].booleanValue()?1:0);\r
-            return result;\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i]?1:0);\r
-            return result;\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-\r
-\r
-\r
-\r
-\r
-    /**\r
-     * Type casts an object to int.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static int toIntegerScalar(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            if (obj instanceof Number)\r
-                return ((Number)obj).intValue();\r
-            if (obj instanceof String)\r
-            {\r
-                String str = (String) obj;\r
-                try {\r
-                    Integer d = new Integer(str);\r
-                    return d.intValue();\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-            }\r
-            if (obj instanceof Boolean) {\r
-                return (((Boolean)obj).booleanValue())?1:0;\r
-            }\r
-            throw new TypeCastException(obj.getClass());\r
-        }\r
-        int len = getArrayLength(obj);\r
-        if (len!=1)\r
-            throw new TypeCastException("Expected length of array is 1");\r
-\r
-        if (obj instanceof double[])\r
-        {\r
-            return ((int[])obj)[0];\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            return array[0].intValue();\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            return array[0].intValue();\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            return array[0].intValue();\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            return (int)array[0];\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            return array[0].intValue();\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            return (int)array[0];\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            return array[0].intValue();\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            return array[0].intValue();\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            return array[0];\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            try {\r
-                return new Integer(array[0]);\r
-            } catch (NumberFormatException e) {\r
-                throw new TypeCastException(obj.getClass());\r
-            }\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            return (array[0].booleanValue()?1:0);\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            return (array[0]?1:0);\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-    /**\r
-     * Type casts an array to int.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static int[] toIntegerArray(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            int scalar = toIntegerScalar(obj);\r
-            return new int[] {scalar};\r
-        }\r
-        int len = getArrayLength(obj);\r
-        int[] result = new int[len];\r
-        // TODO add support for int[] int[] float[], ect..\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].intValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (int)array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].intValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            return (int[])obj;\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].intValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (int)array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].intValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            double[] array = (double[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (int)array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].intValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].intValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i];\r
-            return result;\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                try {\r
-                    result[i] = new Integer(array[i]);\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-                return result;\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i].booleanValue()?1:0);\r
-            return result;\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            boolean[] array = (boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = (array[i]?1:0);\r
-            return result;\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-\r
-\r
-\r
-    /**\r
-     * Type casts an array to boolean.\r
-     *\r
-     * 0 is converted to false, 1 is converted to true, others throw TypeCastException\r
-     * \r
-     * @return type casted value\r
-     * @throws TypeCastException\r
-     */\r
-    public static boolean[] toBooleanArray(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            boolean scalar = toBooleanScalar(obj);\r
-            return new boolean[] {scalar};\r
-        }\r
-        int len = getArrayLength(obj);\r
-        boolean[] result = new boolean[len];\r
-        if (obj instanceof Integer[])\r
-        {\r
-            Integer[] array = (Integer[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                int value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int[] array = (int[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                int value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                result[i] = array[i].booleanValue();\r
-            return result;\r
-        }\r
-        if (obj instanceof boolean[])\r
-        {\r
-            return (boolean[])obj;\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            Float[] array = (Float[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                float value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float[] array = (float[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                float value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            Long[] array = (Long[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                long value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long[] array = (long[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                long value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short[] array = (Short[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                short value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short[] array = (short[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                short value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte[] array = (Byte[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                byte value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte[] array = (byte[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                byte value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            for (int i=0; i<len; i++)\r
-                try {\r
-                    result[i] = new Boolean(array[i]);\r
-                } catch (NumberFormatException e) {\r
-                    throw new TypeCastException(obj.getClass());\r
-                }\r
-                return result;\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            Double[] array = (Double[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                double value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            double[] array = (double[]) obj;\r
-            for (int i=0; i<len; i++) {\r
-                double value = array[i];\r
-                if (value==0) result[i] = false;\r
-                else if (value==1) result[i] = true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            return result;\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-    /**\r
-     * Type casts an object to long.\r
-     * \r
-     * String types are attempted to be converted. If string\r
-     * doesn't represent numeric value, 0 will be returned\r
-     * \r
-     * boolean types return 0/1, for false/true respectively\r
-     * \r
-     * @return type casted value\r
-     */\r
-    public static boolean toBooleanScalar(Object obj)\r
-    throws TypeCastException\r
-    {\r
-        if (!isArray(obj)) {\r
-            if (obj instanceof Boolean) {\r
-                return ((Boolean)obj).booleanValue();\r
-            }\r
-            if (obj instanceof Byte) {\r
-                byte value = (Byte)obj;\r
-                if (value==0) return false;\r
-                else if (value==1) return true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            if (obj instanceof Short) {\r
-                short value = (Short)obj;\r
-                if (value==0) return false;\r
-                else if (value==1) return true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            if (obj instanceof Integer) {\r
-                int value = (Integer)obj;\r
-                if (value==0) return false;\r
-                else if (value==1) return true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            if (obj instanceof Long) {\r
-                long value = (Long)obj;\r
-                if (value==0) return false;\r
-                else if (value==1) return true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            if (obj instanceof Float) {\r
-                float value = (Float)obj;\r
-                if (value==0) return false;\r
-                else if (value==1) return true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            if (obj instanceof Double) {\r
-                double value = (Double)obj;\r
-                if (value==0) return false;\r
-                else if (value==1) return true;\r
-                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            if (obj instanceof String) {\r
-                String value = (String)obj;\r
-                if (value.equals("true") || value.equals("1")) return true;\r
-                if (value.equals("false") || value.equals("0")) return false;\r
-                throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-            }\r
-            throw new TypeCastException(obj.getClass());\r
-        }\r
-        int len = getArrayLength(obj);\r
-        if (len!=1)\r
-            throw new TypeCastException("Expected length of array is 1");\r
-\r
-        if (obj instanceof boolean[])\r
-        {\r
-            return ((boolean[])obj)[0];\r
-        }\r
-        if (obj instanceof Boolean[])\r
-        {\r
-            Boolean[] array = (Boolean[]) obj;\r
-            return array[0].booleanValue();\r
-        }\r
-        if (obj instanceof int[])\r
-        {\r
-            int value = ((int[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof Integer[])\r
-        {\r
-            int value = ((Integer[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof Float[])\r
-        {\r
-            float value = ((Float[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof float[])\r
-        {\r
-            float value = ((float[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof Long[])\r
-        {\r
-            long value = ((Long[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof long[])\r
-        {\r
-            long value = ((long[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof Short[])\r
-        {\r
-            Short value = ((Short[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof short[])\r
-        {\r
-            short value = ((short[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof Byte[])\r
-        {\r
-            Byte value = ((Byte[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof byte[])\r
-        {\r
-            byte value = ((byte[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof String[])\r
-        {\r
-            String[] array = (String[]) obj;\r
-            String value = array[0];\r
-            if (value.equals("true") || value.equals("1")) return true;\r
-            if (value.equals("false") || value.equals("0")) return false;\r
-            throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof Double[])\r
-        {\r
-            double value = ((Double[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        if (obj instanceof double[])\r
-        {\r
-            double value = ((double[])obj)[0];\r
-            if (value==0) return false;\r
-            else if (value==1) return true;\r
-            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");\r
-        }\r
-        throw new TypeCastException(obj.getClass());\r
-    }\r
-\r
-\r
-    @SuppressWarnings("unchecked")\r
-    public static <T> T adaptToClass(Object obj, Class<T> clazz)\r
-    throws TypeCastException\r
-    {\r
-        if (clazz.equals(double[].class))\r
-            return (T) toDoubleArray(obj);\r
-        if (clazz.equals(Double.class))\r
-            return (T) new Double(toDoubleScalar(obj));\r
-\r
-        if (clazz.equals(float[].class))\r
-            return (T) toFloatArray(obj);\r
-        if (clazz.equals(Float.class))\r
-            return (T) new Float(toFloatScalar(obj));\r
-\r
-        if (clazz.equals(long[].class))\r
-            return (T) toLongArray(obj);\r
-        if (clazz.equals(Long.class))\r
-            return (T) new Long(toLongScalar(obj));\r
-\r
-        if (clazz.equals(int[].class))\r
-            return (T) toIntegerArray(obj);\r
-        if (clazz.equals(Integer.class))\r
-            return (T) new Integer(toIntegerScalar(obj));\r
-\r
-        if (clazz.equals(boolean[].class))\r
-            return (T) toBooleanArray(obj);\r
-        if (clazz.equals(Boolean.class))\r
-            return (T) new Long(toLongScalar(obj));\r
-\r
-        throw new TypeCastException("Unsupported conversion type from "+obj.getClass()+" to "+clazz);\r
-    }\r
-\r
-\r
-\r
-    public static String toString(Object obj) {\r
-        if (obj instanceof Object[]) {\r
-            return Arrays.toString((Object[])obj);\r
-        }\r
-        if (obj instanceof double[]) return Arrays.toString((double[])obj);\r
-        if (obj instanceof boolean[]) return Arrays.toString((boolean[])obj);\r
-        if (obj instanceof byte[]) return Arrays.toString((byte[])obj);\r
-        if (obj instanceof char[]) return Arrays.toString((char[])obj);\r
-        if (obj instanceof float[]) return Arrays.toString((float[])obj);\r
-        if (obj instanceof int[]) return Arrays.toString((int[])obj);\r
-        if (obj instanceof long[]) return Arrays.toString((long[])obj);\r
-        if (obj instanceof short[]) return Arrays.toString((short[])obj);\r
-        return obj.toString();\r
-    }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.utils.datastructures;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.security.InvalidParameterException;
+import java.util.Arrays;
+import java.util.Calendar;
+
+/**
+ * Object type casting tools.
+ * 
+ * @author Toni Kalajainen
+ * @author Hannu Niemist&ouml;
+ */
+public class ValueUtils {
+
+    /**
+     * Convert double value to a number class.
+     * 
+     * @param value
+     * @param numberClass
+     * @return
+     * @throws ClassCastException
+     */
+    public static Number doubleToNumberClass(double value, Class<? extends Number> numberClass)
+    throws ClassCastException
+    {
+        if (numberClass==Integer.class)
+            return (int)value;
+        if (numberClass==Byte.class)
+            return (byte)value;
+        if (numberClass==Float.class)
+            return (float)value;
+        if (numberClass==Short.class)
+            return (short)value;
+        if (numberClass==Long.class)
+            return (long)value;
+        if (numberClass==Double.class)
+            return (double)value;
+        throw new ClassCastException("Cannot convert to "+numberClass.getName());
+    }
+
+    /**
+     * Possible object types:
+     * 
+     */
+
+    static final Boolean True = new Boolean(true);
+    static final Boolean False = new Boolean(false);
+
+    public static void serialize(DataOutput stream, Object obj) throws IOException {
+        try {
+            Class<?> clazz = obj.getClass();
+            if(clazz.isArray()) {
+                if(obj instanceof double[]) {
+                    double[] array = (double[])obj;
+                    stream.writeByte(16);
+                    stream.writeInt(array.length);
+                    for(double v : array)
+                        stream.writeDouble(v);
+                }
+                else if(obj instanceof float[]) {
+                    float[] array = (float[])obj;
+                    stream.writeByte(15);
+                    stream.writeInt(array.length);
+                    for(float v : array)
+                        stream.writeFloat(v);
+                }
+                else if(obj instanceof int[]) {
+                    int[] array = (int[])obj;
+                    stream.writeByte(13);
+                    stream.writeInt(array.length);
+                    for(int v : array)
+                        stream.writeInt(v);
+                }
+                else if(obj instanceof String[]) {
+                    String[] array = (String[])obj;
+                    stream.writeByte(17);
+                    stream.writeInt(array.length);
+                    for(String v : array)
+                        stream.writeUTF(v);
+                }
+                else if(obj instanceof boolean[]) {
+                    boolean[] array = (boolean[])obj;
+                    stream.writeByte(11);
+                    stream.writeInt(array.length);
+                    for(boolean v : array)
+                        stream.writeByte(v ? (byte)1 : (byte)0);
+                }
+                else if(obj instanceof byte[]) {
+                    byte[] array = (byte[])obj;
+                    stream.writeByte(12);
+                    stream.writeInt(array.length);
+                    for(byte v : array)
+                        stream.writeByte(v);
+                }
+                else if(obj instanceof long[]) {
+                    long[] array = (long[])obj;
+                    stream.writeByte(14);
+                    stream.writeInt(array.length);
+                    for(long v : array)
+                        stream.writeLong(v);
+                }
+                else {
+                    System.out.println("ValueUtils.serialize failed (invalid type).");
+                    throw new InvalidParameterException();
+                }
+            }
+            else {
+                if(clazz == Double.class) {
+                    stream.writeByte(6);
+                    stream.writeDouble((Double)obj);
+                }
+                else if(clazz == Float.class) {
+                    stream.writeByte(5);
+                    stream.writeDouble((Float)obj);
+                }
+                else if(clazz == Integer.class) {
+                    stream.writeByte(3);
+                    stream.writeInt((Integer)obj);
+                }
+                else if(clazz == String.class) {
+                    stream.writeByte(7);
+                    stream.writeUTF((String)obj);
+                }
+                else if(clazz == Boolean.class) {
+                    stream.writeByte((Boolean)obj ? 1 : 0);
+                }
+                else if(clazz == Byte.class) {
+                    stream.writeByte(2);
+                    stream.writeByte((Byte)obj);
+                }
+                else if(clazz == Long.class) {
+                    stream.writeByte(4);
+                    stream.writeLong((Long)obj);
+                }
+                else {
+                    System.out.println("ValueUtils.serialize failed (invalid type).");
+                    throw new InvalidParameterException();
+                }
+            }
+        } catch(IOException e) {
+            System.out.println("ValueUtils.serialize failed (write failure).");
+            e.printStackTrace();
+            throw e;
+        }
+    }
+
+    public static Object deserialize(DataInput stream) throws IOException {
+        try {
+            byte typeCode = stream.readByte();
+            switch(typeCode) {
+                case 0: return False;
+                case 1: return True;
+                case 2: return stream.readByte();
+                case 3: return stream.readInt();
+                case 4: return stream.readLong();
+                case 5: return stream.readFloat();
+                case 6: return stream.readDouble();
+                case 7: return stream.readUTF();
+
+                case 11: {
+                    int length = stream.readInt();
+                    boolean[] value = new boolean[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readByte() != 0;
+                    return value;
+                }
+                case 12: {
+                    int length = stream.readInt();
+                    byte[] value = new byte[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readByte();
+                    return value;
+                }
+                case 13: {
+                    int length = stream.readInt();
+                    int[] value = new int[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readInt();
+                    return value;
+                }
+                case 14: {
+                    int length = stream.readInt();
+                    long[] value = new long[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readLong();
+                    return value;
+                }
+                case 15: {
+                    int length = stream.readInt();
+                    float[] value = new float[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readFloat();
+                    return value;
+                }
+                case 16: {
+                    int length = stream.readInt();
+                    double[] value = new double[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readDouble();
+                    return value;
+                }
+                case 17: {
+                    int length = stream.readInt();
+                    String[] value = new String[length];
+                    for(int i=0;i<length;++i)
+                        value[i] = stream.readUTF();
+                    return value;
+                }
+
+            }
+
+            System.out.println("ValueUtils.deserialize failed (invalid type code).");
+            throw new IOException("Invalid value data.");
+        } catch(IOException e) {
+            System.out.println("ValueUtils.deserialize failed (reading failure).");
+            e.printStackTrace();
+            throw e;
+        }
+    }
+
+
+
+
+
+
+    /**
+     * is object an array type
+     * @param obj the object
+     * @return true if it is object array (Integer[], Short[], ect)
+     */
+    public static boolean isArray(Object obj) {
+        return obj.getClass().isArray();
+    }
+
+    /**
+     * Get the array length
+     * @param obj object
+     * @return the length
+     */
+    public static int getArrayLength(Object obj) {
+        return Array.getLength(obj);
+    }
+
+    /**
+     * Type casts an array to double.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static double[] toDoubleArray(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            double scalar = toDoubleScalar(obj);
+            return new double[] {scalar};
+        }
+        int len = getArrayLength(obj);
+        double[] result = new double[len];
+        // TODO add support for int[] double[] float[], ect..
+        if (obj instanceof Calendar[])
+        {
+            Calendar[] array = (Calendar[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].getTimeInMillis();
+            return result;
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].doubleValue();
+            return result;
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].doubleValue();
+            return result;
+        }
+        if (obj instanceof double[])
+        {
+            return (double[])obj;
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].doubleValue();
+            return result;
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].doubleValue();
+            return result;
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].doubleValue();
+            return result;
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].doubleValue();
+            return result;
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            for (int i=0; i<len; i++)
+                try {
+                    result[i] = new Double(array[i]);
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+                return result;
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i].booleanValue()?1:0);
+            return result;
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i]?1:0);
+            return result;
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+    /**
+     * Type casts an object to long.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static double toDoubleScalar(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            if (obj instanceof Number)
+                return ((Number)obj).doubleValue();
+            if (obj instanceof Calendar)
+                return ((Calendar) obj).getTimeInMillis();
+            if (obj instanceof String)
+            {
+                String str = (String) obj;
+                try {
+                    Double d = new Double(str);
+                    return d.doubleValue();
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+            }
+            if (obj instanceof Boolean) {
+                return (((Boolean)obj).booleanValue())?1:0;
+            }
+            throw new TypeCastException(obj.getClass());
+        }
+        int len = getArrayLength(obj);
+        if (len!=1)
+            throw new TypeCastException("Expected length of array is 1");
+
+        if (obj instanceof double[])
+        {
+            return ((double[])obj)[0];
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Calendar[])
+        {
+            Calendar[] array = (Calendar[]) obj;
+            return array[0].getTimeInMillis();
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            return array[0].doubleValue();
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            return array[0].doubleValue();
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            return array[0].doubleValue();
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            return array[0].doubleValue();
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            return array[0].doubleValue();
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            return array[0].doubleValue();
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            return array[0];
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            try {
+                return new Double(array[0]);
+            } catch (NumberFormatException e) {
+                throw new TypeCastException(obj.getClass());
+            }
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            return (array[0].booleanValue()?1:0);
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            return (array[0]?1:0);
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+
+    /**
+     * Type casts an array to float.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static float[] toFloatArray(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            float scalar = toFloatScalar(obj);
+            return new float[] {scalar};
+        }
+        int len = getArrayLength(obj);
+        float[] result = new float[len];
+        // TODO add support for int[] float[] float[], ect..
+        if (obj instanceof Calendar[])
+        {
+            Calendar[] array = (Calendar[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].getTimeInMillis();
+            return result;
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].floatValue();
+            return result;
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].floatValue();
+            return result;
+        }
+        if (obj instanceof float[])
+        {
+            return (float[])obj;
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].floatValue();
+            return result;
+        }
+        if (obj instanceof double[])
+        {
+            double[] array = (double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (float)array[i];
+            return result;
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].floatValue();
+            return result;
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].floatValue();
+            return result;
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].floatValue();
+            return result;
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            for (int i=0; i<len; i++)
+                try {
+                    result[i] = new Float(array[i]);
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+                return result;
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i].booleanValue()?1:0);
+            return result;
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i]?1:0);
+            return result;
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+    /**
+     * Type casts an object to float.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static float toFloatScalar(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            if (obj instanceof Number)
+                return ((Number)obj).floatValue();
+            if (obj instanceof Calendar)
+                return ((Calendar) obj).getTimeInMillis();
+            if (obj instanceof String)
+            {
+                String str = (String) obj;
+                try {
+                    Float d = new Float(str);
+                    return d.floatValue();
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+            }
+            if (obj instanceof Boolean) {
+                return (((Boolean)obj).booleanValue())?1:0;
+            }
+            throw new TypeCastException(obj.getClass());
+        }
+        int len = getArrayLength(obj);
+        if (len!=1)
+            throw new TypeCastException("Expected length of array is 1");
+
+        if (obj instanceof float[])
+        {
+            return ((float[])obj)[0];
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Calendar[])
+        {
+            Calendar[] array = (Calendar[]) obj;
+            return array[0].getTimeInMillis();
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            return array[0].floatValue();
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            return array[0].floatValue();
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            return array[0].floatValue();
+        }
+        if (obj instanceof double[])
+        {
+            double[] array = (double[]) obj;
+            return (float)array[0];
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            return array[0].floatValue();
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            return array[0].floatValue();
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            return array[0].floatValue();
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            return array[0];
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            try {
+                return new Float(array[0]);
+            } catch (NumberFormatException e) {
+                throw new TypeCastException(obj.getClass());
+            }
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            return (array[0].booleanValue()?1:0);
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            return (array[0]?1:0);
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+
+    /**
+     * Type casts an object to long.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static long toLongScalar(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            if (obj instanceof Number)
+                return ((Number)obj).longValue();
+            if (obj instanceof Calendar)
+                return ((Calendar) obj).getTimeInMillis();
+            if (obj instanceof String)
+            {
+                String str = (String) obj;
+                try {
+                    Long d = new Long(str);
+                    return d.longValue();
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+            }
+            if (obj instanceof Boolean) {
+                return (((Boolean)obj).booleanValue())?1:0;
+            }
+            throw new TypeCastException(obj.getClass());
+        }
+        int len = getArrayLength(obj);
+        if (len!=1)
+            throw new TypeCastException("Expected length of array is 1");
+
+        if (obj instanceof double[])
+        {
+            return ((long[])obj)[0];
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Calendar[])
+        {
+            Calendar[] array = (Calendar[]) obj;
+            return array[0].getTimeInMillis();
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            return array[0].longValue();
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            return array[0].longValue();
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            return array[0].longValue();
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            return (long)array[0];
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            return array[0].longValue();
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            return array[0].longValue();
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            return array[0].longValue();
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            return array[0];
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            try {
+                return new Long(array[0]);
+            } catch (NumberFormatException e) {
+                throw new TypeCastException(obj.getClass());
+            }
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            return (array[0].booleanValue()?1:0);
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            return (array[0]?1:0);
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+    /**
+     * Type casts an array to long.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static long[] toLongArray(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            long scalar = toLongScalar(obj);
+            return new long[] {scalar};
+        }
+        int len = getArrayLength(obj);
+        long[] result = new long[len];
+        // TODO add support for int[] long[] float[], ect..
+        if (obj instanceof Calendar[])
+        {
+            Calendar[] array = (Calendar[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].getTimeInMillis();
+            return result;
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].longValue();
+            return result;
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].longValue();
+            return result;
+        }
+        if (obj instanceof long[])
+        {
+            return (long[])obj;
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].longValue();
+            return result;
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (long)array[i];
+            return result;
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].longValue();
+            return result;
+        }
+        if (obj instanceof double[])
+        {
+            double[] array = (double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (long)array[i];
+            return result;
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].longValue();
+            return result;
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].longValue();
+            return result;
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            for (int i=0; i<len; i++)
+                try {
+                    result[i] = new Long(array[i]);
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+                return result;
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i].booleanValue()?1:0);
+            return result;
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i]?1:0);
+            return result;
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+
+
+
+
+
+    /**
+     * Type casts an object to int.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static int toIntegerScalar(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            if (obj instanceof Number)
+                return ((Number)obj).intValue();
+            if (obj instanceof String)
+            {
+                String str = (String) obj;
+                try {
+                    Integer d = new Integer(str);
+                    return d.intValue();
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+            }
+            if (obj instanceof Boolean) {
+                return (((Boolean)obj).booleanValue())?1:0;
+            }
+            throw new TypeCastException(obj.getClass());
+        }
+        int len = getArrayLength(obj);
+        if (len!=1)
+            throw new TypeCastException("Expected length of array is 1");
+
+        if (obj instanceof double[])
+        {
+            return ((int[])obj)[0];
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            return array[0].intValue();
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            return array[0].intValue();
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            return array[0].intValue();
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            return (int)array[0];
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            return array[0].intValue();
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            return (int)array[0];
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            return array[0].intValue();
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            return array[0];
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            return array[0].intValue();
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            return array[0];
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            try {
+                return new Integer(array[0]);
+            } catch (NumberFormatException e) {
+                throw new TypeCastException(obj.getClass());
+            }
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            return (array[0].booleanValue()?1:0);
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            return (array[0]?1:0);
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+    /**
+     * Type casts an array to int.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static int[] toIntegerArray(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            int scalar = toIntegerScalar(obj);
+            return new int[] {scalar};
+        }
+        int len = getArrayLength(obj);
+        int[] result = new int[len];
+        // TODO add support for int[] int[] float[], ect..
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].intValue();
+            return result;
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (int)array[i];
+            return result;
+        }
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].intValue();
+            return result;
+        }
+        if (obj instanceof int[])
+        {
+            return (int[])obj;
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].intValue();
+            return result;
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (int)array[i];
+            return result;
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].intValue();
+            return result;
+        }
+        if (obj instanceof double[])
+        {
+            double[] array = (double[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (int)array[i];
+            return result;
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].intValue();
+            return result;
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].intValue();
+            return result;
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i];
+            return result;
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            for (int i=0; i<len; i++)
+                try {
+                    result[i] = new Integer(array[i]);
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+                return result;
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i].booleanValue()?1:0);
+            return result;
+        }
+        if (obj instanceof boolean[])
+        {
+            boolean[] array = (boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = (array[i]?1:0);
+            return result;
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+
+
+
+    /**
+     * Type casts an array to boolean.
+     *
+     * 0 is converted to false, 1 is converted to true, others throw TypeCastException
+     * 
+     * @return type casted value
+     * @throws TypeCastException
+     */
+    public static boolean[] toBooleanArray(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            boolean scalar = toBooleanScalar(obj);
+            return new boolean[] {scalar};
+        }
+        int len = getArrayLength(obj);
+        boolean[] result = new boolean[len];
+        if (obj instanceof Integer[])
+        {
+            Integer[] array = (Integer[]) obj;
+            for (int i=0; i<len; i++) {
+                int value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof int[])
+        {
+            int[] array = (int[]) obj;
+            for (int i=0; i<len; i++) {
+                int value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            for (int i=0; i<len; i++)
+                result[i] = array[i].booleanValue();
+            return result;
+        }
+        if (obj instanceof boolean[])
+        {
+            return (boolean[])obj;
+        }
+        if (obj instanceof Float[])
+        {
+            Float[] array = (Float[]) obj;
+            for (int i=0; i<len; i++) {
+                float value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof float[])
+        {
+            float[] array = (float[]) obj;
+            for (int i=0; i<len; i++) {
+                float value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof Long[])
+        {
+            Long[] array = (Long[]) obj;
+            for (int i=0; i<len; i++) {
+                long value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof long[])
+        {
+            long[] array = (long[]) obj;
+            for (int i=0; i<len; i++) {
+                long value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof Short[])
+        {
+            Short[] array = (Short[]) obj;
+            for (int i=0; i<len; i++) {
+                short value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof short[])
+        {
+            short[] array = (short[]) obj;
+            for (int i=0; i<len; i++) {
+                short value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte[] array = (Byte[]) obj;
+            for (int i=0; i<len; i++) {
+                byte value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof byte[])
+        {
+            byte[] array = (byte[]) obj;
+            for (int i=0; i<len; i++) {
+                byte value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            for (int i=0; i<len; i++)
+                try {
+                    result[i] = new Boolean(array[i]);
+                } catch (NumberFormatException e) {
+                    throw new TypeCastException(obj.getClass());
+                }
+                return result;
+        }
+        if (obj instanceof Double[])
+        {
+            Double[] array = (Double[]) obj;
+            for (int i=0; i<len; i++) {
+                double value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        if (obj instanceof double[])
+        {
+            double[] array = (double[]) obj;
+            for (int i=0; i<len; i++) {
+                double value = array[i];
+                if (value==0) result[i] = false;
+                else if (value==1) result[i] = true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            return result;
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+    /**
+     * Type casts an object to long.
+     * 
+     * String types are attempted to be converted. If string
+     * doesn't represent numeric value, 0 will be returned
+     * 
+     * boolean types return 0/1, for false/true respectively
+     * 
+     * @return type casted value
+     */
+    public static boolean toBooleanScalar(Object obj)
+    throws TypeCastException
+    {
+        if (!isArray(obj)) {
+            if (obj instanceof Boolean) {
+                return ((Boolean)obj).booleanValue();
+            }
+            if (obj instanceof Byte) {
+                byte value = (Byte)obj;
+                if (value==0) return false;
+                else if (value==1) return true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            if (obj instanceof Short) {
+                short value = (Short)obj;
+                if (value==0) return false;
+                else if (value==1) return true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            if (obj instanceof Integer) {
+                int value = (Integer)obj;
+                if (value==0) return false;
+                else if (value==1) return true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            if (obj instanceof Long) {
+                long value = (Long)obj;
+                if (value==0) return false;
+                else if (value==1) return true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            if (obj instanceof Float) {
+                float value = (Float)obj;
+                if (value==0) return false;
+                else if (value==1) return true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            if (obj instanceof Double) {
+                double value = (Double)obj;
+                if (value==0) return false;
+                else if (value==1) return true;
+                else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            if (obj instanceof String) {
+                String value = (String)obj;
+                if (value.equals("true") || value.equals("1")) return true;
+                if (value.equals("false") || value.equals("0")) return false;
+                throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+            }
+            throw new TypeCastException(obj.getClass());
+        }
+        int len = getArrayLength(obj);
+        if (len!=1)
+            throw new TypeCastException("Expected length of array is 1");
+
+        if (obj instanceof boolean[])
+        {
+            return ((boolean[])obj)[0];
+        }
+        if (obj instanceof Boolean[])
+        {
+            Boolean[] array = (Boolean[]) obj;
+            return array[0].booleanValue();
+        }
+        if (obj instanceof int[])
+        {
+            int value = ((int[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof Integer[])
+        {
+            int value = ((Integer[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof Float[])
+        {
+            float value = ((Float[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof float[])
+        {
+            float value = ((float[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof Long[])
+        {
+            long value = ((Long[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof long[])
+        {
+            long value = ((long[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof Short[])
+        {
+            Short value = ((Short[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof short[])
+        {
+            short value = ((short[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof Byte[])
+        {
+            Byte value = ((Byte[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof byte[])
+        {
+            byte value = ((byte[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof String[])
+        {
+            String[] array = (String[]) obj;
+            String value = array[0];
+            if (value.equals("true") || value.equals("1")) return true;
+            if (value.equals("false") || value.equals("0")) return false;
+            throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof Double[])
+        {
+            double value = ((Double[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        if (obj instanceof double[])
+        {
+            double value = ((double[])obj)[0];
+            if (value==0) return false;
+            else if (value==1) return true;
+            else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
+        }
+        throw new TypeCastException(obj.getClass());
+    }
+
+
+    @SuppressWarnings("unchecked")
+    public static <T> T adaptToClass(Object obj, Class<T> clazz)
+    throws TypeCastException
+    {
+        if (clazz.equals(double[].class))
+            return (T) toDoubleArray(obj);
+        if (clazz.equals(Double.class))
+            return (T) new Double(toDoubleScalar(obj));
+
+        if (clazz.equals(float[].class))
+            return (T) toFloatArray(obj);
+        if (clazz.equals(Float.class))
+            return (T) new Float(toFloatScalar(obj));
+
+        if (clazz.equals(long[].class))
+            return (T) toLongArray(obj);
+        if (clazz.equals(Long.class))
+            return (T) new Long(toLongScalar(obj));
+
+        if (clazz.equals(int[].class))
+            return (T) toIntegerArray(obj);
+        if (clazz.equals(Integer.class))
+            return (T) new Integer(toIntegerScalar(obj));
+
+        if (clazz.equals(boolean[].class))
+            return (T) toBooleanArray(obj);
+        if (clazz.equals(Boolean.class))
+            return (T) new Long(toLongScalar(obj));
+
+        throw new TypeCastException("Unsupported conversion type from "+obj.getClass()+" to "+clazz);
+    }
+
+
+
+    public static String toString(Object obj) {
+        if (obj instanceof Object[]) {
+            return Arrays.toString((Object[])obj);
+        }
+        if (obj instanceof double[]) return Arrays.toString((double[])obj);
+        if (obj instanceof boolean[]) return Arrays.toString((boolean[])obj);
+        if (obj instanceof byte[]) return Arrays.toString((byte[])obj);
+        if (obj instanceof char[]) return Arrays.toString((char[])obj);
+        if (obj instanceof float[]) return Arrays.toString((float[])obj);
+        if (obj instanceof int[]) return Arrays.toString((int[])obj);
+        if (obj instanceof long[]) return Arrays.toString((long[])obj);
+        if (obj instanceof short[]) return Arrays.toString((short[])obj);
+        return obj.toString();
+    }
+}