1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils.converters;
20
21
22 import java.util.List;
23 import org.apache.commons.beanutils.ConversionException;
24
25
26 /***
27 * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
28 * String into a primitive array of long. On a conversion failure, returns
29 * a specified default value or throws a {@link ConversionException} depending
30 * on how this instance is constructed.</p>
31 *
32 * @author Craig R. McClanahan
33 * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
34 * @since 1.4
35 * @deprecated Replaced by the new {@link ArrayConverter} implementation
36 */
37
38 public final class LongArrayConverter extends AbstractArrayConverter {
39
40
41
42
43
44 /***
45 * Create a {@link org.apache.commons.beanutils.Converter} that will throw
46 * a {@link ConversionException} if a conversion error occurs.
47 */
48 public LongArrayConverter() {
49
50 this.defaultValue = null;
51 this.useDefault = false;
52
53 }
54
55
56 /***
57 * Create a {@link org.apache.commons.beanutils.Converter} that will return
58 * the specified default value if a conversion error occurs.
59 *
60 * @param defaultValue The default value to be returned
61 */
62 public LongArrayConverter(Object defaultValue) {
63
64 this.defaultValue = defaultValue;
65 this.useDefault = true;
66
67 }
68
69
70
71
72
73 /***
74 * <p>Model object for type comparisons.</p>
75 */
76 private static final long[] MODEL = new long[0];
77
78
79
80
81
82 /***
83 * Convert the specified input object into an output object of the
84 * specified type.
85 *
86 * @param type Data type to which this value should be converted
87 * @param value The input value to be converted
88 * @return the converted value
89 *
90 * @exception ConversionException if conversion cannot be performed
91 * successfully
92 */
93 public Object convert(Class type, Object value) {
94
95
96 if (value == null) {
97 if (useDefault) {
98 return (defaultValue);
99 } else {
100 throw new ConversionException("No value specified");
101 }
102 }
103
104
105 if (MODEL.getClass() == value.getClass()) {
106 return (value);
107 }
108
109
110 if (strings.getClass() == value.getClass()) {
111 try {
112 String[] values = (String[]) value;
113 long[] results = new long[values.length];
114 for (int i = 0; i < values.length; i++) {
115 results[i] = Long.parseLong(values[i]);
116 }
117 return (results);
118 } catch (Exception e) {
119 if (useDefault) {
120 return (defaultValue);
121 } else {
122 throw new ConversionException(value.toString(), e);
123 }
124 }
125 }
126
127
128
129 try {
130 List list = parseElements(value.toString());
131 long[] results = new long[list.size()];
132 for (int i = 0; i < results.length; i++) {
133 results[i] = Long.parseLong((String) list.get(i));
134 }
135 return (results);
136 } catch (Exception e) {
137 if (useDefault) {
138 return (defaultValue);
139 } else {
140 throw new ConversionException(value.toString(), e);
141 }
142 }
143
144 }
145
146
147 }