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.converters;
  17. import org.apache.commons.beanutils.ConversionException;
  18. import org.apache.commons.beanutils.Converter;
  19. /**
  20. * <p>Standard {@link Converter} implementation that converts an incoming
  21. * String into a <code>java.lang.Byte</code> object, optionally using a
  22. * default value or throwing a {@link ConversionException} if a conversion
  23. * error occurs.</p>
  24. *
  25. * @author Craig R. McClanahan
  26. * @version $Revision: 1.9 $ $Date: 2004/02/28 13:18:34 $
  27. * @since 1.3
  28. */
  29. public final class ByteConverter implements Converter {
  30. // ----------------------------------------------------------- Constructors
  31. /**
  32. * Create a {@link Converter} that will throw a {@link ConversionException}
  33. * if a conversion error occurs.
  34. */
  35. public ByteConverter() {
  36. this.defaultValue = null;
  37. this.useDefault = false;
  38. }
  39. /**
  40. * Create a {@link Converter} that will return the specified default value
  41. * if a conversion error occurs.
  42. *
  43. * @param defaultValue The default value to be returned
  44. */
  45. public ByteConverter(Object defaultValue) {
  46. this.defaultValue = defaultValue;
  47. this.useDefault = true;
  48. }
  49. // ----------------------------------------------------- Instance Variables
  50. /**
  51. * The default value specified to our Constructor, if any.
  52. */
  53. private Object defaultValue = null;
  54. /**
  55. * Should we return the default value on conversion errors?
  56. */
  57. private boolean useDefault = true;
  58. // --------------------------------------------------------- Public Methods
  59. /**
  60. * Convert the specified input object into an output object of the
  61. * specified type.
  62. *
  63. * @param type Data type to which this value should be converted
  64. * @param value The input value to be converted
  65. *
  66. * @exception ConversionException if conversion cannot be performed
  67. * successfully
  68. */
  69. public Object convert(Class type, Object value) {
  70. if (value == null) {
  71. if (useDefault) {
  72. return (defaultValue);
  73. } else {
  74. throw new ConversionException("No value specified");
  75. }
  76. }
  77. // System.err.println("VALUE=" + value + ", TYPE=" + value.getClass().getName());
  78. if (value instanceof Byte) {
  79. return (value);
  80. } else if (value instanceof Number) {
  81. return new Byte(((Number)value).byteValue());
  82. }
  83. try {
  84. return (new Byte(value.toString()));
  85. } catch (Exception e) {
  86. if (useDefault) {
  87. return (defaultValue);
  88. } else {
  89. throw new ConversionException(e);
  90. }
  91. }
  92. }
  93. }