1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.beanutils.locale;
  17. import org.apache.commons.collections.FastHashMap;
  18. import java.util.Locale;
  19. /**
  20. * <p>Utility methods for converting locale-sensitive String scalar values to objects of the
  21. * specified Class, String arrays to arrays of the specified Class and
  22. * object to locale-sensitive String scalar value.</p>
  23. *
  24. * <p>The implementations for these method are provided by {@link LocaleConvertUtilsBean}.
  25. * These static utility method use the default instance. More sophisticated can be provided
  26. * by using a <code>LocaleConvertUtilsBean</code> instance.</p>
  27. *
  28. * @author Yauheny Mikulski
  29. */
  30. public class LocaleConvertUtils {
  31. // ----------------------------------------------------- Instance Variables
  32. /**
  33. * <p>Gets the <code>Locale</code> which will be used when
  34. * no <code>Locale</code> is passed to a method.</p>
  35. *
  36. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  37. *
  38. * @see LocaleConvertUtilsBean#getDefaultLocale()
  39. */
  40. public static Locale getDefaultLocale() {
  41. return LocaleConvertUtilsBean.getInstance().getDefaultLocale();
  42. }
  43. /**
  44. * <p>Sets the <code>Locale</code> which will be used when
  45. * no <code>Locale</code> is passed to a method.</p>
  46. *
  47. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  48. *
  49. * @see LocaleConvertUtilsBean#setDefaultLocale(Locale)
  50. */
  51. public static void setDefaultLocale(Locale locale) {
  52. LocaleConvertUtilsBean.getInstance().setDefaultLocale(locale);
  53. }
  54. /**
  55. * <p>Gets applyLocalized.</p>
  56. *
  57. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  58. *
  59. * @see LocaleConvertUtilsBean#getApplyLocalized()
  60. */
  61. public static boolean getApplyLocalized() {
  62. return LocaleConvertUtilsBean.getInstance().getApplyLocalized();
  63. }
  64. /**
  65. * <p>Sets applyLocalized.</p>
  66. *
  67. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  68. *
  69. * @see LocaleConvertUtilsBean#setApplyLocalized(boolean)
  70. */
  71. public static void setApplyLocalized(boolean newApplyLocalized) {
  72. LocaleConvertUtilsBean.getInstance().setApplyLocalized(newApplyLocalized);
  73. }
  74. // --------------------------------------------------------- Methods
  75. /**
  76. * <p>Convert the specified locale-sensitive value into a String.</p>
  77. *
  78. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  79. *
  80. * @see LocaleConvertUtilsBean#convert(Object)
  81. */
  82. public static String convert(Object value) {
  83. return LocaleConvertUtilsBean.getInstance().convert(value);
  84. }
  85. /**
  86. * <p>Convert the specified locale-sensitive value into a String
  87. * using the convertion pattern.</p>
  88. *
  89. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  90. *
  91. * @see LocaleConvertUtilsBean#convert(Object, String)
  92. */
  93. public static String convert(Object value, String pattern) {
  94. return LocaleConvertUtilsBean.getInstance().convert(value, pattern);
  95. }
  96. /**
  97. * <p>Convert the specified locale-sensitive value into a String
  98. * using the paticular convertion pattern.</p>
  99. *
  100. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  101. *
  102. * @see LocaleConvertUtilsBean#convert(Object, Locale, String)
  103. */
  104. public static String convert(Object value, Locale locale, String pattern) {
  105. return LocaleConvertUtilsBean.getInstance().convert(value, locale, pattern);
  106. }
  107. /**
  108. * <p>Convert the specified value to an object of the specified class (if
  109. * possible). Otherwise, return a String representation of the value.</p>
  110. *
  111. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  112. *
  113. * @see LocaleConvertUtilsBean#convert(String, Class)
  114. */
  115. public static Object convert(String value, Class clazz) {
  116. return LocaleConvertUtilsBean.getInstance().convert(value, clazz);
  117. }
  118. /**
  119. * <p>Convert the specified value to an object of the specified class (if
  120. * possible) using the convertion pattern. Otherwise, return a String
  121. * representation of the value.</p>
  122. *
  123. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  124. *
  125. * @see LocaleConvertUtilsBean#convert(String, Class, String)
  126. */
  127. public static Object convert(String value, Class clazz, String pattern) {
  128. return LocaleConvertUtilsBean.getInstance().convert(value, clazz, pattern);
  129. }
  130. /**
  131. * <p>Convert the specified value to an object of the specified class (if
  132. * possible) using the convertion pattern. Otherwise, return a String
  133. * representation of the value.</p>
  134. *
  135. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  136. *
  137. * @see LocaleConvertUtilsBean#convert(String, Class, Locale, String)
  138. */
  139. public static Object convert(String value, Class clazz, Locale locale, String pattern) {
  140. return LocaleConvertUtilsBean.getInstance().convert(value, clazz, locale, pattern);
  141. }
  142. /**
  143. * <p>Convert an array of specified values to an array of objects of the
  144. * specified class (if possible) using the convertion pattern.</p>
  145. *
  146. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  147. *
  148. * @see LocaleConvertUtilsBean#convert(String[], Class, String)
  149. */
  150. public static Object convert(String values[], Class clazz, String pattern) {
  151. return LocaleConvertUtilsBean.getInstance().convert(values, clazz, pattern);
  152. }
  153. /**
  154. * <p>Convert an array of specified values to an array of objects of the
  155. * specified class (if possible).</p>
  156. *
  157. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  158. *
  159. * @see LocaleConvertUtilsBean#convert(String[], Class)
  160. */
  161. public static Object convert(String values[], Class clazz) {
  162. return LocaleConvertUtilsBean.getInstance().convert(values, clazz);
  163. }
  164. /**
  165. * <p>Convert an array of specified values to an array of objects of the
  166. * specified class (if possible) using the convertion pattern.</p>
  167. *
  168. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  169. *
  170. * @see LocaleConvertUtilsBean#convert(String[], Class, Locale, String)
  171. */
  172. public static Object convert(String values[], Class clazz, Locale locale, String pattern) {
  173. return LocaleConvertUtilsBean.getInstance().convert(values, clazz, locale, pattern);
  174. }
  175. /**
  176. * <p>Register a custom {@link LocaleConverter} for the specified destination
  177. * <code>Class</code>, replacing any previously registered converter.</p>
  178. *
  179. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  180. *
  181. * @see LocaleConvertUtilsBean#register(LocaleConverter, Class, Locale)
  182. */
  183. public static void register(LocaleConverter converter, Class clazz, Locale locale) {
  184. LocaleConvertUtilsBean.getInstance().register(converter, clazz, locale);
  185. }
  186. /**
  187. * <p>Remove any registered {@link LocaleConverter}.</p>
  188. *
  189. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  190. *
  191. * @see LocaleConvertUtilsBean#deregister()
  192. */
  193. public static void deregister() {
  194. LocaleConvertUtilsBean.getInstance().deregister();
  195. }
  196. /**
  197. * <p>Remove any registered {@link LocaleConverter} for the specified locale.</p>
  198. *
  199. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  200. *
  201. * @see LocaleConvertUtilsBean#deregister(Locale)
  202. */
  203. public static void deregister(Locale locale) {
  204. LocaleConvertUtilsBean.getInstance().deregister(locale);
  205. }
  206. /**
  207. * <p>Remove any registered {@link LocaleConverter} for the specified locale and Class.</p>
  208. *
  209. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  210. *
  211. * @see LocaleConvertUtilsBean#deregister(Class, Locale)
  212. */
  213. public static void deregister(Class clazz, Locale locale) {
  214. LocaleConvertUtilsBean.getInstance().deregister(clazz, locale);
  215. }
  216. /**
  217. * <p>Look up and return any registered {@link LocaleConverter} for the specified
  218. * destination class and locale; if there is no registered Converter, return
  219. * <code>null</code>.</p>
  220. *
  221. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  222. *
  223. * @see LocaleConvertUtilsBean#lookup(Class, Locale)
  224. */
  225. public static LocaleConverter lookup(Class clazz, Locale locale) {
  226. return LocaleConvertUtilsBean.getInstance().lookup(clazz, locale);
  227. }
  228. /**
  229. * <p>Look up and return any registered FastHashMap instance for the specified locale.</p>
  230. *
  231. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  232. *
  233. * @see LocaleConvertUtilsBean#lookup(Locale)
  234. */
  235. protected static FastHashMap lookup(Locale locale) {
  236. return LocaleConvertUtilsBean.getInstance().lookup(locale);
  237. }
  238. /**
  239. * <p>Create all {@link LocaleConverter} types for specified locale.</p>
  240. *
  241. * <p>For more details see <code>LocaleConvertUtilsBean</code></p>
  242. *
  243. * @see LocaleConvertUtilsBean#create(Locale)
  244. */
  245. protected static FastHashMap create(Locale locale) {
  246. return LocaleConvertUtilsBean.getInstance().create(locale);
  247. }
  248. }