1 /***
2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.
3 * <p>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * <p>
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer. Redistributions in binary
9 * form must reproduce the above copyright notice, this list of conditions and
10 * the following disclaimer in the documentation and/or other materials provided
11 * with the distribution. Neither the name of CodeStreet LLC. nor the names of
12 * its contributors may be used to endorse or promote products derived from this
13 * software without specific prior written permission.
14 * <p>
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 * <p>
27 */
28 package com.codestreet.messageforge;
29
30 import java.text.NumberFormat;
31
32 /***
33 * Base class for all converters.
34 *
35 * @author Jawaid Hakim.
36 */
37 public abstract class Converter
38 {
39 /***
40 * Get the version of the library.
41 *
42 * @return Converter version.
43 */
44 public static String getVersion()
45 {
46 // TODO: get version from repository
47 return "1.0";
48 }
49
50 public static RMapMessage createMsgObject(String msgName)
51 throws ConverterException
52 {
53 try
54 {
55 return RMsgFactoryImpl.getInstance().createMsgObject(msgName);
56 }
57 catch (FactoryException ex)
58 {
59 throw new ConverterException(ex);
60 }
61 }
62
63 public static RMapMessage createMsgObject(Class cls)
64 throws ConverterException
65 {
66 try
67 {
68 return RMsgFactoryImpl.getInstance().createMsgObject(cls);
69 }
70 catch (FactoryException ex)
71 {
72 throw new ConverterException(ex);
73 }
74 }
75
76 /***
77 * Set default message factory. More than one message factory may be
78 * installed. If more than one factory is then installed then each factory
79 * is used - one at a time - until teh desired message can be instantiated.
80 *
81 * @param factories
82 * Default message factories.
83 */
84 public static synchronized void setDefaultFactory(RMsgFactory[] factories)
85 throws ConverterException
86 {
87 RMsgFactoryImpl.getInstance().setDefaultMsgFactories(factories);
88 }
89
90 /***
91 * Sets the maximum number of digits allowed in the fraction portion of a
92 * BigDecimal. Default is <code>8</code>. If less than 0 then 0 is used.
93 *
94 * @param maximumFractionDigits
95 * maximum number of digits allowed in the fraction portion of a
96 * BigDecimal
97 */
98 public static synchronized void setMaximumFractionDigits(
99 int maximumFractionDigits)
100 {
101 maximumFractionDigits_ = maximumFractionDigits;
102 }
103
104 /***
105 * Set bean validator instance.
106 *
107 * @param beanValidator
108 * Bean validator instance. This instance will be called to
109 * validate beans.
110 */
111 public static synchronized void setBeanValidator(
112 RBeanValidator beanValidator)
113 {
114 beanValidator_ = beanValidator;
115 }
116
117 /***
118 * Get the bean validator instance.
119 *
120 * @return Bean validator instance.
121 */
122 public static RBeanValidator getBeanValidator()
123 {
124 return beanValidator_;
125 }
126
127 /***
128 * Marshall a <tt>RFldDatetime</tt> field object as a string.
129 *
130 * @param dateTime
131 * Datetime field object.
132 * @return Message object as a string.
133 * @see #setDateFromStr(RFldDatetime, String)
134 */
135 protected static String dateToStr(RFldDatetime dateTime)
136 {
137 return RDateFormat.getInstance().format(dateTime.getValue());
138 }
139
140 /***
141 * Ummarshall a <tt>RFldDatetime</tt> field object from a string. The
142 * string is parsed and the value of the message object is set.
143 *
144 * @param dateTime
145 * Datetime field object.
146 * @param strDate
147 * Date. It should have been formatted using dateToStr().
148 * @see #dateToStr(RFldDatetime)
149 */
150 protected static void setDateFromStr(RFldDatetime dateTime, String strDate)
151 throws FieldValidationException
152 {
153 try
154 {
155 dateTime.set(RDateFormat.getInstance().parse(strDate));
156 }
157 catch (java.text.ParseException ex)
158 {
159 throw new FieldValidationException(ex);
160 }
161 }
162
163 /***
164 * Marshall a <tt>RFldDatetime</tt> field object as a <tt>Long</tt>.
165 *
166 * @param dateTime
167 * Datetime field object.
168 * @return Message object as a <tt>Long</tt>.
169 * @see #setDateFromLong(RFldDatetime, Long)
170 */
171 protected static Long dateToLong(RFldDatetime dateTime)
172 {
173 return new Long(dateTime.getValue().getTime());
174 }
175
176 /***
177 * Ummarshall a <tt>RFldDatetime</tt> field object from a <tt>Long</tt>.
178 *
179 * @param dateTime
180 * Datetime field object.
181 * @param longDate
182 * Date.
183 * @see #dateToLong(RFldDatetime)
184 */
185 protected static void setDateFromLong(RFldDatetime dateTime, Long longDate)
186 throws FieldValidationException
187 {
188 dateTime.set(longDate.longValue());
189 }
190
191 /***
192 * Marshall a <tt>RFldDatetime</tt> field object as a <tt>Double</tt>.
193 *
194 * @param dateTime
195 * Datetime field object.
196 * @return Message object as a <tt>Double</tt>.
197 * @see #setDateFromLong(RFldDatetime, Long)
198 */
199 protected static Double dateToDouble(RFldDatetime dateTime)
200 {
201 return new Double(dateTime.getValue().getTime());
202 }
203
204 /***
205 * Ummarshall a <tt>RFldDatetime</tt> field object from a <tt>Double</tt>.
206 *
207 * @param dateTime
208 * Datetime field object.
209 * @param dblDate
210 * Date.
211 * @see #dateToLong(RFldDatetime)
212 */
213 protected static void setDateFromDouble(RFldDatetime dateTime,
214 Double dblDate) throws FieldValidationException
215 {
216 dateTime.set(dblDate.longValue());
217 }
218
219 /***
220 * Get the lenient setting for the date formatter. Default locale is
221 * <tt>true</tt>.
222 *
223 * @return Lenient setting.
224 */
225 public static boolean getLenient()
226 {
227 return lenient_;
228 }
229
230 /***
231 * Get the number formatter. This returns a Threadsafe <tt>NumberFormat</tt>
232 * instance.
233 *
234 * @return Threadsafe <tt>NumberFormat</tt> instance.
235 * <code>GroupingUsed</code> is set to <code>false</code> and
236 * <code>MaximumFractionDigits</code> is set to
237 * <code>maximumFractionDigits</code>.
238 * @see #setMaximumFractionDigits(int)
239 */
240 static NumberFormat getBigDecimalFormatter()
241 {
242 NumberFormat numberFmt = (NumberFormat) BIGDECIMAL_FORMAT_THREADLOCAL
243 .get();
244 if (numberFmt == null)
245 {
246 numberFmt = NumberFormat.getNumberInstance();
247 numberFmt.setGroupingUsed(false);
248 numberFmt.setMaximumFractionDigits(maximumFractionDigits_);
249 BIGDECIMAL_FORMAT_THREADLOCAL.set(numberFmt);
250 }
251 return numberFmt;
252 }
253
254 private static RBeanValidator beanValidator_;
255
256 private static int maximumFractionDigits_ = 8;
257
258 private static boolean lenient_ = true;
259
260 private static final ThreadLocal BIGDECIMAL_FORMAT_THREADLOCAL = new ThreadLocal();
261 }
This page was automatically generated by Maven