View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 short.  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   * @version $Id$
33   * @since 1.4
34   * @deprecated Replaced by the new {@link ArrayConverter} implementation
35   */
36  
37  @Deprecated
38  public final class ShortArrayConverter extends AbstractArrayConverter {
39  
40  
41      // ----------------------------------------------------------- Constructors
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 ShortArrayConverter() {
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 ShortArrayConverter(final Object defaultValue) {
63  
64          this.defaultValue = defaultValue;
65          this.useDefault = true;
66  
67      }
68  
69  
70      // ------------------------------------------------------- Static Variables
71  
72  
73      /**
74       * <p>Model object for type comparisons.</p>
75       */
76      private static final short[] MODEL = new short[0];
77  
78  
79      // --------------------------------------------------------- Public Methods
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       * @throws ConversionException if conversion cannot be performed
91       *  successfully
92       */
93      @Override
94      public Object convert(final Class type, final Object value) {
95  
96          // Deal with a null value
97          if (value == null) {
98              if (useDefault) {
99                  return (defaultValue);
100             } else {
101                 throw new ConversionException("No value specified");
102             }
103         }
104 
105         // Deal with the no-conversion-needed case
106         if (MODEL.getClass() == value.getClass()) {
107             return (value);
108         }
109 
110         // Deal with input value as a String array
111         if (strings.getClass() == value.getClass()) {
112             try {
113                 final String[] values = (String[]) value;
114                 final short[] results = new short[values.length];
115                 for (int i = 0; i < values.length; i++) {
116                     results[i] = Short.parseShort(values[i]);
117                 }
118                 return (results);
119             } catch (final Exception e) {
120                 if (useDefault) {
121                     return (defaultValue);
122                 } else {
123                     throw new ConversionException(value.toString(), e);
124                 }
125             }
126         }
127 
128         // Parse the input value as a String into elements
129         // and convert to the appropriate type
130         try {
131             final List list = parseElements(value.toString());
132             final short[] results = new short[list.size()];
133             for (int i = 0; i < results.length; i++) {
134                 results[i] = Short.parseShort((String) list.get(i));
135             }
136             return (results);
137         } catch (final Exception e) {
138             if (useDefault) {
139                 return (defaultValue);
140             } else {
141                 throw new ConversionException(value.toString(), e);
142             }
143         }
144 
145     }
146 
147 
148 }