1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nl.openedge.baritus.util;
32
33 import java.lang.reflect.Array;
34 import java.util.Collection;
35 import java.util.Map;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 import nl.openedge.baritus.ConverterRegistry;
41 import nl.openedge.baritus.converters.ConversionException;
42 import nl.openedge.baritus.converters.Converter;
43
44 /***
45 * Misc utility methods for handling values
46 *
47 * @author Eelco Hillenius
48 */
49 public final class ValueUtils
50 {
51
52
53 private static Log log = LogFactory.getLog(ValueUtils.class);
54
55 /***
56 * check if the value is null or empty
57 * @param value object to check on
58 * @return true if value is not null AND not empty (e.g.
59 * in case of a String or Collection)
60 */
61 public static boolean isNullOrEmpty(Object value)
62 {
63 if (value instanceof String)
64 {
65 return (value == null || (((String)value).trim().equals("")));
66 }
67 if (value instanceof Object[])
68 {
69 if (value == null)
70 {
71 return true;
72 }
73 else if (((Object[])value).length == 0)
74 {
75 return true;
76 }
77 else if (((Object[])value).length == 1)
78 {
79 return isNullOrEmpty(((Object[])value)[0]);
80 }
81 else
82 {
83 return false;
84 }
85 }
86 else if (value instanceof Collection)
87 {
88 return (value == null || (((Collection)value).isEmpty()));
89 }
90 else if (value instanceof Map)
91 {
92 return (value == null || (((Map)value).isEmpty()));
93 }
94 else
95 {
96 return (value == null);
97 }
98 }
99
100 /***
101 * Convert the specified value into a String. If the specified value
102 * is an array, the first element (converted to a String) will be
103 * returned. The registered {@link Converter} for the
104 * <code>java.lang.String</code> class will be used, which allows
105 * applications to customize Object->String conversions (the default
106 * implementation simply uses toString()).
107 *
108 * @param value Value to be converted (may be null)
109 */
110 public static String convertToString(Object value)
111 {
112
113 if (value == null)
114 {
115 return ((String) null);
116 }
117 else if (value.getClass().isArray())
118 {
119 if (Array.getLength(value) < 1)
120 {
121 return (null);
122 }
123 value = Array.get(value, 0);
124 if (value == null)
125 {
126 return ((String) null);
127 }
128 else
129 {
130 try
131 {
132 Converter converter = ConverterRegistry.getInstance().lookup(String.class);
133 Object converted = converter.convert(String.class, value);
134 return (converted instanceof String) ?
135 (String)converted : String.valueOf(converted);
136 }
137 catch (Exception e)
138 {
139 e.printStackTrace();
140 throw new ConversionException(e);
141 }
142 }
143 }
144 else
145 {
146 try
147 {
148 Converter converter = ConverterRegistry.getInstance().lookup(String.class);
149 Object converted = converter.convert(String.class, value);
150 return (converted instanceof String) ?
151 (String)converted : String.valueOf(converted);
152 }
153 catch (Exception e)
154 {
155 log.error(e.getMessage(), e);
156 throw new ConversionException(e);
157 }
158 }
159
160 }
161
162 }