001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale;
019:
020: import org.apache.commons.collections.FastHashMap;
021:
022: import java.util.Locale;
023:
024: /**
025: * <p>Utility methods for converting locale-sensitive String scalar values to objects of the
026: * specified Class, String arrays to arrays of the specified Class and
027: * object to locale-sensitive String scalar value.</p>
028: *
029: * <p>The implementations for these method are provided by {@link LocaleConvertUtilsBean}.
030: * These static utility method use the default instance. More sophisticated can be provided
031: * by using a <code>LocaleConvertUtilsBean</code> instance.</p>
032: *
033: * @author Yauheny Mikulski
034: */
035: public class LocaleConvertUtils {
036:
037: // ----------------------------------------------------- Instance Variables
038:
039: /**
040: * <p>Gets the <code>Locale</code> which will be used when
041: * no <code>Locale</code> is passed to a method.</p>
042: *
043: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
044: * @return the default locale
045: * @see LocaleConvertUtilsBean#getDefaultLocale()
046: */
047: public static Locale getDefaultLocale() {
048:
049: return LocaleConvertUtilsBean.getInstance().getDefaultLocale();
050: }
051:
052: /**
053: * <p>Sets the <code>Locale</code> which will be used when
054: * no <code>Locale</code> is passed to a method.</p>
055: *
056: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
057: *
058: * @param locale the default locale
059: * @see LocaleConvertUtilsBean#setDefaultLocale(Locale)
060: */
061: public static void setDefaultLocale(Locale locale) {
062:
063: LocaleConvertUtilsBean.getInstance().setDefaultLocale(locale);
064: }
065:
066: /**
067: * <p>Gets applyLocalized.</p>
068: *
069: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
070: *
071: * @return <code>true</code> if pattern is localized,
072: * otherwise <code>false</code>
073: * @see LocaleConvertUtilsBean#getApplyLocalized()
074: */
075: public static boolean getApplyLocalized() {
076: return LocaleConvertUtilsBean.getInstance().getApplyLocalized();
077: }
078:
079: /**
080: * <p>Sets applyLocalized.</p>
081: *
082: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
083: *
084: * @param newApplyLocalized <code>true</code> if pattern is localized,
085: * otherwise <code>false</code>
086: * @see LocaleConvertUtilsBean#setApplyLocalized(boolean)
087: */
088: public static void setApplyLocalized(boolean newApplyLocalized) {
089: LocaleConvertUtilsBean.getInstance().setApplyLocalized(
090: newApplyLocalized);
091: }
092:
093: // --------------------------------------------------------- Methods
094:
095: /**
096: * <p>Convert the specified locale-sensitive value into a String.</p>
097: *
098: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
099: *
100: * @param value The Value to be converted
101: * @return the converted value
102: * @see LocaleConvertUtilsBean#convert(Object)
103: */
104: public static String convert(Object value) {
105: return LocaleConvertUtilsBean.getInstance().convert(value);
106: }
107:
108: /**
109: * <p>Convert the specified locale-sensitive value into a String
110: * using the conversion pattern.</p>
111: *
112: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
113: *
114: * @param value The Value to be converted
115: * @param pattern The convertion pattern
116: * @return the converted value
117: * @see LocaleConvertUtilsBean#convert(Object, String)
118: */
119: public static String convert(Object value, String pattern) {
120: return LocaleConvertUtilsBean.getInstance().convert(value,
121: pattern);
122: }
123:
124: /**
125: * <p>Convert the specified locale-sensitive value into a String
126: * using the paticular convertion pattern.</p>
127: *
128: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
129: *
130: * @param value The Value to be converted
131: * @param locale The locale
132: * @param pattern The convertion pattern
133: * @return the converted value
134: * @see LocaleConvertUtilsBean#convert(Object, Locale, String)
135: */
136: public static String convert(Object value, Locale locale,
137: String pattern) {
138:
139: return LocaleConvertUtilsBean.getInstance().convert(value,
140: locale, pattern);
141: }
142:
143: /**
144: * <p>Convert the specified value to an object of the specified class (if
145: * possible). Otherwise, return a String representation of the value.</p>
146: *
147: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
148: *
149: * @param value The String scalar value to be converted
150: * @param clazz The Data type to which this value should be converted.
151: * @return the converted value
152: * @see LocaleConvertUtilsBean#convert(String, Class)
153: */
154: public static Object convert(String value, Class clazz) {
155:
156: return LocaleConvertUtilsBean.getInstance().convert(value,
157: clazz);
158: }
159:
160: /**
161: * <p>Convert the specified value to an object of the specified class (if
162: * possible) using the convertion pattern. Otherwise, return a String
163: * representation of the value.</p>
164: *
165: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
166: *
167: * @param value The String scalar value to be converted
168: * @param clazz The Data type to which this value should be converted.
169: * @param pattern The convertion pattern
170: * @return the converted value
171: * @see LocaleConvertUtilsBean#convert(String, Class, String)
172: */
173: public static Object convert(String value, Class clazz,
174: String pattern) {
175:
176: return LocaleConvertUtilsBean.getInstance().convert(value,
177: clazz, pattern);
178: }
179:
180: /**
181: * <p>Convert the specified value to an object of the specified class (if
182: * possible) using the convertion pattern. Otherwise, return a String
183: * representation of the value.</p>
184: *
185: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
186: *
187: * @param value The String scalar value to be converted
188: * @param clazz The Data type to which this value should be converted.
189: * @param locale The locale
190: * @param pattern The convertion pattern
191: * @return the converted value
192: * @see LocaleConvertUtilsBean#convert(String, Class, Locale, String)
193: */
194: public static Object convert(String value, Class clazz,
195: Locale locale, String pattern) {
196:
197: return LocaleConvertUtilsBean.getInstance().convert(value,
198: clazz, locale, pattern);
199: }
200:
201: /**
202: * <p>Convert an array of specified values to an array of objects of the
203: * specified class (if possible) using the convertion pattern.</p>
204: *
205: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
206: *
207: * @param values Value to be converted (may be null)
208: * @param clazz Java array or element class to be converted to
209: * @param pattern The convertion pattern
210: * @return the converted value
211: * @see LocaleConvertUtilsBean#convert(String[], Class, String)
212: */
213: public static Object convert(String[] values, Class clazz,
214: String pattern) {
215:
216: return LocaleConvertUtilsBean.getInstance().convert(values,
217: clazz, pattern);
218: }
219:
220: /**
221: * <p>Convert an array of specified values to an array of objects of the
222: * specified class (if possible).</p>
223: *
224: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
225: *
226: * @param values Value to be converted (may be null)
227: * @param clazz Java array or element class to be converted to
228: * @return the converted value
229: * @see LocaleConvertUtilsBean#convert(String[], Class)
230: */
231: public static Object convert(String[] values, Class clazz) {
232:
233: return LocaleConvertUtilsBean.getInstance().convert(values,
234: clazz);
235: }
236:
237: /**
238: * <p>Convert an array of specified values to an array of objects of the
239: * specified class (if possible) using the convertion pattern.</p>
240: *
241: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
242: *
243: * @param values Value to be converted (may be null)
244: * @param clazz Java array or element class to be converted to
245: * @param locale The locale
246: * @param pattern The convertion pattern
247: * @return the converted value
248: * @see LocaleConvertUtilsBean#convert(String[], Class, Locale, String)
249: */
250: public static Object convert(String[] values, Class clazz,
251: Locale locale, String pattern) {
252:
253: return LocaleConvertUtilsBean.getInstance().convert(values,
254: clazz, locale, pattern);
255: }
256:
257: /**
258: * <p>Register a custom {@link LocaleConverter} for the specified destination
259: * <code>Class</code>, replacing any previously registered converter.</p>
260: *
261: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
262: *
263: * @param converter The LocaleConverter to be registered
264: * @param clazz The Destination class for conversions performed by this
265: * Converter
266: * @param locale The locale
267: * @see LocaleConvertUtilsBean#register(LocaleConverter, Class, Locale)
268: */
269: public static void register(LocaleConverter converter, Class clazz,
270: Locale locale) {
271:
272: LocaleConvertUtilsBean.getInstance().register(converter, clazz,
273: locale);
274: }
275:
276: /**
277: * <p>Remove any registered {@link LocaleConverter}.</p>
278: *
279: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
280: *
281: * @see LocaleConvertUtilsBean#deregister()
282: */
283: public static void deregister() {
284:
285: LocaleConvertUtilsBean.getInstance().deregister();
286: }
287:
288: /**
289: * <p>Remove any registered {@link LocaleConverter} for the specified locale.</p>
290: *
291: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
292: *
293: * @param locale The locale
294: * @see LocaleConvertUtilsBean#deregister(Locale)
295: */
296: public static void deregister(Locale locale) {
297:
298: LocaleConvertUtilsBean.getInstance().deregister(locale);
299: }
300:
301: /**
302: * <p>Remove any registered {@link LocaleConverter} for the specified locale and Class.</p>
303: *
304: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
305: *
306: * @param clazz Class for which to remove a registered Converter
307: * @param locale The locale
308: * @see LocaleConvertUtilsBean#deregister(Class, Locale)
309: */
310: public static void deregister(Class clazz, Locale locale) {
311:
312: LocaleConvertUtilsBean.getInstance().deregister(clazz, locale);
313: }
314:
315: /**
316: * <p>Look up and return any registered {@link LocaleConverter} for the specified
317: * destination class and locale; if there is no registered Converter, return
318: * <code>null</code>.</p>
319: *
320: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
321: *
322: * @param clazz Class for which to return a registered Converter
323: * @param locale The Locale
324: * @return The registered locale Converter, if any
325: * @see LocaleConvertUtilsBean#lookup(Class, Locale)
326: */
327: public static LocaleConverter lookup(Class clazz, Locale locale) {
328:
329: return LocaleConvertUtilsBean.getInstance().lookup(clazz,
330: locale);
331: }
332:
333: /**
334: * <p>Look up and return any registered FastHashMap instance for the specified locale.</p>
335: *
336: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
337: *
338: * @param locale The Locale
339: * @return The FastHashMap instance contains the all {@link LocaleConverter} types for
340: * the specified locale.
341: * @see LocaleConvertUtilsBean#lookup(Locale)
342: * @deprecated This method will be modified to return a Map in the next release.
343: */
344: protected static FastHashMap lookup(Locale locale) {
345: return LocaleConvertUtilsBean.getInstance().lookup(locale);
346: }
347:
348: /**
349: * <p>Create all {@link LocaleConverter} types for specified locale.</p>
350: *
351: * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
352: *
353: * @param locale The Locale
354: * @return The FastHashMap instance contains the all {@link LocaleConverter} types
355: * for the specified locale.
356: * @see LocaleConvertUtilsBean#create(Locale)
357: * @deprecated This method will be modified to return a Map in the next release.
358: */
359: protected static FastHashMap create(Locale locale) {
360:
361: return LocaleConvertUtilsBean.getInstance().create(locale);
362: }
363: }
|