KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > TypeConverter


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xb.binding;
8
9 import java.text.ParseException JavaDoc;
10
11 /**
12  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
13  * @version <tt>$Revision: 1.1.2.1 $</tt>
14  */

15 public interface TypeConverter
16 {
17    TypeConverter STRING = new TypeConverter()
18    {
19       public Object JavaDoc unmarshal(String JavaDoc value)
20       {
21          return value;
22       }
23
24       public String JavaDoc marshal(Object JavaDoc value)
25       {
26          return (String JavaDoc)value;
27       }
28    };
29
30    TypeConverter INT = new TypeConverter()
31    {
32       public Object JavaDoc unmarshal(String JavaDoc value)
33       {
34          return Integer.valueOf(value);
35       }
36
37       public String JavaDoc marshal(Object JavaDoc value)
38       {
39          return String.valueOf(value);
40       }
41    };
42
43    TypeConverter LONG = new TypeConverter()
44    {
45       public Object JavaDoc unmarshal(String JavaDoc value)
46       {
47          return Long.valueOf(value);
48       }
49
50       public String JavaDoc marshal(Object JavaDoc value)
51       {
52          return String.valueOf(value);
53       }
54    };
55
56    TypeConverter DOUBLE = new TypeConverter()
57    {
58       public Object JavaDoc unmarshal(String JavaDoc value)
59       {
60          return Double.valueOf(value);
61       }
62
63       public String JavaDoc marshal(Object JavaDoc value)
64       {
65          return String.valueOf(value);
66       }
67    };
68
69    TypeConverter FLOAT = new TypeConverter()
70    {
71       public Object JavaDoc unmarshal(String JavaDoc value)
72       {
73          return Float.valueOf(value);
74       }
75
76       public String JavaDoc marshal(Object JavaDoc value)
77       {
78          return String.valueOf(value);
79       }
80    };
81
82    TypeConverter SHORT = new TypeConverter()
83    {
84       public Object JavaDoc unmarshal(String JavaDoc value)
85       {
86          return Short.valueOf(value);
87       }
88
89       public String JavaDoc marshal(Object JavaDoc value)
90       {
91          return String.valueOf(value);
92       }
93    };
94
95    TypeConverter BYTE = new TypeConverter()
96    {
97       public Object JavaDoc unmarshal(String JavaDoc value)
98       {
99          return Byte.valueOf(value);
100       }
101
102       public String JavaDoc marshal(Object JavaDoc value)
103       {
104          return String.valueOf(value);
105       }
106    };
107
108    TypeConverter CHAR = new TypeConverter()
109    {
110       public Object JavaDoc unmarshal(String JavaDoc value)
111       {
112          return value == null ? null : new Character JavaDoc(value.charAt(0));
113       }
114
115       public String JavaDoc marshal(Object JavaDoc value)
116       {
117          return String.valueOf(value);
118       }
119    };
120
121    TypeConverter JAVA_UTIL_DATE = new TypeConverter()
122    {
123       private static final String JavaDoc FORMAT = "yyyy-MM-dd";
124
125       public Object JavaDoc unmarshal(String JavaDoc value)
126       {
127          try
128          {
129             return new java.text.SimpleDateFormat JavaDoc(FORMAT).parse(value);
130          }
131          catch(ParseException JavaDoc e)
132          {
133             throw new IllegalStateException JavaDoc("Failed to parse date string value: " + value);
134          }
135       }
136
137       public String JavaDoc marshal(Object JavaDoc value)
138       {
139          return new java.text.SimpleDateFormat JavaDoc(FORMAT).format(value);
140       }
141    };
142
143    Object JavaDoc unmarshal(String JavaDoc value);
144
145    String JavaDoc marshal(Object JavaDoc value);
146 }
147
Popular Tags