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