View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils2.locale;
19  
20  import java.util.Locale;
21  
22  /**
23   * <p>
24   * Utility methods for converting locale-sensitive String scalar values to objects of the specified Class, String arrays to arrays of the specified Class and
25   * object to locale-sensitive String scalar value.
26   * </p>
27   *
28   * <p>
29   * The implementations for these method are provided by {@link LocaleConvertUtilsBean}. These static utility method use the default instance. More sophisticated
30   * can be provided by using a {@code LocaleConvertUtilsBean} instance.
31   * </p>
32   */
33  public final class LocaleConvertUtils {
34  
35      /**
36       * <p>
37       * Converts the specified locale-sensitive value into a String.
38       * </p>
39       *
40       * <p>
41       * For more details see {@code LocaleConvertUtilsBean}
42       * </p>
43       *
44       * @param value The Value to be converted
45       * @return the converted value
46       * @see LocaleConvertUtilsBean#convert(Object)
47       */
48      public static String convert(final Object value) {
49          return LocaleConvertUtilsBean.getInstance().convert(value);
50      }
51  
52      /**
53       * <p>
54       * Converts the specified locale-sensitive value into a String using the particular conversion pattern.
55       * </p>
56       *
57       * <p>
58       * For more details see {@code LocaleConvertUtilsBean}
59       * </p>
60       *
61       * @param value   The Value to be converted
62       * @param locale  The locale
63       * @param pattern The conversion pattern
64       * @return the converted value
65       * @see LocaleConvertUtilsBean#convert(Object, Locale, String)
66       */
67      public static String convert(final Object value, final Locale locale, final String pattern) {
68          return LocaleConvertUtilsBean.getInstance().convert(value, locale, pattern);
69      }
70  
71      /**
72       * <p>
73       * Converts the specified locale-sensitive value into a String using the conversion pattern.
74       * </p>
75       *
76       * <p>
77       * For more details see {@code LocaleConvertUtilsBean}
78       * </p>
79       *
80       * @param value   The Value to be converted
81       * @param pattern The conversion pattern
82       * @return the converted value
83       * @see LocaleConvertUtilsBean#convert(Object, String)
84       */
85      public static String convert(final Object value, final String pattern) {
86          return LocaleConvertUtilsBean.getInstance().convert(value, pattern);
87      }
88  
89      /**
90       * <p>
91       * Converts the specified value to an object of the specified class (if possible). Otherwise, return a String representation of the value.
92       * </p>
93       *
94       * <p>
95       * For more details see {@code LocaleConvertUtilsBean}
96       * </p>
97       *
98       * @param value The String scalar value to be converted
99       * @param clazz The Data type to which this value should be converted.
100      * @return the converted value
101      * @see LocaleConvertUtilsBean#convert(String, Class)
102      */
103     public static Object convert(final String value, final Class<?> clazz) {
104         return LocaleConvertUtilsBean.getInstance().convert(value, clazz);
105     }
106 
107     /**
108      * <p>
109      * Converts the specified value to an object of the specified class (if possible) using the conversion pattern. Otherwise, return a String representation of
110      * the value.
111      * </p>
112      *
113      * <p>
114      * For more details see {@code LocaleConvertUtilsBean}
115      * </p>
116      *
117      * @param value   The String scalar value to be converted
118      * @param clazz   The Data type to which this value should be converted.
119      * @param locale  The locale
120      * @param pattern The conversion pattern
121      * @return the converted value
122      * @see LocaleConvertUtilsBean#convert(String, Class, Locale, String)
123      */
124     public static Object convert(final String value, final Class<?> clazz, final Locale locale, final String pattern) {
125         return LocaleConvertUtilsBean.getInstance().convert(value, clazz, locale, pattern);
126     }
127 
128     /**
129      * <p>
130      * Converts the specified value to an object of the specified class (if possible) using the conversion pattern. Otherwise, return a String representation of
131      * the value.
132      * </p>
133      *
134      * <p>
135      * For more details see {@code LocaleConvertUtilsBean}
136      * </p>
137      *
138      * @param value   The String scalar value to be converted
139      * @param clazz   The Data type to which this value should be converted.
140      * @param pattern The conversion pattern
141      * @return the converted value
142      * @see LocaleConvertUtilsBean#convert(String, Class, String)
143      */
144     public static Object convert(final String value, final Class<?> clazz, final String pattern) {
145         return LocaleConvertUtilsBean.getInstance().convert(value, clazz, pattern);
146     }
147 
148     /**
149      * <p>
150      * Convert an array of specified values to an array of objects of the specified class (if possible).
151      * </p>
152      *
153      * <p>
154      * For more details see {@code LocaleConvertUtilsBean}
155      * </p>
156      *
157      * @param values Value to be converted (may be null)
158      * @param clazz  Java array or element class to be converted to
159      * @return the converted value
160      * @see LocaleConvertUtilsBean#convert(String[], Class)
161      */
162     public static Object convert(final String[] values, final Class<?> clazz) {
163         return LocaleConvertUtilsBean.getInstance().convert(values, clazz);
164     }
165 
166     /**
167      * <p>
168      * Convert an array of specified values to an array of objects of the specified class (if possible) using the conversion pattern.
169      * </p>
170      *
171      * <p>
172      * For more details see {@code LocaleConvertUtilsBean}
173      * </p>
174      *
175      * @param values  Value to be converted (may be null)
176      * @param clazz   Java array or element class to be converted to
177      * @param locale  The locale
178      * @param pattern The conversion pattern
179      * @return the converted value
180      * @see LocaleConvertUtilsBean#convert(String[], Class, Locale, String)
181      */
182     public static Object convert(final String[] values, final Class<?> clazz, final Locale locale, final String pattern) {
183         return LocaleConvertUtilsBean.getInstance().convert(values, clazz, locale, pattern);
184     }
185 
186     /**
187      * <p>
188      * Convert an array of specified values to an array of objects of the specified class (if possible) using the conversion pattern.
189      * </p>
190      *
191      * <p>
192      * For more details see {@code LocaleConvertUtilsBean}
193      * </p>
194      *
195      * @param values  Value to be converted (may be null)
196      * @param clazz   Java array or element class to be converted to
197      * @param pattern The conversion pattern
198      * @return the converted value
199      * @see LocaleConvertUtilsBean#convert(String[], Class, String)
200      */
201     public static Object convert(final String[] values, final Class<?> clazz, final String pattern) {
202         return LocaleConvertUtilsBean.getInstance().convert(values, clazz, pattern);
203     }
204 
205     /**
206      * <p>
207      * Remove any registered {@link LocaleConverter}.
208      * </p>
209      *
210      * <p>
211      * For more details see {@code LocaleConvertUtilsBean}
212      * </p>
213      *
214      * @see LocaleConvertUtilsBean#deregister()
215      */
216     public static void deregister() {
217         LocaleConvertUtilsBean.getInstance().deregister();
218     }
219 
220     /**
221      * <p>
222      * Remove any registered {@link LocaleConverter} for the specified locale and Class.
223      * </p>
224      *
225      * <p>
226      * For more details see {@code LocaleConvertUtilsBean}
227      * </p>
228      *
229      * @param clazz  Class for which to remove a registered Converter
230      * @param locale The locale
231      * @see LocaleConvertUtilsBean#deregister(Class, Locale)
232      */
233     public static void deregister(final Class<?> clazz, final Locale locale) {
234         LocaleConvertUtilsBean.getInstance().deregister(clazz, locale);
235     }
236 
237     /**
238      * <p>
239      * Remove any registered {@link LocaleConverter} for the specified locale.
240      * </p>
241      *
242      * <p>
243      * For more details see {@code LocaleConvertUtilsBean}
244      * </p>
245      *
246      * @param locale The locale
247      * @see LocaleConvertUtilsBean#deregister(Locale)
248      */
249     public static void deregister(final Locale locale) {
250         LocaleConvertUtilsBean.getInstance().deregister(locale);
251     }
252 
253     /**
254      * <p>
255      * Gets applyLocalized.
256      * </p>
257      *
258      * <p>
259      * For more details see {@code LocaleConvertUtilsBean}
260      * </p>
261      *
262      * @return {@code true} if pattern is localized, otherwise {@code false}
263      * @see LocaleConvertUtilsBean#getApplyLocalized()
264      */
265     public static boolean getApplyLocalized() {
266         return LocaleConvertUtilsBean.getInstance().getApplyLocalized();
267     }
268 
269     /**
270      * <p>
271      * Gets the {@link Locale} which will be used when no {@link Locale} is passed to a method.
272      * </p>
273      *
274      * <p>
275      * For more details see {@code LocaleConvertUtilsBean}
276      * </p>
277      *
278      * @return the default locale
279      * @see LocaleConvertUtilsBean#getDefaultLocale()
280      */
281     public static Locale getDefaultLocale() {
282         return LocaleConvertUtilsBean.getInstance().getDefaultLocale();
283     }
284 
285     /**
286      * <p>
287      * Look up and return any registered {@link LocaleConverter} for the specified destination class and locale; if there is no registered Converter, return
288      * {@code null}.
289      * </p>
290      *
291      * <p>
292      * For more details see {@code LocaleConvertUtilsBean}
293      * </p>
294      *
295      * @param <T>    The converter type.
296      * @param clazz  Class for which to return a registered Converter
297      * @param locale The Locale
298      * @return The registered locale Converter, if any
299      * @see LocaleConvertUtilsBean#lookup(Class, Locale)
300      */
301     public static <T> LocaleConverter<T> lookup(final Class<T> clazz, final Locale locale) {
302         return LocaleConvertUtilsBean.getInstance().lookup(clazz, locale);
303     }
304 
305     /**
306      * <p>
307      * Register a custom {@link LocaleConverter} for the specified destination {@code Class}, replacing any previously registered converter.
308      * </p>
309      *
310      * <p>
311      * For more details see {@code LocaleConvertUtilsBean}
312      * </p>
313      *
314      * @param <T>       The converter type.
315      * @param converter The LocaleConverter to be registered
316      * @param clazz     The Destination class for conversions performed by this Converter
317      * @param locale    The locale
318      * @see LocaleConvertUtilsBean#register(LocaleConverter, Class, Locale)
319      */
320     public static <T> void register(final LocaleConverter<T> converter, final Class<T> clazz, final Locale locale) {
321         LocaleConvertUtilsBean.getInstance().register(converter, clazz, locale);
322     }
323 
324     /**
325      * <p>
326      * Sets applyLocalized.
327      * </p>
328      *
329      * <p>
330      * For more details see {@code LocaleConvertUtilsBean}
331      * </p>
332      *
333      * @param newApplyLocalized {@code true} if pattern is localized, otherwise {@code false}
334      * @see LocaleConvertUtilsBean#setApplyLocalized(boolean)
335      */
336     public static void setApplyLocalized(final boolean newApplyLocalized) {
337         LocaleConvertUtilsBean.getInstance().setApplyLocalized(newApplyLocalized);
338     }
339 
340     /**
341      * <p>
342      * Sets the {@link Locale} which will be used when no {@link Locale} is passed to a method.
343      * </p>
344      *
345      * <p>
346      * For more details see {@code LocaleConvertUtilsBean}
347      * </p>
348      *
349      * @param locale the default locale
350      * @see LocaleConvertUtilsBean#setDefaultLocale(Locale)
351      */
352     public static void setDefaultLocale(final Locale locale) {
353         LocaleConvertUtilsBean.getInstance().setDefaultLocale(locale);
354     }
355 
356     private LocaleConvertUtils() {
357         // empty
358     }
359 }