1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.datastructures;
14 import java.io.DataInput;
15 import java.io.DataOutput;
16 import java.io.IOException;
17 import java.lang.reflect.Array;
18 import java.security.InvalidParameterException;
19 import java.util.Arrays;
20 import java.util.Calendar;
23 * Object type casting tools.
25 * @author Toni Kalajainen
26 * @author Hannu Niemistö
28 public class ValueUtils {
31 * Convert double value to a number class.
36 * @throws ClassCastException
38 public static Number doubleToNumberClass(double value, Class<? extends Number> numberClass)
39 throws ClassCastException
41 if (numberClass==Integer.class)
43 if (numberClass==Byte.class)
45 if (numberClass==Float.class)
47 if (numberClass==Short.class)
49 if (numberClass==Long.class)
51 if (numberClass==Double.class)
53 throw new ClassCastException("Cannot convert to "+numberClass.getName());
57 * Possible object types:
61 static final Boolean True = new Boolean(true);
62 static final Boolean False = new Boolean(false);
64 public static void serialize(DataOutput stream, Object obj) throws IOException {
66 Class<?> clazz = obj.getClass();
68 if(obj instanceof double[]) {
69 double[] array = (double[])obj;
71 stream.writeInt(array.length);
73 stream.writeDouble(v);
75 else if(obj instanceof float[]) {
76 float[] array = (float[])obj;
78 stream.writeInt(array.length);
82 else if(obj instanceof int[]) {
83 int[] array = (int[])obj;
85 stream.writeInt(array.length);
89 else if(obj instanceof String[]) {
90 String[] array = (String[])obj;
92 stream.writeInt(array.length);
96 else if(obj instanceof boolean[]) {
97 boolean[] array = (boolean[])obj;
99 stream.writeInt(array.length);
100 for(boolean v : array)
101 stream.writeByte(v ? (byte)1 : (byte)0);
103 else if(obj instanceof byte[]) {
104 byte[] array = (byte[])obj;
105 stream.writeByte(12);
106 stream.writeInt(array.length);
110 else if(obj instanceof long[]) {
111 long[] array = (long[])obj;
112 stream.writeByte(14);
113 stream.writeInt(array.length);
118 System.out.println("ValueUtils.serialize failed (invalid type).");
119 throw new InvalidParameterException();
123 if(clazz == Double.class) {
125 stream.writeDouble((Double)obj);
127 else if(clazz == Float.class) {
129 stream.writeDouble((Float)obj);
131 else if(clazz == Integer.class) {
133 stream.writeInt((Integer)obj);
135 else if(clazz == String.class) {
137 stream.writeUTF((String)obj);
139 else if(clazz == Boolean.class) {
140 stream.writeByte((Boolean)obj ? 1 : 0);
142 else if(clazz == Byte.class) {
144 stream.writeByte((Byte)obj);
146 else if(clazz == Long.class) {
148 stream.writeLong((Long)obj);
151 System.out.println("ValueUtils.serialize failed (invalid type).");
152 throw new InvalidParameterException();
155 } catch(IOException e) {
156 System.out.println("ValueUtils.serialize failed (write failure).");
162 public static Object deserialize(DataInput stream) throws IOException {
164 byte typeCode = stream.readByte();
166 case 0: return False;
168 case 2: return stream.readByte();
169 case 3: return stream.readInt();
170 case 4: return stream.readLong();
171 case 5: return stream.readFloat();
172 case 6: return stream.readDouble();
173 case 7: return stream.readUTF();
176 int length = stream.readInt();
177 boolean[] value = new boolean[length];
178 for(int i=0;i<length;++i)
179 value[i] = stream.readByte() != 0;
183 int length = stream.readInt();
184 byte[] value = new byte[length];
185 for(int i=0;i<length;++i)
186 value[i] = stream.readByte();
190 int length = stream.readInt();
191 int[] value = new int[length];
192 for(int i=0;i<length;++i)
193 value[i] = stream.readInt();
197 int length = stream.readInt();
198 long[] value = new long[length];
199 for(int i=0;i<length;++i)
200 value[i] = stream.readLong();
204 int length = stream.readInt();
205 float[] value = new float[length];
206 for(int i=0;i<length;++i)
207 value[i] = stream.readFloat();
211 int length = stream.readInt();
212 double[] value = new double[length];
213 for(int i=0;i<length;++i)
214 value[i] = stream.readDouble();
218 int length = stream.readInt();
219 String[] value = new String[length];
220 for(int i=0;i<length;++i)
221 value[i] = stream.readUTF();
227 System.out.println("ValueUtils.deserialize failed (invalid type code).");
228 throw new IOException("Invalid value data.");
229 } catch(IOException e) {
230 System.out.println("ValueUtils.deserialize failed (reading failure).");
242 * is object an array type
243 * @param obj the object
244 * @return true if it is object array (Integer[], Short[], ect)
246 public static boolean isArray(Object obj) {
247 return obj.getClass().isArray();
251 * Get the array length
255 public static int getArrayLength(Object obj) {
256 return Array.getLength(obj);
260 * Type casts an array to double.
262 * String types are attempted to be converted. If string
263 * doesn't represent numeric value, 0 will be returned
265 * boolean types return 0/1, for false/true respectively
267 * @return type casted value
269 public static double[] toDoubleArray(Object obj)
270 throws TypeCastException
273 double scalar = toDoubleScalar(obj);
274 return new double[] {scalar};
276 int len = getArrayLength(obj);
277 double[] result = new double[len];
278 // TODO add support for int[] double[] float[], ect..
279 if (obj instanceof Calendar[])
281 Calendar[] array = (Calendar[]) obj;
282 for (int i=0; i<len; i++)
283 result[i] = array[i].getTimeInMillis();
286 if (obj instanceof Integer[])
288 Integer[] array = (Integer[]) obj;
289 for (int i=0; i<len; i++)
290 result[i] = array[i].doubleValue();
293 if (obj instanceof int[])
295 int[] array = (int[]) obj;
296 for (int i=0; i<len; i++)
297 result[i] = array[i];
300 if (obj instanceof Double[])
302 Double[] array = (Double[]) obj;
303 for (int i=0; i<len; i++)
304 result[i] = array[i].doubleValue();
307 if (obj instanceof double[])
309 return (double[])obj;
311 if (obj instanceof Float[])
313 Float[] array = (Float[]) obj;
314 for (int i=0; i<len; i++)
315 result[i] = array[i].doubleValue();
318 if (obj instanceof float[])
320 float[] array = (float[]) obj;
321 for (int i=0; i<len; i++)
322 result[i] = array[i];
325 if (obj instanceof Long[])
327 Long[] array = (Long[]) obj;
328 for (int i=0; i<len; i++)
329 result[i] = array[i].doubleValue();
332 if (obj instanceof long[])
334 long[] array = (long[]) obj;
335 for (int i=0; i<len; i++)
336 result[i] = array[i];
339 if (obj instanceof Short[])
341 Short[] array = (Short[]) obj;
342 for (int i=0; i<len; i++)
343 result[i] = array[i].doubleValue();
346 if (obj instanceof short[])
348 short[] array = (short[]) obj;
349 for (int i=0; i<len; i++)
350 result[i] = array[i];
353 if (obj instanceof Byte[])
355 Byte[] array = (Byte[]) obj;
356 for (int i=0; i<len; i++)
357 result[i] = array[i].doubleValue();
360 if (obj instanceof byte[])
362 byte[] array = (byte[]) obj;
363 for (int i=0; i<len; i++)
364 result[i] = array[i];
367 if (obj instanceof String[])
369 String[] array = (String[]) obj;
370 for (int i=0; i<len; i++)
372 result[i] = new Double(array[i]);
373 } catch (NumberFormatException e) {
374 throw new TypeCastException(obj.getClass());
378 if (obj instanceof Boolean[])
380 Boolean[] array = (Boolean[]) obj;
381 for (int i=0; i<len; i++)
382 result[i] = (array[i].booleanValue()?1:0);
385 if (obj instanceof boolean[])
387 boolean[] array = (boolean[]) obj;
388 for (int i=0; i<len; i++)
389 result[i] = (array[i]?1:0);
392 throw new TypeCastException(obj.getClass());
396 * Type casts an object to long.
398 * String types are attempted to be converted. If string
399 * doesn't represent numeric value, 0 will be returned
401 * boolean types return 0/1, for false/true respectively
403 * @return type casted value
405 public static double toDoubleScalar(Object obj)
406 throws TypeCastException
409 if (obj instanceof Number)
410 return ((Number)obj).doubleValue();
411 if (obj instanceof Calendar)
412 return ((Calendar) obj).getTimeInMillis();
413 if (obj instanceof String)
415 String str = (String) obj;
417 Double d = new Double(str);
418 return d.doubleValue();
419 } catch (NumberFormatException e) {
420 throw new TypeCastException(obj.getClass());
423 if (obj instanceof Boolean) {
424 return (((Boolean)obj).booleanValue())?1:0;
426 throw new TypeCastException(obj.getClass());
428 int len = getArrayLength(obj);
430 throw new TypeCastException("Expected length of array is 1");
432 if (obj instanceof double[])
434 return ((double[])obj)[0];
436 if (obj instanceof int[])
438 int[] array = (int[]) obj;
441 if (obj instanceof Calendar[])
443 Calendar[] array = (Calendar[]) obj;
444 return array[0].getTimeInMillis();
446 if (obj instanceof Integer[])
448 Integer[] array = (Integer[]) obj;
449 return array[0].doubleValue();
451 if (obj instanceof Double[])
453 Double[] array = (Double[]) obj;
454 return array[0].doubleValue();
456 if (obj instanceof Float[])
458 Float[] array = (Float[]) obj;
459 return array[0].doubleValue();
461 if (obj instanceof float[])
463 float[] array = (float[]) obj;
466 if (obj instanceof Long[])
468 Long[] array = (Long[]) obj;
469 return array[0].doubleValue();
471 if (obj instanceof long[])
473 long[] array = (long[]) obj;
476 if (obj instanceof Short[])
478 Short[] array = (Short[]) obj;
479 return array[0].doubleValue();
481 if (obj instanceof short[])
483 short[] array = (short[]) obj;
486 if (obj instanceof Byte[])
488 Byte[] array = (Byte[]) obj;
489 return array[0].doubleValue();
491 if (obj instanceof byte[])
493 byte[] array = (byte[]) obj;
496 if (obj instanceof String[])
498 String[] array = (String[]) obj;
500 return new Double(array[0]);
501 } catch (NumberFormatException e) {
502 throw new TypeCastException(obj.getClass());
505 if (obj instanceof Boolean[])
507 Boolean[] array = (Boolean[]) obj;
508 return (array[0].booleanValue()?1:0);
510 if (obj instanceof boolean[])
512 boolean[] array = (boolean[]) obj;
513 return (array[0]?1:0);
515 throw new TypeCastException(obj.getClass());
520 * Type casts an array to float.
522 * String types are attempted to be converted. If string
523 * doesn't represent numeric value, 0 will be returned
525 * boolean types return 0/1, for false/true respectively
527 * @return type casted value
529 public static float[] toFloatArray(Object obj)
530 throws TypeCastException
533 float scalar = toFloatScalar(obj);
534 return new float[] {scalar};
536 int len = getArrayLength(obj);
537 float[] result = new float[len];
538 // TODO add support for int[] float[] float[], ect..
539 if (obj instanceof Calendar[])
541 Calendar[] array = (Calendar[]) obj;
542 for (int i=0; i<len; i++)
543 result[i] = array[i].getTimeInMillis();
546 if (obj instanceof Integer[])
548 Integer[] array = (Integer[]) obj;
549 for (int i=0; i<len; i++)
550 result[i] = array[i].floatValue();
553 if (obj instanceof int[])
555 int[] array = (int[]) obj;
556 for (int i=0; i<len; i++)
557 result[i] = array[i];
560 if (obj instanceof Float[])
562 Float[] array = (Float[]) obj;
563 for (int i=0; i<len; i++)
564 result[i] = array[i].floatValue();
567 if (obj instanceof float[])
571 if (obj instanceof Double[])
573 Double[] array = (Double[]) obj;
574 for (int i=0; i<len; i++)
575 result[i] = array[i].floatValue();
578 if (obj instanceof double[])
580 double[] array = (double[]) obj;
581 for (int i=0; i<len; i++)
582 result[i] = (float)array[i];
585 if (obj instanceof Long[])
587 Long[] array = (Long[]) obj;
588 for (int i=0; i<len; i++)
589 result[i] = array[i].floatValue();
592 if (obj instanceof long[])
594 long[] array = (long[]) obj;
595 for (int i=0; i<len; i++)
596 result[i] = array[i];
599 if (obj instanceof Short[])
601 Short[] array = (Short[]) obj;
602 for (int i=0; i<len; i++)
603 result[i] = array[i].floatValue();
606 if (obj instanceof short[])
608 short[] array = (short[]) obj;
609 for (int i=0; i<len; i++)
610 result[i] = array[i];
613 if (obj instanceof Byte[])
615 Byte[] array = (Byte[]) obj;
616 for (int i=0; i<len; i++)
617 result[i] = array[i].floatValue();
620 if (obj instanceof byte[])
622 byte[] array = (byte[]) obj;
623 for (int i=0; i<len; i++)
624 result[i] = array[i];
627 if (obj instanceof String[])
629 String[] array = (String[]) obj;
630 for (int i=0; i<len; i++)
632 result[i] = new Float(array[i]);
633 } catch (NumberFormatException e) {
634 throw new TypeCastException(obj.getClass());
638 if (obj instanceof Boolean[])
640 Boolean[] array = (Boolean[]) obj;
641 for (int i=0; i<len; i++)
642 result[i] = (array[i].booleanValue()?1:0);
645 if (obj instanceof boolean[])
647 boolean[] array = (boolean[]) obj;
648 for (int i=0; i<len; i++)
649 result[i] = (array[i]?1:0);
652 throw new TypeCastException(obj.getClass());
656 * Type casts an object to float.
658 * String types are attempted to be converted. If string
659 * doesn't represent numeric value, 0 will be returned
661 * boolean types return 0/1, for false/true respectively
663 * @return type casted value
665 public static float toFloatScalar(Object obj)
666 throws TypeCastException
669 if (obj instanceof Number)
670 return ((Number)obj).floatValue();
671 if (obj instanceof Calendar)
672 return ((Calendar) obj).getTimeInMillis();
673 if (obj instanceof String)
675 String str = (String) obj;
677 Float d = new Float(str);
678 return d.floatValue();
679 } catch (NumberFormatException e) {
680 throw new TypeCastException(obj.getClass());
683 if (obj instanceof Boolean) {
684 return (((Boolean)obj).booleanValue())?1:0;
686 throw new TypeCastException(obj.getClass());
688 int len = getArrayLength(obj);
690 throw new TypeCastException("Expected length of array is 1");
692 if (obj instanceof float[])
694 return ((float[])obj)[0];
696 if (obj instanceof int[])
698 int[] array = (int[]) obj;
701 if (obj instanceof Calendar[])
703 Calendar[] array = (Calendar[]) obj;
704 return array[0].getTimeInMillis();
706 if (obj instanceof Integer[])
708 Integer[] array = (Integer[]) obj;
709 return array[0].floatValue();
711 if (obj instanceof Float[])
713 Float[] array = (Float[]) obj;
714 return array[0].floatValue();
716 if (obj instanceof Float[])
718 Float[] array = (Float[]) obj;
719 return array[0].floatValue();
721 if (obj instanceof double[])
723 double[] array = (double[]) obj;
724 return (float)array[0];
726 if (obj instanceof Long[])
728 Long[] array = (Long[]) obj;
729 return array[0].floatValue();
731 if (obj instanceof long[])
733 long[] array = (long[]) obj;
736 if (obj instanceof Short[])
738 Short[] array = (Short[]) obj;
739 return array[0].floatValue();
741 if (obj instanceof short[])
743 short[] array = (short[]) obj;
746 if (obj instanceof Byte[])
748 Byte[] array = (Byte[]) obj;
749 return array[0].floatValue();
751 if (obj instanceof byte[])
753 byte[] array = (byte[]) obj;
756 if (obj instanceof String[])
758 String[] array = (String[]) obj;
760 return new Float(array[0]);
761 } catch (NumberFormatException e) {
762 throw new TypeCastException(obj.getClass());
765 if (obj instanceof Boolean[])
767 Boolean[] array = (Boolean[]) obj;
768 return (array[0].booleanValue()?1:0);
770 if (obj instanceof boolean[])
772 boolean[] array = (boolean[]) obj;
773 return (array[0]?1:0);
775 throw new TypeCastException(obj.getClass());
780 * Type casts an object to long.
782 * String types are attempted to be converted. If string
783 * doesn't represent numeric value, 0 will be returned
785 * boolean types return 0/1, for false/true respectively
787 * @return type casted value
789 public static long toLongScalar(Object obj)
790 throws TypeCastException
793 if (obj instanceof Number)
794 return ((Number)obj).longValue();
795 if (obj instanceof Calendar)
796 return ((Calendar) obj).getTimeInMillis();
797 if (obj instanceof String)
799 String str = (String) obj;
801 Long d = new Long(str);
802 return d.longValue();
803 } catch (NumberFormatException e) {
804 throw new TypeCastException(obj.getClass());
807 if (obj instanceof Boolean) {
808 return (((Boolean)obj).booleanValue())?1:0;
810 throw new TypeCastException(obj.getClass());
812 int len = getArrayLength(obj);
814 throw new TypeCastException("Expected length of array is 1");
816 if (obj instanceof double[])
818 return ((long[])obj)[0];
820 if (obj instanceof int[])
822 int[] array = (int[]) obj;
825 if (obj instanceof Calendar[])
827 Calendar[] array = (Calendar[]) obj;
828 return array[0].getTimeInMillis();
830 if (obj instanceof Integer[])
832 Integer[] array = (Integer[]) obj;
833 return array[0].longValue();
835 if (obj instanceof Double[])
837 Double[] array = (Double[]) obj;
838 return array[0].longValue();
840 if (obj instanceof Float[])
842 Float[] array = (Float[]) obj;
843 return array[0].longValue();
845 if (obj instanceof float[])
847 float[] array = (float[]) obj;
848 return (long)array[0];
850 if (obj instanceof Long[])
852 Long[] array = (Long[]) obj;
853 return array[0].longValue();
855 if (obj instanceof long[])
857 long[] array = (long[]) obj;
860 if (obj instanceof Short[])
862 Short[] array = (Short[]) obj;
863 return array[0].longValue();
865 if (obj instanceof short[])
867 short[] array = (short[]) obj;
870 if (obj instanceof Byte[])
872 Byte[] array = (Byte[]) obj;
873 return array[0].longValue();
875 if (obj instanceof byte[])
877 byte[] array = (byte[]) obj;
880 if (obj instanceof String[])
882 String[] array = (String[]) obj;
884 return new Long(array[0]);
885 } catch (NumberFormatException e) {
886 throw new TypeCastException(obj.getClass());
889 if (obj instanceof Boolean[])
891 Boolean[] array = (Boolean[]) obj;
892 return (array[0].booleanValue()?1:0);
894 if (obj instanceof boolean[])
896 boolean[] array = (boolean[]) obj;
897 return (array[0]?1:0);
899 throw new TypeCastException(obj.getClass());
903 * Type casts an array to long.
905 * String types are attempted to be converted. If string
906 * doesn't represent numeric value, 0 will be returned
908 * boolean types return 0/1, for false/true respectively
910 * @return type casted value
912 public static long[] toLongArray(Object obj)
913 throws TypeCastException
916 long scalar = toLongScalar(obj);
917 return new long[] {scalar};
919 int len = getArrayLength(obj);
920 long[] result = new long[len];
921 // TODO add support for int[] long[] float[], ect..
922 if (obj instanceof Calendar[])
924 Calendar[] array = (Calendar[]) obj;
925 for (int i=0; i<len; i++)
926 result[i] = array[i].getTimeInMillis();
929 if (obj instanceof Integer[])
931 Integer[] array = (Integer[]) obj;
932 for (int i=0; i<len; i++)
933 result[i] = array[i].longValue();
936 if (obj instanceof int[])
938 int[] array = (int[]) obj;
939 for (int i=0; i<len; i++)
940 result[i] = array[i];
943 if (obj instanceof Long[])
945 Long[] array = (Long[]) obj;
946 for (int i=0; i<len; i++)
947 result[i] = array[i].longValue();
950 if (obj instanceof long[])
954 if (obj instanceof Float[])
956 Float[] array = (Float[]) obj;
957 for (int i=0; i<len; i++)
958 result[i] = array[i].longValue();
961 if (obj instanceof float[])
963 float[] array = (float[]) obj;
964 for (int i=0; i<len; i++)
965 result[i] = (long)array[i];
968 if (obj instanceof Double[])
970 Double[] array = (Double[]) obj;
971 for (int i=0; i<len; i++)
972 result[i] = array[i].longValue();
975 if (obj instanceof double[])
977 double[] array = (double[]) obj;
978 for (int i=0; i<len; i++)
979 result[i] = (long)array[i];
982 if (obj instanceof Short[])
984 Short[] array = (Short[]) obj;
985 for (int i=0; i<len; i++)
986 result[i] = array[i].longValue();
989 if (obj instanceof short[])
991 short[] array = (short[]) obj;
992 for (int i=0; i<len; i++)
993 result[i] = array[i];
996 if (obj instanceof Byte[])
998 Byte[] array = (Byte[]) obj;
999 for (int i=0; i<len; i++)
1000 result[i] = array[i].longValue();
1003 if (obj instanceof byte[])
1005 byte[] array = (byte[]) obj;
1006 for (int i=0; i<len; i++)
1007 result[i] = array[i];
1010 if (obj instanceof String[])
1012 String[] array = (String[]) obj;
1013 for (int i=0; i<len; i++)
1015 result[i] = new Long(array[i]);
1016 } catch (NumberFormatException e) {
1017 throw new TypeCastException(obj.getClass());
1021 if (obj instanceof Boolean[])
1023 Boolean[] array = (Boolean[]) obj;
1024 for (int i=0; i<len; i++)
1025 result[i] = (array[i].booleanValue()?1:0);
1028 if (obj instanceof boolean[])
1030 boolean[] array = (boolean[]) obj;
1031 for (int i=0; i<len; i++)
1032 result[i] = (array[i]?1:0);
1035 throw new TypeCastException(obj.getClass());
1044 * Type casts an object to int.
1046 * String types are attempted to be converted. If string
1047 * doesn't represent numeric value, 0 will be returned
1049 * boolean types return 0/1, for false/true respectively
1051 * @return type casted value
1053 public static int toIntegerScalar(Object obj)
1054 throws TypeCastException
1056 if (!isArray(obj)) {
1057 if (obj instanceof Number)
1058 return ((Number)obj).intValue();
1059 if (obj instanceof String)
1061 String str = (String) obj;
1063 Integer d = new Integer(str);
1064 return d.intValue();
1065 } catch (NumberFormatException e) {
1066 throw new TypeCastException(obj.getClass());
1069 if (obj instanceof Boolean) {
1070 return (((Boolean)obj).booleanValue())?1:0;
1072 throw new TypeCastException(obj.getClass());
1074 int len = getArrayLength(obj);
1076 throw new TypeCastException("Expected length of array is 1");
1078 if (obj instanceof double[])
1080 return ((int[])obj)[0];
1082 if (obj instanceof int[])
1084 int[] array = (int[]) obj;
1087 if (obj instanceof Integer[])
1089 Integer[] array = (Integer[]) obj;
1090 return array[0].intValue();
1092 if (obj instanceof Double[])
1094 Double[] array = (Double[]) obj;
1095 return array[0].intValue();
1097 if (obj instanceof Float[])
1099 Float[] array = (Float[]) obj;
1100 return array[0].intValue();
1102 if (obj instanceof float[])
1104 float[] array = (float[]) obj;
1105 return (int)array[0];
1107 if (obj instanceof Long[])
1109 Long[] array = (Long[]) obj;
1110 return array[0].intValue();
1112 if (obj instanceof long[])
1114 long[] array = (long[]) obj;
1115 return (int)array[0];
1117 if (obj instanceof Short[])
1119 Short[] array = (Short[]) obj;
1120 return array[0].intValue();
1122 if (obj instanceof short[])
1124 short[] array = (short[]) obj;
1127 if (obj instanceof Byte[])
1129 Byte[] array = (Byte[]) obj;
1130 return array[0].intValue();
1132 if (obj instanceof byte[])
1134 byte[] array = (byte[]) obj;
1137 if (obj instanceof String[])
1139 String[] array = (String[]) obj;
1141 return new Integer(array[0]);
1142 } catch (NumberFormatException e) {
1143 throw new TypeCastException(obj.getClass());
1146 if (obj instanceof Boolean[])
1148 Boolean[] array = (Boolean[]) obj;
1149 return (array[0].booleanValue()?1:0);
1151 if (obj instanceof boolean[])
1153 boolean[] array = (boolean[]) obj;
1154 return (array[0]?1:0);
1156 throw new TypeCastException(obj.getClass());
1160 * Type casts an array to int.
1162 * String types are attempted to be converted. If string
1163 * doesn't represent numeric value, 0 will be returned
1165 * boolean types return 0/1, for false/true respectively
1167 * @return type casted value
1169 public static int[] toIntegerArray(Object obj)
1170 throws TypeCastException
1172 if (!isArray(obj)) {
1173 int scalar = toIntegerScalar(obj);
1174 return new int[] {scalar};
1176 int len = getArrayLength(obj);
1177 int[] result = new int[len];
1178 // TODO add support for int[] int[] float[], ect..
1179 if (obj instanceof Integer[])
1181 Integer[] array = (Integer[]) obj;
1182 for (int i=0; i<len; i++)
1183 result[i] = array[i].intValue();
1186 if (obj instanceof long[])
1188 long[] array = (long[]) obj;
1189 for (int i=0; i<len; i++)
1190 result[i] = (int)array[i];
1193 if (obj instanceof Integer[])
1195 Integer[] array = (Integer[]) obj;
1196 for (int i=0; i<len; i++)
1197 result[i] = array[i].intValue();
1200 if (obj instanceof int[])
1204 if (obj instanceof Float[])
1206 Float[] array = (Float[]) obj;
1207 for (int i=0; i<len; i++)
1208 result[i] = array[i].intValue();
1211 if (obj instanceof float[])
1213 float[] array = (float[]) obj;
1214 for (int i=0; i<len; i++)
1215 result[i] = (int)array[i];
1218 if (obj instanceof Double[])
1220 Double[] array = (Double[]) obj;
1221 for (int i=0; i<len; i++)
1222 result[i] = array[i].intValue();
1225 if (obj instanceof double[])
1227 double[] array = (double[]) obj;
1228 for (int i=0; i<len; i++)
1229 result[i] = (int)array[i];
1232 if (obj instanceof Short[])
1234 Short[] array = (Short[]) obj;
1235 for (int i=0; i<len; i++)
1236 result[i] = array[i].intValue();
1239 if (obj instanceof short[])
1241 short[] array = (short[]) obj;
1242 for (int i=0; i<len; i++)
1243 result[i] = array[i];
1246 if (obj instanceof Byte[])
1248 Byte[] array = (Byte[]) obj;
1249 for (int i=0; i<len; i++)
1250 result[i] = array[i].intValue();
1253 if (obj instanceof byte[])
1255 byte[] array = (byte[]) obj;
1256 for (int i=0; i<len; i++)
1257 result[i] = array[i];
1260 if (obj instanceof String[])
1262 String[] array = (String[]) obj;
1263 for (int i=0; i<len; i++)
1265 result[i] = new Integer(array[i]);
1266 } catch (NumberFormatException e) {
1267 throw new TypeCastException(obj.getClass());
1271 if (obj instanceof Boolean[])
1273 Boolean[] array = (Boolean[]) obj;
1274 for (int i=0; i<len; i++)
1275 result[i] = (array[i].booleanValue()?1:0);
1278 if (obj instanceof boolean[])
1280 boolean[] array = (boolean[]) obj;
1281 for (int i=0; i<len; i++)
1282 result[i] = (array[i]?1:0);
1285 throw new TypeCastException(obj.getClass());
1292 * Type casts an array to boolean.
1294 * 0 is converted to false, 1 is converted to true, others throw TypeCastException
1296 * @return type casted value
1297 * @throws TypeCastException
1299 public static boolean[] toBooleanArray(Object obj)
1300 throws TypeCastException
1302 if (!isArray(obj)) {
1303 boolean scalar = toBooleanScalar(obj);
1304 return new boolean[] {scalar};
1306 int len = getArrayLength(obj);
1307 boolean[] result = new boolean[len];
1308 if (obj instanceof Integer[])
1310 Integer[] array = (Integer[]) obj;
1311 for (int i=0; i<len; i++) {
1312 int value = array[i];
1313 if (value==0) result[i] = false;
1314 else if (value==1) result[i] = true;
1315 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1319 if (obj instanceof int[])
1321 int[] array = (int[]) obj;
1322 for (int i=0; i<len; i++) {
1323 int value = array[i];
1324 if (value==0) result[i] = false;
1325 else if (value==1) result[i] = true;
1326 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1330 if (obj instanceof Boolean[])
1332 Boolean[] array = (Boolean[]) obj;
1333 for (int i=0; i<len; i++)
1334 result[i] = array[i].booleanValue();
1337 if (obj instanceof boolean[])
1339 return (boolean[])obj;
1341 if (obj instanceof Float[])
1343 Float[] array = (Float[]) obj;
1344 for (int i=0; i<len; i++) {
1345 float value = array[i];
1346 if (value==0) result[i] = false;
1347 else if (value==1) result[i] = true;
1348 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1352 if (obj instanceof float[])
1354 float[] array = (float[]) obj;
1355 for (int i=0; i<len; i++) {
1356 float value = array[i];
1357 if (value==0) result[i] = false;
1358 else if (value==1) result[i] = true;
1359 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1363 if (obj instanceof Long[])
1365 Long[] array = (Long[]) obj;
1366 for (int i=0; i<len; i++) {
1367 long value = array[i];
1368 if (value==0) result[i] = false;
1369 else if (value==1) result[i] = true;
1370 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1374 if (obj instanceof long[])
1376 long[] array = (long[]) obj;
1377 for (int i=0; i<len; i++) {
1378 long value = array[i];
1379 if (value==0) result[i] = false;
1380 else if (value==1) result[i] = true;
1381 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1385 if (obj instanceof Short[])
1387 Short[] array = (Short[]) obj;
1388 for (int i=0; i<len; i++) {
1389 short value = array[i];
1390 if (value==0) result[i] = false;
1391 else if (value==1) result[i] = true;
1392 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1396 if (obj instanceof short[])
1398 short[] array = (short[]) obj;
1399 for (int i=0; i<len; i++) {
1400 short value = array[i];
1401 if (value==0) result[i] = false;
1402 else if (value==1) result[i] = true;
1403 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1407 if (obj instanceof Byte[])
1409 Byte[] array = (Byte[]) obj;
1410 for (int i=0; i<len; i++) {
1411 byte value = array[i];
1412 if (value==0) result[i] = false;
1413 else if (value==1) result[i] = true;
1414 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1418 if (obj instanceof byte[])
1420 byte[] array = (byte[]) obj;
1421 for (int i=0; i<len; i++) {
1422 byte value = array[i];
1423 if (value==0) result[i] = false;
1424 else if (value==1) result[i] = true;
1425 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1429 if (obj instanceof String[])
1431 String[] array = (String[]) obj;
1432 for (int i=0; i<len; i++)
1434 result[i] = new Boolean(array[i]);
1435 } catch (NumberFormatException e) {
1436 throw new TypeCastException(obj.getClass());
1440 if (obj instanceof Double[])
1442 Double[] array = (Double[]) obj;
1443 for (int i=0; i<len; i++) {
1444 double value = array[i];
1445 if (value==0) result[i] = false;
1446 else if (value==1) result[i] = true;
1447 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1451 if (obj instanceof double[])
1453 double[] array = (double[]) obj;
1454 for (int i=0; i<len; i++) {
1455 double value = array[i];
1456 if (value==0) result[i] = false;
1457 else if (value==1) result[i] = true;
1458 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1462 throw new TypeCastException(obj.getClass());
1466 * Type casts an object to long.
1468 * String types are attempted to be converted. If string
1469 * doesn't represent numeric value, 0 will be returned
1471 * boolean types return 0/1, for false/true respectively
1473 * @return type casted value
1475 public static boolean toBooleanScalar(Object obj)
1476 throws TypeCastException
1478 if (!isArray(obj)) {
1479 if (obj instanceof Boolean) {
1480 return ((Boolean)obj).booleanValue();
1482 if (obj instanceof Byte) {
1483 byte value = (Byte)obj;
1484 if (value==0) return false;
1485 else if (value==1) return true;
1486 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1488 if (obj instanceof Short) {
1489 short value = (Short)obj;
1490 if (value==0) return false;
1491 else if (value==1) return true;
1492 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1494 if (obj instanceof Integer) {
1495 int value = (Integer)obj;
1496 if (value==0) return false;
1497 else if (value==1) return true;
1498 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1500 if (obj instanceof Long) {
1501 long value = (Long)obj;
1502 if (value==0) return false;
1503 else if (value==1) return true;
1504 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1506 if (obj instanceof Float) {
1507 float value = (Float)obj;
1508 if (value==0) return false;
1509 else if (value==1) return true;
1510 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1512 if (obj instanceof Double) {
1513 double value = (Double)obj;
1514 if (value==0) return false;
1515 else if (value==1) return true;
1516 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1518 if (obj instanceof String) {
1519 String value = (String)obj;
1520 if (value.equals("true") || value.equals("1")) return true;
1521 if (value.equals("false") || value.equals("0")) return false;
1522 throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1524 throw new TypeCastException(obj.getClass());
1526 int len = getArrayLength(obj);
1528 throw new TypeCastException("Expected length of array is 1");
1530 if (obj instanceof boolean[])
1532 return ((boolean[])obj)[0];
1534 if (obj instanceof Boolean[])
1536 Boolean[] array = (Boolean[]) obj;
1537 return array[0].booleanValue();
1539 if (obj instanceof int[])
1541 int value = ((int[])obj)[0];
1542 if (value==0) return false;
1543 else if (value==1) return true;
1544 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1546 if (obj instanceof Integer[])
1548 int value = ((Integer[])obj)[0];
1549 if (value==0) return false;
1550 else if (value==1) return true;
1551 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1553 if (obj instanceof Float[])
1555 float value = ((Float[])obj)[0];
1556 if (value==0) return false;
1557 else if (value==1) return true;
1558 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1560 if (obj instanceof float[])
1562 float value = ((float[])obj)[0];
1563 if (value==0) return false;
1564 else if (value==1) return true;
1565 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1567 if (obj instanceof Long[])
1569 long value = ((Long[])obj)[0];
1570 if (value==0) return false;
1571 else if (value==1) return true;
1572 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1574 if (obj instanceof long[])
1576 long value = ((long[])obj)[0];
1577 if (value==0) return false;
1578 else if (value==1) return true;
1579 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1581 if (obj instanceof Short[])
1583 Short value = ((Short[])obj)[0];
1584 if (value==0) return false;
1585 else if (value==1) return true;
1586 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1588 if (obj instanceof short[])
1590 short value = ((short[])obj)[0];
1591 if (value==0) return false;
1592 else if (value==1) return true;
1593 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1595 if (obj instanceof Byte[])
1597 Byte value = ((Byte[])obj)[0];
1598 if (value==0) return false;
1599 else if (value==1) return true;
1600 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1602 if (obj instanceof byte[])
1604 byte value = ((byte[])obj)[0];
1605 if (value==0) return false;
1606 else if (value==1) return true;
1607 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1609 if (obj instanceof String[])
1611 String[] array = (String[]) obj;
1612 String value = array[0];
1613 if (value.equals("true") || value.equals("1")) return true;
1614 if (value.equals("false") || value.equals("0")) return false;
1615 throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1617 if (obj instanceof Double[])
1619 double value = ((Double[])obj)[0];
1620 if (value==0) return false;
1621 else if (value==1) return true;
1622 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1624 if (obj instanceof double[])
1626 double value = ((double[])obj)[0];
1627 if (value==0) return false;
1628 else if (value==1) return true;
1629 else throw new TypeCastException("Cannot convert "+value+" to boolean value - 0 or 1 is expected");
1631 throw new TypeCastException(obj.getClass());
1635 @SuppressWarnings("unchecked")
1636 public static <T> T adaptToClass(Object obj, Class<T> clazz)
1637 throws TypeCastException
1639 if (clazz.equals(double[].class))
1640 return (T) toDoubleArray(obj);
1641 if (clazz.equals(Double.class))
1642 return (T) new Double(toDoubleScalar(obj));
1644 if (clazz.equals(float[].class))
1645 return (T) toFloatArray(obj);
1646 if (clazz.equals(Float.class))
1647 return (T) new Float(toFloatScalar(obj));
1649 if (clazz.equals(long[].class))
1650 return (T) toLongArray(obj);
1651 if (clazz.equals(Long.class))
1652 return (T) new Long(toLongScalar(obj));
1654 if (clazz.equals(int[].class))
1655 return (T) toIntegerArray(obj);
1656 if (clazz.equals(Integer.class))
1657 return (T) new Integer(toIntegerScalar(obj));
1659 if (clazz.equals(boolean[].class))
1660 return (T) toBooleanArray(obj);
1661 if (clazz.equals(Boolean.class))
1662 return (T) new Long(toLongScalar(obj));
1664 throw new TypeCastException("Unsupported conversion type from "+obj.getClass()+" to "+clazz);
1669 public static String toString(Object obj) {
1670 if (obj instanceof Object[]) {
1671 return Arrays.toString((Object[])obj);
1673 if (obj instanceof double[]) return Arrays.toString((double[])obj);
1674 if (obj instanceof boolean[]) return Arrays.toString((boolean[])obj);
1675 if (obj instanceof byte[]) return Arrays.toString((byte[])obj);
1676 if (obj instanceof char[]) return Arrays.toString((char[])obj);
1677 if (obj instanceof float[]) return Arrays.toString((float[])obj);
1678 if (obj instanceof int[]) return Arrays.toString((int[])obj);
1679 if (obj instanceof long[]) return Arrays.toString((long[])obj);
1680 if (obj instanceof short[]) return Arrays.toString((short[])obj);
1681 return obj.toString();