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