1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.transform.converters;
17
18 import java.util.Locale;
19
20 import net.sf.composite.util.ObjectUtils;
21 import net.sf.morph.transform.DecoratedConverter;
22 import net.sf.morph.transform.transformers.BaseTransformer;
23
24 /**
25 * Converts any object to a Boolean. Empty objects will be converted to
26 * <code>false</code> and non-empty objects will be converted to
27 * <code>true</code>.
28 *
29 * @author Matt Sgarlata
30 * @since Dec 31, 2004
31 * @see net.sf.morph.util.MorphUtils#isEmpty(Object)
32 */
33 public class ObjectToBooleanConverter extends BaseTransformer implements DecoratedConverter {
34
35 private static final Class[] DESTINATION_TYPES = { Boolean.class,
36 boolean.class };
37
38 private static final Class[] SOURCE_TYPES = { Object.class, null };
39
40 /**
41 * {@inheritDoc}
42 */
43 protected Object convertImpl(Class destinationClass, Object source,
44 Locale locale) throws Exception {
45
46 return ObjectUtils.isEmpty(source) ? Boolean.FALSE : Boolean.TRUE;
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 protected boolean isWrappingRuntimeExceptions() {
53 return true;
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 protected Class[] getSourceClassesImpl() throws Exception {
60 return SOURCE_TYPES;
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 protected Class[] getDestinationClassesImpl() throws Exception {
67 return DESTINATION_TYPES;
68 }
69
70 }