]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.datastructures;
13
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;
21
22 /**
23  * Object type casting tools.
24  * 
25  * @author Toni Kalajainen
26  * @author Hannu Niemistö
27  */
28 public class ValueUtils {
29
30     /**
31      * Convert double value to a number class.
32      * 
33      * @param value
34      * @param numberClass
35      * @return
36      * @throws ClassCastException
37      */
38     public static Number doubleToNumberClass(double value, Class<? extends Number> numberClass)
39     throws ClassCastException
40     {
41         if (numberClass==Integer.class)
42             return (int)value;
43         if (numberClass==Byte.class)
44             return (byte)value;
45         if (numberClass==Float.class)
46             return (float)value;
47         if (numberClass==Short.class)
48             return (short)value;
49         if (numberClass==Long.class)
50             return (long)value;
51         if (numberClass==Double.class)
52             return (double)value;
53         throw new ClassCastException("Cannot convert to "+numberClass.getName());
54     }
55
56     /**
57      * Possible object types:
58      * 
59      */
60
61     static final Boolean True = new Boolean(true);
62     static final Boolean False = new Boolean(false);
63
64     public static void serialize(DataOutput stream, Object obj) throws IOException {
65         try {
66             Class<?> clazz = obj.getClass();
67             if(clazz.isArray()) {
68                 if(obj instanceof double[]) {
69                     double[] array = (double[])obj;
70                     stream.writeByte(16);
71                     stream.writeInt(array.length);
72                     for(double v : array)
73                         stream.writeDouble(v);
74                 }
75                 else if(obj instanceof float[]) {
76                     float[] array = (float[])obj;
77                     stream.writeByte(15);
78                     stream.writeInt(array.length);
79                     for(float v : array)
80                         stream.writeFloat(v);
81                 }
82                 else if(obj instanceof int[]) {
83                     int[] array = (int[])obj;
84                     stream.writeByte(13);
85                     stream.writeInt(array.length);
86                     for(int v : array)
87                         stream.writeInt(v);
88                 }
89                 else if(obj instanceof String[]) {
90                     String[] array = (String[])obj;
91                     stream.writeByte(17);
92                     stream.writeInt(array.length);
93                     for(String v : array)
94                         stream.writeUTF(v);
95                 }
96                 else if(obj instanceof boolean[]) {
97                     boolean[] array = (boolean[])obj;
98                     stream.writeByte(11);
99                     stream.writeInt(array.length);
100                     for(boolean v : array)
101                         stream.writeByte(v ? (byte)1 : (byte)0);
102                 }
103                 else if(obj instanceof byte[]) {
104                     byte[] array = (byte[])obj;
105                     stream.writeByte(12);
106                     stream.writeInt(array.length);
107                     for(byte v : array)
108                         stream.writeByte(v);
109                 }
110                 else if(obj instanceof long[]) {
111                     long[] array = (long[])obj;
112                     stream.writeByte(14);
113                     stream.writeInt(array.length);
114                     for(long v : array)
115                         stream.writeLong(v);
116                 }
117                 else {
118                     System.out.println("ValueUtils.serialize failed (invalid type).");
119                     throw new InvalidParameterException();
120                 }
121             }
122             else {
123                 if(clazz == Double.class) {
124                     stream.writeByte(6);
125                     stream.writeDouble((Double)obj);
126                 }
127                 else if(clazz == Float.class) {
128                     stream.writeByte(5);
129                     stream.writeDouble((Float)obj);
130                 }
131                 else if(clazz == Integer.class) {
132                     stream.writeByte(3);
133                     stream.writeInt((Integer)obj);
134                 }
135                 else if(clazz == String.class) {
136                     stream.writeByte(7);
137                     stream.writeUTF((String)obj);
138                 }
139                 else if(clazz == Boolean.class) {
140                     stream.writeByte((Boolean)obj ? 1 : 0);
141                 }
142                 else if(clazz == Byte.class) {
143                     stream.writeByte(2);
144                     stream.writeByte((Byte)obj);
145                 }
146                 else if(clazz == Long.class) {
147                     stream.writeByte(4);
148                     stream.writeLong((Long)obj);
149                 }
150                 else {
151                     System.out.println("ValueUtils.serialize failed (invalid type).");
152                     throw new InvalidParameterException();
153                 }
154             }
155         } catch(IOException e) {
156             System.out.println("ValueUtils.serialize failed (write failure).");
157             e.printStackTrace();
158             throw e;
159         }
160     }
161
162     public static Object deserialize(DataInput stream) throws IOException {
163         try {
164             byte typeCode = stream.readByte();
165             switch(typeCode) {
166                 case 0: return False;
167                 case 1: return True;
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();
174
175                 case 11: {
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;
180                     return value;
181                 }
182                 case 12: {
183                     int length = stream.readInt();
184                     byte[] value = new byte[length];
185                     for(int i=0;i<length;++i)
186                         value[i] = stream.readByte();
187                     return value;
188                 }
189                 case 13: {
190                     int length = stream.readInt();
191                     int[] value = new int[length];
192                     for(int i=0;i<length;++i)
193                         value[i] = stream.readInt();
194                     return value;
195                 }
196                 case 14: {
197                     int length = stream.readInt();
198                     long[] value = new long[length];
199                     for(int i=0;i<length;++i)
200                         value[i] = stream.readLong();
201                     return value;
202                 }
203                 case 15: {
204                     int length = stream.readInt();
205                     float[] value = new float[length];
206                     for(int i=0;i<length;++i)
207                         value[i] = stream.readFloat();
208                     return value;
209                 }
210                 case 16: {
211                     int length = stream.readInt();
212                     double[] value = new double[length];
213                     for(int i=0;i<length;++i)
214                         value[i] = stream.readDouble();
215                     return value;
216                 }
217                 case 17: {
218                     int length = stream.readInt();
219                     String[] value = new String[length];
220                     for(int i=0;i<length;++i)
221                         value[i] = stream.readUTF();
222                     return value;
223                 }
224
225             }
226
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).");
231             e.printStackTrace();
232             throw e;
233         }
234     }
235
236
237
238
239
240
241     /**
242      * is object an array type
243      * @param obj the object
244      * @return true if it is object array (Integer[], Short[], ect)
245      */
246     public static boolean isArray(Object obj) {
247         return obj.getClass().isArray();
248     }
249
250     /**
251      * Get the array length
252      * @param obj object
253      * @return the length
254      */
255     public static int getArrayLength(Object obj) {
256         return Array.getLength(obj);
257     }
258
259     /**
260      * Type casts an array to double.
261      * 
262      * String types are attempted to be converted. If string
263      * doesn't represent numeric value, 0 will be returned
264      * 
265      * boolean types return 0/1, for false/true respectively
266      * 
267      * @return type casted value
268      */
269     public static double[] toDoubleArray(Object obj)
270     throws TypeCastException
271     {
272         if (!isArray(obj)) {
273             double scalar = toDoubleScalar(obj);
274             return new double[] {scalar};
275         }
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[])
280         {
281             Calendar[] array = (Calendar[]) obj;
282             for (int i=0; i<len; i++)
283                 result[i] = array[i].getTimeInMillis();
284             return result;
285         }
286         if (obj instanceof Integer[])
287         {
288             Integer[] array = (Integer[]) obj;
289             for (int i=0; i<len; i++)
290                 result[i] = array[i].doubleValue();
291             return result;
292         }
293         if (obj instanceof int[])
294         {
295             int[] array = (int[]) obj;
296             for (int i=0; i<len; i++)
297                 result[i] = array[i];
298             return result;
299         }
300         if (obj instanceof Double[])
301         {
302             Double[] array = (Double[]) obj;
303             for (int i=0; i<len; i++)
304                 result[i] = array[i].doubleValue();
305             return result;
306         }
307         if (obj instanceof double[])
308         {
309             return (double[])obj;
310         }
311         if (obj instanceof Float[])
312         {
313             Float[] array = (Float[]) obj;
314             for (int i=0; i<len; i++)
315                 result[i] = array[i].doubleValue();
316             return result;
317         }
318         if (obj instanceof float[])
319         {
320             float[] array = (float[]) obj;
321             for (int i=0; i<len; i++)
322                 result[i] = array[i];
323             return result;
324         }
325         if (obj instanceof Long[])
326         {
327             Long[] array = (Long[]) obj;
328             for (int i=0; i<len; i++)
329                 result[i] = array[i].doubleValue();
330             return result;
331         }
332         if (obj instanceof long[])
333         {
334             long[] array = (long[]) obj;
335             for (int i=0; i<len; i++)
336                 result[i] = array[i];
337             return result;
338         }
339         if (obj instanceof Short[])
340         {
341             Short[] array = (Short[]) obj;
342             for (int i=0; i<len; i++)
343                 result[i] = array[i].doubleValue();
344             return result;
345         }
346         if (obj instanceof short[])
347         {
348             short[] array = (short[]) obj;
349             for (int i=0; i<len; i++)
350                 result[i] = array[i];
351             return result;
352         }
353         if (obj instanceof Byte[])
354         {
355             Byte[] array = (Byte[]) obj;
356             for (int i=0; i<len; i++)
357                 result[i] = array[i].doubleValue();
358             return result;
359         }
360         if (obj instanceof byte[])
361         {
362             byte[] array = (byte[]) obj;
363             for (int i=0; i<len; i++)
364                 result[i] = array[i];
365             return result;
366         }
367         if (obj instanceof String[])
368         {
369             String[] array = (String[]) obj;
370             for (int i=0; i<len; i++)
371                 try {
372                     result[i] = new Double(array[i]);
373                 } catch (NumberFormatException e) {
374                     throw new TypeCastException(obj.getClass());
375                 }
376                 return result;
377         }
378         if (obj instanceof Boolean[])
379         {
380             Boolean[] array = (Boolean[]) obj;
381             for (int i=0; i<len; i++)
382                 result[i] = (array[i].booleanValue()?1:0);
383             return result;
384         }
385         if (obj instanceof boolean[])
386         {
387             boolean[] array = (boolean[]) obj;
388             for (int i=0; i<len; i++)
389                 result[i] = (array[i]?1:0);
390             return result;
391         }
392         throw new TypeCastException(obj.getClass());
393     }
394
395     /**
396      * Type casts an object to long.
397      * 
398      * String types are attempted to be converted. If string
399      * doesn't represent numeric value, 0 will be returned
400      * 
401      * boolean types return 0/1, for false/true respectively
402      * 
403      * @return type casted value
404      */
405     public static double toDoubleScalar(Object obj)
406     throws TypeCastException
407     {
408         if (!isArray(obj)) {
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)
414             {
415                 String str = (String) obj;
416                 try {
417                     Double d = new Double(str);
418                     return d.doubleValue();
419                 } catch (NumberFormatException e) {
420                     throw new TypeCastException(obj.getClass());
421                 }
422             }
423             if (obj instanceof Boolean) {
424                 return (((Boolean)obj).booleanValue())?1:0;
425             }
426             throw new TypeCastException(obj.getClass());
427         }
428         int len = getArrayLength(obj);
429         if (len!=1)
430             throw new TypeCastException("Expected length of array is 1");
431
432         if (obj instanceof double[])
433         {
434             return ((double[])obj)[0];
435         }
436         if (obj instanceof int[])
437         {
438             int[] array = (int[]) obj;
439             return array[0];
440         }
441         if (obj instanceof Calendar[])
442         {
443             Calendar[] array = (Calendar[]) obj;
444             return array[0].getTimeInMillis();
445         }
446         if (obj instanceof Integer[])
447         {
448             Integer[] array = (Integer[]) obj;
449             return array[0].doubleValue();
450         }
451         if (obj instanceof Double[])
452         {
453             Double[] array = (Double[]) obj;
454             return array[0].doubleValue();
455         }
456         if (obj instanceof Float[])
457         {
458             Float[] array = (Float[]) obj;
459             return array[0].doubleValue();
460         }
461         if (obj instanceof float[])
462         {
463             float[] array = (float[]) obj;
464             return array[0];
465         }
466         if (obj instanceof Long[])
467         {
468             Long[] array = (Long[]) obj;
469             return array[0].doubleValue();
470         }
471         if (obj instanceof long[])
472         {
473             long[] array = (long[]) obj;
474             return array[0];
475         }
476         if (obj instanceof Short[])
477         {
478             Short[] array = (Short[]) obj;
479             return array[0].doubleValue();
480         }
481         if (obj instanceof short[])
482         {
483             short[] array = (short[]) obj;
484             return array[0];
485         }
486         if (obj instanceof Byte[])
487         {
488             Byte[] array = (Byte[]) obj;
489             return array[0].doubleValue();
490         }
491         if (obj instanceof byte[])
492         {
493             byte[] array = (byte[]) obj;
494             return array[0];
495         }
496         if (obj instanceof String[])
497         {
498             String[] array = (String[]) obj;
499             try {
500                 return new Double(array[0]);
501             } catch (NumberFormatException e) {
502                 throw new TypeCastException(obj.getClass());
503             }
504         }
505         if (obj instanceof Boolean[])
506         {
507             Boolean[] array = (Boolean[]) obj;
508             return (array[0].booleanValue()?1:0);
509         }
510         if (obj instanceof boolean[])
511         {
512             boolean[] array = (boolean[]) obj;
513             return (array[0]?1:0);
514         }
515         throw new TypeCastException(obj.getClass());
516     }
517
518
519     /**
520      * Type casts an array to float.
521      * 
522      * String types are attempted to be converted. If string
523      * doesn't represent numeric value, 0 will be returned
524      * 
525      * boolean types return 0/1, for false/true respectively
526      * 
527      * @return type casted value
528      */
529     public static float[] toFloatArray(Object obj)
530     throws TypeCastException
531     {
532         if (!isArray(obj)) {
533             float scalar = toFloatScalar(obj);
534             return new float[] {scalar};
535         }
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[])
540         {
541             Calendar[] array = (Calendar[]) obj;
542             for (int i=0; i<len; i++)
543                 result[i] = array[i].getTimeInMillis();
544             return result;
545         }
546         if (obj instanceof Integer[])
547         {
548             Integer[] array = (Integer[]) obj;
549             for (int i=0; i<len; i++)
550                 result[i] = array[i].floatValue();
551             return result;
552         }
553         if (obj instanceof int[])
554         {
555             int[] array = (int[]) obj;
556             for (int i=0; i<len; i++)
557                 result[i] = array[i];
558             return result;
559         }
560         if (obj instanceof Float[])
561         {
562             Float[] array = (Float[]) obj;
563             for (int i=0; i<len; i++)
564                 result[i] = array[i].floatValue();
565             return result;
566         }
567         if (obj instanceof float[])
568         {
569             return (float[])obj;
570         }
571         if (obj instanceof Double[])
572         {
573             Double[] array = (Double[]) obj;
574             for (int i=0; i<len; i++)
575                 result[i] = array[i].floatValue();
576             return result;
577         }
578         if (obj instanceof double[])
579         {
580             double[] array = (double[]) obj;
581             for (int i=0; i<len; i++)
582                 result[i] = (float)array[i];
583             return result;
584         }
585         if (obj instanceof Long[])
586         {
587             Long[] array = (Long[]) obj;
588             for (int i=0; i<len; i++)
589                 result[i] = array[i].floatValue();
590             return result;
591         }
592         if (obj instanceof long[])
593         {
594             long[] array = (long[]) obj;
595             for (int i=0; i<len; i++)
596                 result[i] = array[i];
597             return result;
598         }
599         if (obj instanceof Short[])
600         {
601             Short[] array = (Short[]) obj;
602             for (int i=0; i<len; i++)
603                 result[i] = array[i].floatValue();
604             return result;
605         }
606         if (obj instanceof short[])
607         {
608             short[] array = (short[]) obj;
609             for (int i=0; i<len; i++)
610                 result[i] = array[i];
611             return result;
612         }
613         if (obj instanceof Byte[])
614         {
615             Byte[] array = (Byte[]) obj;
616             for (int i=0; i<len; i++)
617                 result[i] = array[i].floatValue();
618             return result;
619         }
620         if (obj instanceof byte[])
621         {
622             byte[] array = (byte[]) obj;
623             for (int i=0; i<len; i++)
624                 result[i] = array[i];
625             return result;
626         }
627         if (obj instanceof String[])
628         {
629             String[] array = (String[]) obj;
630             for (int i=0; i<len; i++)
631                 try {
632                     result[i] = new Float(array[i]);
633                 } catch (NumberFormatException e) {
634                     throw new TypeCastException(obj.getClass());
635                 }
636                 return result;
637         }
638         if (obj instanceof Boolean[])
639         {
640             Boolean[] array = (Boolean[]) obj;
641             for (int i=0; i<len; i++)
642                 result[i] = (array[i].booleanValue()?1:0);
643             return result;
644         }
645         if (obj instanceof boolean[])
646         {
647             boolean[] array = (boolean[]) obj;
648             for (int i=0; i<len; i++)
649                 result[i] = (array[i]?1:0);
650             return result;
651         }
652         throw new TypeCastException(obj.getClass());
653     }
654
655     /**
656      * Type casts an object to float.
657      * 
658      * String types are attempted to be converted. If string
659      * doesn't represent numeric value, 0 will be returned
660      * 
661      * boolean types return 0/1, for false/true respectively
662      * 
663      * @return type casted value
664      */
665     public static float toFloatScalar(Object obj)
666     throws TypeCastException
667     {
668         if (!isArray(obj)) {
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)
674             {
675                 String str = (String) obj;
676                 try {
677                     Float d = new Float(str);
678                     return d.floatValue();
679                 } catch (NumberFormatException e) {
680                     throw new TypeCastException(obj.getClass());
681                 }
682             }
683             if (obj instanceof Boolean) {
684                 return (((Boolean)obj).booleanValue())?1:0;
685             }
686             throw new TypeCastException(obj.getClass());
687         }
688         int len = getArrayLength(obj);
689         if (len!=1)
690             throw new TypeCastException("Expected length of array is 1");
691
692         if (obj instanceof float[])
693         {
694             return ((float[])obj)[0];
695         }
696         if (obj instanceof int[])
697         {
698             int[] array = (int[]) obj;
699             return array[0];
700         }
701         if (obj instanceof Calendar[])
702         {
703             Calendar[] array = (Calendar[]) obj;
704             return array[0].getTimeInMillis();
705         }
706         if (obj instanceof Integer[])
707         {
708             Integer[] array = (Integer[]) obj;
709             return array[0].floatValue();
710         }
711         if (obj instanceof Float[])
712         {
713             Float[] array = (Float[]) obj;
714             return array[0].floatValue();
715         }
716         if (obj instanceof Float[])
717         {
718             Float[] array = (Float[]) obj;
719             return array[0].floatValue();
720         }
721         if (obj instanceof double[])
722         {
723             double[] array = (double[]) obj;
724             return (float)array[0];
725         }
726         if (obj instanceof Long[])
727         {
728             Long[] array = (Long[]) obj;
729             return array[0].floatValue();
730         }
731         if (obj instanceof long[])
732         {
733             long[] array = (long[]) obj;
734             return array[0];
735         }
736         if (obj instanceof Short[])
737         {
738             Short[] array = (Short[]) obj;
739             return array[0].floatValue();
740         }
741         if (obj instanceof short[])
742         {
743             short[] array = (short[]) obj;
744             return array[0];
745         }
746         if (obj instanceof Byte[])
747         {
748             Byte[] array = (Byte[]) obj;
749             return array[0].floatValue();
750         }
751         if (obj instanceof byte[])
752         {
753             byte[] array = (byte[]) obj;
754             return array[0];
755         }
756         if (obj instanceof String[])
757         {
758             String[] array = (String[]) obj;
759             try {
760                 return new Float(array[0]);
761             } catch (NumberFormatException e) {
762                 throw new TypeCastException(obj.getClass());
763             }
764         }
765         if (obj instanceof Boolean[])
766         {
767             Boolean[] array = (Boolean[]) obj;
768             return (array[0].booleanValue()?1:0);
769         }
770         if (obj instanceof boolean[])
771         {
772             boolean[] array = (boolean[]) obj;
773             return (array[0]?1:0);
774         }
775         throw new TypeCastException(obj.getClass());
776     }
777
778
779     /**
780      * Type casts an object to long.
781      * 
782      * String types are attempted to be converted. If string
783      * doesn't represent numeric value, 0 will be returned
784      * 
785      * boolean types return 0/1, for false/true respectively
786      * 
787      * @return type casted value
788      */
789     public static long toLongScalar(Object obj)
790     throws TypeCastException
791     {
792         if (!isArray(obj)) {
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)
798             {
799                 String str = (String) obj;
800                 try {
801                     Long d = new Long(str);
802                     return d.longValue();
803                 } catch (NumberFormatException e) {
804                     throw new TypeCastException(obj.getClass());
805                 }
806             }
807             if (obj instanceof Boolean) {
808                 return (((Boolean)obj).booleanValue())?1:0;
809             }
810             throw new TypeCastException(obj.getClass());
811         }
812         int len = getArrayLength(obj);
813         if (len!=1)
814             throw new TypeCastException("Expected length of array is 1");
815
816         if (obj instanceof double[])
817         {
818             return ((long[])obj)[0];
819         }
820         if (obj instanceof int[])
821         {
822             int[] array = (int[]) obj;
823             return array[0];
824         }
825         if (obj instanceof Calendar[])
826         {
827             Calendar[] array = (Calendar[]) obj;
828             return array[0].getTimeInMillis();
829         }
830         if (obj instanceof Integer[])
831         {
832             Integer[] array = (Integer[]) obj;
833             return array[0].longValue();
834         }
835         if (obj instanceof Double[])
836         {
837             Double[] array = (Double[]) obj;
838             return array[0].longValue();
839         }
840         if (obj instanceof Float[])
841         {
842             Float[] array = (Float[]) obj;
843             return array[0].longValue();
844         }
845         if (obj instanceof float[])
846         {
847             float[] array = (float[]) obj;
848             return (long)array[0];
849         }
850         if (obj instanceof Long[])
851         {
852             Long[] array = (Long[]) obj;
853             return array[0].longValue();
854         }
855         if (obj instanceof long[])
856         {
857             long[] array = (long[]) obj;
858             return array[0];
859         }
860         if (obj instanceof Short[])
861         {
862             Short[] array = (Short[]) obj;
863             return array[0].longValue();
864         }
865         if (obj instanceof short[])
866         {
867             short[] array = (short[]) obj;
868             return array[0];
869         }
870         if (obj instanceof Byte[])
871         {
872             Byte[] array = (Byte[]) obj;
873             return array[0].longValue();
874         }
875         if (obj instanceof byte[])
876         {
877             byte[] array = (byte[]) obj;
878             return array[0];
879         }
880         if (obj instanceof String[])
881         {
882             String[] array = (String[]) obj;
883             try {
884                 return new Long(array[0]);
885             } catch (NumberFormatException e) {
886                 throw new TypeCastException(obj.getClass());
887             }
888         }
889         if (obj instanceof Boolean[])
890         {
891             Boolean[] array = (Boolean[]) obj;
892             return (array[0].booleanValue()?1:0);
893         }
894         if (obj instanceof boolean[])
895         {
896             boolean[] array = (boolean[]) obj;
897             return (array[0]?1:0);
898         }
899         throw new TypeCastException(obj.getClass());
900     }
901
902     /**
903      * Type casts an array to long.
904      * 
905      * String types are attempted to be converted. If string
906      * doesn't represent numeric value, 0 will be returned
907      * 
908      * boolean types return 0/1, for false/true respectively
909      * 
910      * @return type casted value
911      */
912     public static long[] toLongArray(Object obj)
913     throws TypeCastException
914     {
915         if (!isArray(obj)) {
916             long scalar = toLongScalar(obj);
917             return new long[] {scalar};
918         }
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[])
923         {
924             Calendar[] array = (Calendar[]) obj;
925             for (int i=0; i<len; i++)
926                 result[i] = array[i].getTimeInMillis();
927             return result;
928         }
929         if (obj instanceof Integer[])
930         {
931             Integer[] array = (Integer[]) obj;
932             for (int i=0; i<len; i++)
933                 result[i] = array[i].longValue();
934             return result;
935         }
936         if (obj instanceof int[])
937         {
938             int[] array = (int[]) obj;
939             for (int i=0; i<len; i++)
940                 result[i] = array[i];
941             return result;
942         }
943         if (obj instanceof Long[])
944         {
945             Long[] array = (Long[]) obj;
946             for (int i=0; i<len; i++)
947                 result[i] = array[i].longValue();
948             return result;
949         }
950         if (obj instanceof long[])
951         {
952             return (long[])obj;
953         }
954         if (obj instanceof Float[])
955         {
956             Float[] array = (Float[]) obj;
957             for (int i=0; i<len; i++)
958                 result[i] = array[i].longValue();
959             return result;
960         }
961         if (obj instanceof float[])
962         {
963             float[] array = (float[]) obj;
964             for (int i=0; i<len; i++)
965                 result[i] = (long)array[i];
966             return result;
967         }
968         if (obj instanceof Double[])
969         {
970             Double[] array = (Double[]) obj;
971             for (int i=0; i<len; i++)
972                 result[i] = array[i].longValue();
973             return result;
974         }
975         if (obj instanceof double[])
976         {
977             double[] array = (double[]) obj;
978             for (int i=0; i<len; i++)
979                 result[i] = (long)array[i];
980             return result;
981         }
982         if (obj instanceof Short[])
983         {
984             Short[] array = (Short[]) obj;
985             for (int i=0; i<len; i++)
986                 result[i] = array[i].longValue();
987             return result;
988         }
989         if (obj instanceof short[])
990         {
991             short[] array = (short[]) obj;
992             for (int i=0; i<len; i++)
993                 result[i] = array[i];
994             return result;
995         }
996         if (obj instanceof Byte[])
997         {
998             Byte[] array = (Byte[]) obj;
999             for (int i=0; i<len; i++)
1000                 result[i] = array[i].longValue();
1001             return result;
1002         }
1003         if (obj instanceof byte[])
1004         {
1005             byte[] array = (byte[]) obj;
1006             for (int i=0; i<len; i++)
1007                 result[i] = array[i];
1008             return result;
1009         }
1010         if (obj instanceof String[])
1011         {
1012             String[] array = (String[]) obj;
1013             for (int i=0; i<len; i++)
1014                 try {
1015                     result[i] = new Long(array[i]);
1016                 } catch (NumberFormatException e) {
1017                     throw new TypeCastException(obj.getClass());
1018                 }
1019                 return result;
1020         }
1021         if (obj instanceof Boolean[])
1022         {
1023             Boolean[] array = (Boolean[]) obj;
1024             for (int i=0; i<len; i++)
1025                 result[i] = (array[i].booleanValue()?1:0);
1026             return result;
1027         }
1028         if (obj instanceof boolean[])
1029         {
1030             boolean[] array = (boolean[]) obj;
1031             for (int i=0; i<len; i++)
1032                 result[i] = (array[i]?1:0);
1033             return result;
1034         }
1035         throw new TypeCastException(obj.getClass());
1036     }
1037
1038
1039
1040
1041
1042
1043     /**
1044      * Type casts an object to int.
1045      * 
1046      * String types are attempted to be converted. If string
1047      * doesn't represent numeric value, 0 will be returned
1048      * 
1049      * boolean types return 0/1, for false/true respectively
1050      * 
1051      * @return type casted value
1052      */
1053     public static int toIntegerScalar(Object obj)
1054     throws TypeCastException
1055     {
1056         if (!isArray(obj)) {
1057             if (obj instanceof Number)
1058                 return ((Number)obj).intValue();
1059             if (obj instanceof String)
1060             {
1061                 String str = (String) obj;
1062                 try {
1063                     Integer d = new Integer(str);
1064                     return d.intValue();
1065                 } catch (NumberFormatException e) {
1066                     throw new TypeCastException(obj.getClass());
1067                 }
1068             }
1069             if (obj instanceof Boolean) {
1070                 return (((Boolean)obj).booleanValue())?1:0;
1071             }
1072             throw new TypeCastException(obj.getClass());
1073         }
1074         int len = getArrayLength(obj);
1075         if (len!=1)
1076             throw new TypeCastException("Expected length of array is 1");
1077
1078         if (obj instanceof double[])
1079         {
1080             return ((int[])obj)[0];
1081         }
1082         if (obj instanceof int[])
1083         {
1084             int[] array = (int[]) obj;
1085             return array[0];
1086         }
1087         if (obj instanceof Integer[])
1088         {
1089             Integer[] array = (Integer[]) obj;
1090             return array[0].intValue();
1091         }
1092         if (obj instanceof Double[])
1093         {
1094             Double[] array = (Double[]) obj;
1095             return array[0].intValue();
1096         }
1097         if (obj instanceof Float[])
1098         {
1099             Float[] array = (Float[]) obj;
1100             return array[0].intValue();
1101         }
1102         if (obj instanceof float[])
1103         {
1104             float[] array = (float[]) obj;
1105             return (int)array[0];
1106         }
1107         if (obj instanceof Long[])
1108         {
1109             Long[] array = (Long[]) obj;
1110             return array[0].intValue();
1111         }
1112         if (obj instanceof long[])
1113         {
1114             long[] array = (long[]) obj;
1115             return (int)array[0];
1116         }
1117         if (obj instanceof Short[])
1118         {
1119             Short[] array = (Short[]) obj;
1120             return array[0].intValue();
1121         }
1122         if (obj instanceof short[])
1123         {
1124             short[] array = (short[]) obj;
1125             return array[0];
1126         }
1127         if (obj instanceof Byte[])
1128         {
1129             Byte[] array = (Byte[]) obj;
1130             return array[0].intValue();
1131         }
1132         if (obj instanceof byte[])
1133         {
1134             byte[] array = (byte[]) obj;
1135             return array[0];
1136         }
1137         if (obj instanceof String[])
1138         {
1139             String[] array = (String[]) obj;
1140             try {
1141                 return new Integer(array[0]);
1142             } catch (NumberFormatException e) {
1143                 throw new TypeCastException(obj.getClass());
1144             }
1145         }
1146         if (obj instanceof Boolean[])
1147         {
1148             Boolean[] array = (Boolean[]) obj;
1149             return (array[0].booleanValue()?1:0);
1150         }
1151         if (obj instanceof boolean[])
1152         {
1153             boolean[] array = (boolean[]) obj;
1154             return (array[0]?1:0);
1155         }
1156         throw new TypeCastException(obj.getClass());
1157     }
1158
1159     /**
1160      * Type casts an array to int.
1161      * 
1162      * String types are attempted to be converted. If string
1163      * doesn't represent numeric value, 0 will be returned
1164      * 
1165      * boolean types return 0/1, for false/true respectively
1166      * 
1167      * @return type casted value
1168      */
1169     public static int[] toIntegerArray(Object obj)
1170     throws TypeCastException
1171     {
1172         if (!isArray(obj)) {
1173             int scalar = toIntegerScalar(obj);
1174             return new int[] {scalar};
1175         }
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[])
1180         {
1181             Integer[] array = (Integer[]) obj;
1182             for (int i=0; i<len; i++)
1183                 result[i] = array[i].intValue();
1184             return result;
1185         }
1186         if (obj instanceof long[])
1187         {
1188             long[] array = (long[]) obj;
1189             for (int i=0; i<len; i++)
1190                 result[i] = (int)array[i];
1191             return result;
1192         }
1193         if (obj instanceof Integer[])
1194         {
1195             Integer[] array = (Integer[]) obj;
1196             for (int i=0; i<len; i++)
1197                 result[i] = array[i].intValue();
1198             return result;
1199         }
1200         if (obj instanceof int[])
1201         {
1202             return (int[])obj;
1203         }
1204         if (obj instanceof Float[])
1205         {
1206             Float[] array = (Float[]) obj;
1207             for (int i=0; i<len; i++)
1208                 result[i] = array[i].intValue();
1209             return result;
1210         }
1211         if (obj instanceof float[])
1212         {
1213             float[] array = (float[]) obj;
1214             for (int i=0; i<len; i++)
1215                 result[i] = (int)array[i];
1216             return result;
1217         }
1218         if (obj instanceof Double[])
1219         {
1220             Double[] array = (Double[]) obj;
1221             for (int i=0; i<len; i++)
1222                 result[i] = array[i].intValue();
1223             return result;
1224         }
1225         if (obj instanceof double[])
1226         {
1227             double[] array = (double[]) obj;
1228             for (int i=0; i<len; i++)
1229                 result[i] = (int)array[i];
1230             return result;
1231         }
1232         if (obj instanceof Short[])
1233         {
1234             Short[] array = (Short[]) obj;
1235             for (int i=0; i<len; i++)
1236                 result[i] = array[i].intValue();
1237             return result;
1238         }
1239         if (obj instanceof short[])
1240         {
1241             short[] array = (short[]) obj;
1242             for (int i=0; i<len; i++)
1243                 result[i] = array[i];
1244             return result;
1245         }
1246         if (obj instanceof Byte[])
1247         {
1248             Byte[] array = (Byte[]) obj;
1249             for (int i=0; i<len; i++)
1250                 result[i] = array[i].intValue();
1251             return result;
1252         }
1253         if (obj instanceof byte[])
1254         {
1255             byte[] array = (byte[]) obj;
1256             for (int i=0; i<len; i++)
1257                 result[i] = array[i];
1258             return result;
1259         }
1260         if (obj instanceof String[])
1261         {
1262             String[] array = (String[]) obj;
1263             for (int i=0; i<len; i++)
1264                 try {
1265                     result[i] = new Integer(array[i]);
1266                 } catch (NumberFormatException e) {
1267                     throw new TypeCastException(obj.getClass());
1268                 }
1269                 return result;
1270         }
1271         if (obj instanceof Boolean[])
1272         {
1273             Boolean[] array = (Boolean[]) obj;
1274             for (int i=0; i<len; i++)
1275                 result[i] = (array[i].booleanValue()?1:0);
1276             return result;
1277         }
1278         if (obj instanceof boolean[])
1279         {
1280             boolean[] array = (boolean[]) obj;
1281             for (int i=0; i<len; i++)
1282                 result[i] = (array[i]?1:0);
1283             return result;
1284         }
1285         throw new TypeCastException(obj.getClass());
1286     }
1287
1288
1289
1290
1291     /**
1292      * Type casts an array to boolean.
1293      *
1294      * 0 is converted to false, 1 is converted to true, others throw TypeCastException
1295      * 
1296      * @return type casted value
1297      * @throws TypeCastException
1298      */
1299     public static boolean[] toBooleanArray(Object obj)
1300     throws TypeCastException
1301     {
1302         if (!isArray(obj)) {
1303             boolean scalar = toBooleanScalar(obj);
1304             return new boolean[] {scalar};
1305         }
1306         int len = getArrayLength(obj);
1307         boolean[] result = new boolean[len];
1308         if (obj instanceof Integer[])
1309         {
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");
1316             }
1317             return result;
1318         }
1319         if (obj instanceof int[])
1320         {
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");
1327             }
1328             return result;
1329         }
1330         if (obj instanceof Boolean[])
1331         {
1332             Boolean[] array = (Boolean[]) obj;
1333             for (int i=0; i<len; i++)
1334                 result[i] = array[i].booleanValue();
1335             return result;
1336         }
1337         if (obj instanceof boolean[])
1338         {
1339             return (boolean[])obj;
1340         }
1341         if (obj instanceof Float[])
1342         {
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");
1349             }
1350             return result;
1351         }
1352         if (obj instanceof float[])
1353         {
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");
1360             }
1361             return result;
1362         }
1363         if (obj instanceof Long[])
1364         {
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");
1371             }
1372             return result;
1373         }
1374         if (obj instanceof long[])
1375         {
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");
1382             }
1383             return result;
1384         }
1385         if (obj instanceof Short[])
1386         {
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");
1393             }
1394             return result;
1395         }
1396         if (obj instanceof short[])
1397         {
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");
1404             }
1405             return result;
1406         }
1407         if (obj instanceof Byte[])
1408         {
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");
1415             }
1416             return result;
1417         }
1418         if (obj instanceof byte[])
1419         {
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");
1426             }
1427             return result;
1428         }
1429         if (obj instanceof String[])
1430         {
1431             String[] array = (String[]) obj;
1432             for (int i=0; i<len; i++)
1433                 try {
1434                     result[i] = new Boolean(array[i]);
1435                 } catch (NumberFormatException e) {
1436                     throw new TypeCastException(obj.getClass());
1437                 }
1438                 return result;
1439         }
1440         if (obj instanceof Double[])
1441         {
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");
1448             }
1449             return result;
1450         }
1451         if (obj instanceof double[])
1452         {
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");
1459             }
1460             return result;
1461         }
1462         throw new TypeCastException(obj.getClass());
1463     }
1464
1465     /**
1466      * Type casts an object to long.
1467      * 
1468      * String types are attempted to be converted. If string
1469      * doesn't represent numeric value, 0 will be returned
1470      * 
1471      * boolean types return 0/1, for false/true respectively
1472      * 
1473      * @return type casted value
1474      */
1475     public static boolean toBooleanScalar(Object obj)
1476     throws TypeCastException
1477     {
1478         if (!isArray(obj)) {
1479             if (obj instanceof Boolean) {
1480                 return ((Boolean)obj).booleanValue();
1481             }
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");
1487             }
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");
1493             }
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");
1499             }
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");
1505             }
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");
1511             }
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");
1517             }
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");
1523             }
1524             throw new TypeCastException(obj.getClass());
1525         }
1526         int len = getArrayLength(obj);
1527         if (len!=1)
1528             throw new TypeCastException("Expected length of array is 1");
1529
1530         if (obj instanceof boolean[])
1531         {
1532             return ((boolean[])obj)[0];
1533         }
1534         if (obj instanceof Boolean[])
1535         {
1536             Boolean[] array = (Boolean[]) obj;
1537             return array[0].booleanValue();
1538         }
1539         if (obj instanceof int[])
1540         {
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");
1545         }
1546         if (obj instanceof Integer[])
1547         {
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");
1552         }
1553         if (obj instanceof Float[])
1554         {
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");
1559         }
1560         if (obj instanceof float[])
1561         {
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");
1566         }
1567         if (obj instanceof Long[])
1568         {
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");
1573         }
1574         if (obj instanceof long[])
1575         {
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");
1580         }
1581         if (obj instanceof Short[])
1582         {
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");
1587         }
1588         if (obj instanceof short[])
1589         {
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");
1594         }
1595         if (obj instanceof Byte[])
1596         {
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");
1601         }
1602         if (obj instanceof byte[])
1603         {
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");
1608         }
1609         if (obj instanceof String[])
1610         {
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");
1616         }
1617         if (obj instanceof Double[])
1618         {
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");
1623         }
1624         if (obj instanceof double[])
1625         {
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");
1630         }
1631         throw new TypeCastException(obj.getClass());
1632     }
1633
1634
1635     @SuppressWarnings("unchecked")
1636     public static <T> T adaptToClass(Object obj, Class<T> clazz)
1637     throws TypeCastException
1638     {
1639         if (clazz.equals(double[].class))
1640             return (T) toDoubleArray(obj);
1641         if (clazz.equals(Double.class))
1642             return (T) new Double(toDoubleScalar(obj));
1643
1644         if (clazz.equals(float[].class))
1645             return (T) toFloatArray(obj);
1646         if (clazz.equals(Float.class))
1647             return (T) new Float(toFloatScalar(obj));
1648
1649         if (clazz.equals(long[].class))
1650             return (T) toLongArray(obj);
1651         if (clazz.equals(Long.class))
1652             return (T) new Long(toLongScalar(obj));
1653
1654         if (clazz.equals(int[].class))
1655             return (T) toIntegerArray(obj);
1656         if (clazz.equals(Integer.class))
1657             return (T) new Integer(toIntegerScalar(obj));
1658
1659         if (clazz.equals(boolean[].class))
1660             return (T) toBooleanArray(obj);
1661         if (clazz.equals(Boolean.class))
1662             return (T) new Long(toLongScalar(obj));
1663
1664         throw new TypeCastException("Unsupported conversion type from "+obj.getClass()+" to "+clazz);
1665     }
1666
1667
1668
1669     public static String toString(Object obj) {
1670         if (obj instanceof Object[]) {
1671             return Arrays.toString((Object[])obj);
1672         }
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();
1682     }
1683 }