1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.strategy;
18
19 import org.apache.commons.beanutils.ConvertUtils;
20 import org.apache.commons.betwixt.expression.Context;
21
22 /***
23 * String <-> object conversion strategy that delegates to ConvertUtils.
24 *
25 * @author Robert Burrell Donkin
26 * @since 0.5
27 */
28 public class ConvertUtilsObjectStringConverter extends ObjectStringConverter {
29
30 /***
31 * Converts an object to a string representation using ConvertUtils.
32 *
33 * @param object the object to be converted, possibly null
34 * @param type the property class of the object, not null
35 * @param flavour a string allow symantic differences in formatting
36 * to be communicated (ignored)
37 * @param context not null
38 * @return a String representation, not null
39 */
40 public String objectToString(Object object, Class type, String flavour, Context context) {
41 if ( object != null ) {
42 String text = ConvertUtils.convert( object );
43 if ( text != null ) {
44 return text;
45 }
46 }
47 return "";
48 }
49
50 /***
51 * Converts an object to a string representation using ConvertUtils.
52 * This implementation ignores null and empty string values (rather than converting them).
53 *
54 * @param value the String to be converted, not null
55 * @param type the property class to be returned (if possible), not null
56 * @param flavour a string allow symantic differences in formatting
57 * to be communicated (ignored)
58 * @param context not null
59 * @return an Object converted from the String, not null
60 */
61 public Object stringToObject(String value, Class type, String flavour, Context context) {
62 if (value == null || "".equals(value))
63 {
64 return null;
65 }
66
67 return ConvertUtils.convert( value, type );
68 }
69 }