KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > BooleanArrayConverter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.typeconverter;
4
5 /**
6  * Converts given object to byte[].
7  */

8 public class BooleanArrayConverter implements TypeConverter {
9
10     public static boolean[] valueOf(Object JavaDoc value) {
11         if (value == null) {
12             return null;
13         }
14         Class JavaDoc type = value.getClass();
15         if (type.isArray() == false) {
16             if (value instanceof Boolean JavaDoc) {
17                 return new boolean[] {((Boolean JavaDoc) value).booleanValue()};
18             }
19             return new boolean[] {BooleanConverter.valueOf(value.toString()).booleanValue()};
20         }
21
22         if (type == boolean[].class) {
23             return (boolean[]) value;
24         }
25         if (type == int[].class) {
26             int[] values = (int[]) value;
27             boolean[] results = new boolean[values.length];
28             for (int i = 0; i < values.length; i++) {
29                 results[i] = (values[i] != 0);
30             }
31             return results;
32         }
33         if (type == long[].class) {
34             long[] values = (long[]) value;
35             boolean[] results = new boolean[values.length];
36             for (int i = 0; i < values.length; i++) {
37                 results[i] = (values[i] != 0);
38             }
39             return results;
40         }
41         if (type == double[].class) {
42             double[] values = (double[]) value;
43             boolean[] results = new boolean[values.length];
44             for (int i = 0; i < values.length; i++) {
45                 results[i] = (values[i] != 0);
46             }
47             return results;
48         }
49         if (type == float[].class) {
50             float[] values = (float[]) value;
51             boolean[] results = new boolean[values.length];
52             for (int i = 0; i < values.length; i++) {
53                 results[i] = (values[i] != 0);
54             }
55             return results;
56         }
57         if (type == byte[].class) {
58             byte[] values = (byte[]) value;
59             boolean[] results = new boolean[values.length];
60             for (int i = 0; i < values.length; i++) {
61                 results[i] = (values[i] != 0);
62             }
63             return results;
64         }
65         if (type == short[].class) {
66             short[] values = (short[]) value;
67             boolean[] results = new boolean[values.length];
68             for (int i = 0; i < values.length; i++) {
69                 results[i] = (values[i] != 0);
70             }
71             return results;
72         }
73
74         Object JavaDoc[] values = (Object JavaDoc[]) value;
75         boolean[] results = new boolean[values.length];
76         for (int i = 0; i < values.length; i++) {
77             results[i] = BooleanConverter.valueOf(values[i]).booleanValue();
78         }
79         return results;
80     }
81
82     public Object JavaDoc convert(Object JavaDoc value) {
83         return valueOf(value);
84     }
85 }
86
Popular Tags