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 java.util.List;
  18. import org.apache.commons.beanutils.ConversionException;
  19. import org.apache.commons.beanutils.Converter;
  20. /**
  21. * <p>Standard {@link Converter} implementation that converts an incoming
  22. * String into a primitive array of long. On a conversion failure, returns
  23. * a specified default value or throws a {@link ConversionException} depending
  24. * on how this instance is constructed.</p>
  25. *
  26. * @author Craig R. McClanahan
  27. * @version $Revision: 1.7 $ $Date: 2004/02/28 13:18:34 $
  28. * @since 1.4
  29. */
  30. public final class LongArrayConverter extends AbstractArrayConverter {
  31. // ----------------------------------------------------------- Constructors
  32. /**
  33. * Create a {@link Converter} that will throw a {@link ConversionException}
  34. * if a conversion error occurs.
  35. */
  36. public LongArrayConverter() {
  37. this.defaultValue = null;
  38. this.useDefault = false;
  39. }
  40. /**
  41. * Create a {@link Converter} that will return the specified default value
  42. * if a conversion error occurs.
  43. *
  44. * @param defaultValue The default value to be returned
  45. */
  46. public LongArrayConverter(Object defaultValue) {
  47. this.defaultValue = defaultValue;
  48. this.useDefault = true;
  49. }
  50. // ------------------------------------------------------- Static Variables
  51. /**
  52. * <p>Model object for type comparisons.</p>
  53. */
  54. private static long model[] = new long[0];
  55. // --------------------------------------------------------- Public Methods
  56. /**
  57. * Convert the specified input object into an output object of the
  58. * specified type.
  59. *
  60. * @param type Data type to which this value should be converted
  61. * @param value The input value to be converted
  62. *
  63. * @exception ConversionException if conversion cannot be performed
  64. * successfully
  65. */
  66. public Object convert(Class type, Object value) {
  67. // Deal with a null value
  68. if (value == null) {
  69. if (useDefault) {
  70. return (defaultValue);
  71. } else {
  72. throw new ConversionException("No value specified");
  73. }
  74. }
  75. // Deal with the no-conversion-needed case
  76. if (model.getClass() == value.getClass()) {
  77. return (value);
  78. }
  79. // Deal with input value as a String array
  80. if (strings.getClass() == value.getClass()) {
  81. try {
  82. String values[] = (String[]) value;
  83. long results[] = new long[values.length];
  84. for (int i = 0; i < values.length; i++) {
  85. results[i] = Long.parseLong(values[i]);
  86. }
  87. return (results);
  88. } catch (Exception e) {
  89. if (useDefault) {
  90. return (defaultValue);
  91. } else {
  92. throw new ConversionException(value.toString(), e);
  93. }
  94. }
  95. }
  96. // Parse the input value as a String into elements
  97. // and convert to the appropriate type
  98. try {
  99. List list = parseElements(value.toString());
  100. long results[] = new long[list.size()];
  101. for (int i = 0; i < results.length; i++) {
  102. results[i] = Long.parseLong((String) list.get(i));
  103. }
  104. return (results);
  105. } catch (Exception e) {
  106. if (useDefault) {
  107. return (defaultValue);
  108. } else {
  109. throw new ConversionException(value.toString(), e);
  110. }
  111. }
  112. }
  113. }