KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > converters > StringArrayConverter


1 package jodd.bean.converters;
2
3 /**
4  * Converts given object to String[].
5  * If object is an array than it is converted to String[] array.
6  * If object is not an array, new String[] array will be created with one
7  * element only: String of the given object.
8  */

9 public class StringArrayConverter implements jodd.bean.Converter {
10
11     public Object convert(Object value) {
12
13         if (value == null) {
14             return (String) null;
15         }
16
17         // handle arrays
18
if (value.getClass().isArray() == true) {
19             if (value instanceof String[]) {
20                 return (String[]) value;
21             } else {
22                 Object[] valueArray = (Object[]) value;
23                 String[] result = new String[valueArray.length];
24                 for (int i = 0; i < valueArray.length; i++) {
25                     result[i] = valueArray[i].toString();
26                 }
27                 return result;
28             }
29         }
30         // handle all other values
31
return new String[] { value.toString() };
32     }
33 }
34
Popular Tags