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;
  17. /**
  18. * <p>Utility methods for converting String scalar values to objects of the
  19. * specified Class, String arrays to arrays of the specified Class.</p>
  20. *
  21. * <p>For more details, see <code>ConvertUtilsBean</code> which provides the
  22. * implementations for these methods.</p>
  23. *
  24. * @author Craig R. McClanahan
  25. * @author Ralph Schaer
  26. * @author Chris Audley
  27. * @version $Revision: 1.17 $ $Date: 2004/02/28 13:18:33 $
  28. * @see ConvertUtilsBean
  29. */
  30. public class ConvertUtils {
  31. // ------------------------------------------------------ Static Properties
  32. /**
  33. * Gets the default value for Boolean conversions.
  34. * @deprecated Register replacement converters for Boolean.TYPE and
  35. * Boolean.class instead
  36. */
  37. public static boolean getDefaultBoolean() {
  38. return (ConvertUtilsBean.getInstance().getDefaultBoolean());
  39. }
  40. /**
  41. * Sets the default value for Boolean conversions.
  42. * @deprecated Register replacement converters for Boolean.TYPE and
  43. * Boolean.class instead
  44. */
  45. public static void setDefaultBoolean(boolean newDefaultBoolean) {
  46. ConvertUtilsBean.getInstance().setDefaultBoolean(newDefaultBoolean);
  47. }
  48. /**
  49. * Gets the default value for Byte conversions.
  50. * @deprecated Register replacement converters for Byte.TYPE and
  51. * Byte.class instead
  52. */
  53. public static byte getDefaultByte() {
  54. return ConvertUtilsBean.getInstance().getDefaultByte();
  55. }
  56. /**
  57. * Sets the default value for Byte conversions.
  58. * @deprecated Register replacement converters for Byte.TYPE and
  59. * Byte.class instead
  60. */
  61. public static void setDefaultByte(byte newDefaultByte) {
  62. ConvertUtilsBean.getInstance().setDefaultByte(newDefaultByte);
  63. }
  64. /**
  65. * Gets the default value for Character conversions.
  66. * @deprecated Register replacement converters for Character.TYPE and
  67. * Character.class instead
  68. */
  69. public static char getDefaultCharacter() {
  70. return ConvertUtilsBean.getInstance().getDefaultCharacter();
  71. }
  72. /**
  73. * Sets the default value for Character conversions.
  74. * @deprecated Register replacement converters for Character.TYPE and
  75. * Character.class instead
  76. */
  77. public static void setDefaultCharacter(char newDefaultCharacter) {
  78. ConvertUtilsBean.getInstance().setDefaultCharacter(newDefaultCharacter);
  79. }
  80. /**
  81. * Gets the default value for Double conversions.
  82. * @deprecated Register replacement converters for Double.TYPE and
  83. * Double.class instead
  84. */
  85. public static double getDefaultDouble() {
  86. return ConvertUtilsBean.getInstance().getDefaultDouble();
  87. }
  88. /**
  89. * Sets the default value for Double conversions.
  90. * @deprecated Register replacement converters for Double.TYPE and
  91. * Double.class instead
  92. */
  93. public static void setDefaultDouble(double newDefaultDouble) {
  94. ConvertUtilsBean.getInstance().setDefaultDouble(newDefaultDouble);
  95. }
  96. /**
  97. * Get the default value for Float conversions.
  98. * @deprecated Register replacement converters for Float.TYPE and
  99. * Float.class instead
  100. */
  101. public static float getDefaultFloat() {
  102. return ConvertUtilsBean.getInstance().getDefaultFloat();
  103. }
  104. /**
  105. * Sets the default value for Float conversions.
  106. * @deprecated Register replacement converters for Float.TYPE and
  107. * Float.class instead
  108. */
  109. public static void setDefaultFloat(float newDefaultFloat) {
  110. ConvertUtilsBean.getInstance().setDefaultFloat(newDefaultFloat);
  111. }
  112. /**
  113. * Gets the default value for Integer conversions.
  114. * @deprecated Register replacement converters for Integer.TYPE and
  115. * Integer.class instead
  116. */
  117. public static int getDefaultInteger() {
  118. return ConvertUtilsBean.getInstance().getDefaultInteger();
  119. }
  120. /**
  121. * Sets the default value for Integer conversions.
  122. * @deprecated Register replacement converters for Integer.TYPE and
  123. * Integer.class instead
  124. */
  125. public static void setDefaultInteger(int newDefaultInteger) {
  126. ConvertUtilsBean.getInstance().setDefaultInteger(newDefaultInteger);
  127. }
  128. /**
  129. * Gets the default value for Long conversions.
  130. * @deprecated Register replacement converters for Long.TYPE and
  131. * Long.class instead
  132. */
  133. public static long getDefaultLong() {
  134. return (ConvertUtilsBean.getInstance().getDefaultLong());
  135. }
  136. /**
  137. * Sets the default value for Long conversions.
  138. * @deprecated Register replacement converters for Long.TYPE and
  139. * Long.class instead
  140. */
  141. public static void setDefaultLong(long newDefaultLong) {
  142. ConvertUtilsBean.getInstance().setDefaultLong(newDefaultLong);
  143. }
  144. /**
  145. * Gets the default value for Short conversions.
  146. * @deprecated Register replacement converters for Short.TYPE and
  147. * Short.class instead
  148. */
  149. public static short getDefaultShort() {
  150. return ConvertUtilsBean.getInstance().getDefaultShort();
  151. }
  152. /**
  153. * Sets the default value for Short conversions.
  154. * @deprecated Register replacement converters for Short.TYPE and
  155. * Short.class instead
  156. */
  157. public static void setDefaultShort(short newDefaultShort) {
  158. ConvertUtilsBean.getInstance().setDefaultShort(newDefaultShort);
  159. }
  160. // --------------------------------------------------------- Public Classes
  161. /**
  162. * <p>Convert the specified value into a String.</p>
  163. *
  164. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  165. *
  166. * @see ConvertUtilsBean#convert(Object)
  167. */
  168. public static String convert(Object value) {
  169. return ConvertUtilsBean.getInstance().convert(value);
  170. }
  171. /**
  172. * <p>Convert the specified value to an object of the specified class (if
  173. * possible). Otherwise, return a String representation of the value.</p>
  174. *
  175. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  176. *
  177. * @see ConvertUtilsBean#convert(String, Class)
  178. */
  179. public static Object convert(String value, Class clazz) {
  180. return ConvertUtilsBean.getInstance().convert(value, clazz);
  181. }
  182. /**
  183. * <p>Convert an array of specified values to an array of objects of the
  184. * specified class (if possible).</p>
  185. *
  186. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  187. *
  188. * @see ConvertUtilsBean#convert(String[], Class)
  189. */
  190. public static Object convert(String values[], Class clazz) {
  191. return ConvertUtilsBean.getInstance().convert(values, clazz);
  192. }
  193. /**
  194. * <p>Remove all registered {@link Converter}s, and re-establish the
  195. * standard Converters.</p>
  196. *
  197. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  198. *
  199. * @see ConvertUtilsBean#deregister()
  200. */
  201. public static void deregister() {
  202. ConvertUtilsBean.getInstance().deregister();
  203. }
  204. /**
  205. * <p>Remove any registered {@link Converter} for the specified destination
  206. * <code>Class</code>.</p>
  207. *
  208. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  209. *
  210. * @see ConvertUtilsBean#deregister(Class)
  211. */
  212. public static void deregister(Class clazz) {
  213. ConvertUtilsBean.getInstance().deregister(clazz);
  214. }
  215. /**
  216. * <p>Look up and return any registered {@link Converter} for the specified
  217. * destination class; if there is no registered Converter, return
  218. * <code>null</code>.</p>
  219. *
  220. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  221. *
  222. * @see ConvertUtilsBean#lookup(Class)
  223. */
  224. public static Converter lookup(Class clazz) {
  225. return ConvertUtilsBean.getInstance().lookup(clazz);
  226. }
  227. /**
  228. * <p>Register a custom {@link Converter} for the specified destination
  229. * <code>Class</code>, replacing any previously registered Converter.</p>
  230. *
  231. * <p>For more details see <code>ConvertUtilsBean</code>.</p>
  232. *
  233. * @see ConvertUtilsBean#register(Converter, Class)
  234. */
  235. public static void register(Converter converter, Class clazz) {
  236. ConvertUtilsBean.getInstance().register(converter, clazz);
  237. }
  238. }