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 java.io.Serializable;
19
20 import org.apache.commons.betwixt.expression.Context;
21
22 /***
23 * <p>Strategy class for string <-> object conversions.
24 * Implementations of this interface are used by Betwixt to perform
25 * string <-> object conversions.
26 * This performs only the most basic conversions.
27 * Most applications will use a subclass.
28 * </p>
29 * <p>It is strongly recommended that (in order to support round tripping)
30 * that <code>objectToString</code> and <code>stringToObject</code>
31 * are inverse functions.
32 * In other words, given the same flavour, context and type the applying
33 * objectToString to the result of stringToObject should be equal to the
34 * original input.
35 * </p>
36 * @author Robert Burrell Donkin
37 * @since 0.5
38 */
39 public class ObjectStringConverter implements Serializable {
40
41 /***
42 * Converts an object to a string representation.
43 * This basic implementation returns object.toString()
44 * or an empty string if the given object is null.
45 *
46 * @param object the object to be converted, possibly null
47 * @param type the property class of the object, not null
48 * @param flavour a string allow symantic differences in formatting to be communicated
49 * @param context the context, not null
50 * @return a String representation, not null
51 */
52 public String objectToString(Object object, Class type, String flavour, Context context) {
53 if ( object != null ) {
54 return object.toString();
55 }
56 return "";
57 }
58
59 /***
60 * Converts a string representation to an object.
61 * It is acceptable for an implementation to return the string if it cannot convert
62 * the string to the given class type.
63 * This basic implementation just returns a string.
64 *
65 * @param value the String to be converted
66 * @param type the property class to be returned (if possible), not null
67 * @param flavour a string allow symantic differences in formatting to be communicated
68 * @param context the context, not null
69 * @return an Object converted from the String, not null
70 */
71 public Object stringToObject(String value, Class type, String flavour, Context context) {
72 return value;
73 }
74 }