| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ShortArrayConverter |
|
| 7.333333333333333;7.333 |
| 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 | * @author Craig R. McClanahan | |
| 33 | * @version $Revision: 556229 $ $Date: 2007-07-14 02:11:19 -0400 (Sat, 14 Jul 2007) $ | |
| 34 | * @since 1.4 | |
| 35 | * @deprecated Replaced by the new {@link ArrayConverter} implementation | |
| 36 | */ | |
| 37 | ||
| 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 | 0 | public ShortArrayConverter() { |
| 49 | ||
| 50 | 0 | this.defaultValue = null; |
| 51 | 0 | this.useDefault = false; |
| 52 | ||
| 53 | 0 | } |
| 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 | 0 | public ShortArrayConverter(Object defaultValue) { |
| 63 | ||
| 64 | 0 | this.defaultValue = defaultValue; |
| 65 | 0 | this.useDefault = true; |
| 66 | ||
| 67 | 0 | } |
| 68 | ||
| 69 | ||
| 70 | // ------------------------------------------------------- Static Variables | |
| 71 | ||
| 72 | ||
| 73 | /** | |
| 74 | * <p>Model object for type comparisons.</p> | |
| 75 | */ | |
| 76 | 0 | 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 | * @exception ConversionException if conversion cannot be performed | |
| 91 | * successfully | |
| 92 | */ | |
| 93 | public Object convert(Class type, Object value) { | |
| 94 | ||
| 95 | // Deal with a null value | |
| 96 | 0 | if (value == null) { |
| 97 | 0 | if (useDefault) { |
| 98 | 0 | return (defaultValue); |
| 99 | } else { | |
| 100 | 0 | throw new ConversionException("No value specified"); |
| 101 | } | |
| 102 | } | |
| 103 | ||
| 104 | // Deal with the no-conversion-needed case | |
| 105 | 0 | if (MODEL.getClass() == value.getClass()) { |
| 106 | 0 | return (value); |
| 107 | } | |
| 108 | ||
| 109 | // Deal with input value as a String array | |
| 110 | 0 | if (strings.getClass() == value.getClass()) { |
| 111 | try { | |
| 112 | 0 | String[] values = (String[]) value; |
| 113 | 0 | short[] results = new short[values.length]; |
| 114 | 0 | for (int i = 0; i < values.length; i++) { |
| 115 | 0 | results[i] = Short.parseShort(values[i]); |
| 116 | } | |
| 117 | 0 | return (results); |
| 118 | 0 | } catch (Exception e) { |
| 119 | 0 | if (useDefault) { |
| 120 | 0 | return (defaultValue); |
| 121 | } else { | |
| 122 | 0 | throw new ConversionException(value.toString(), e); |
| 123 | } | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | // Parse the input value as a String into elements | |
| 128 | // and convert to the appropriate type | |
| 129 | try { | |
| 130 | 0 | List list = parseElements(value.toString()); |
| 131 | 0 | short[] results = new short[list.size()]; |
| 132 | 0 | for (int i = 0; i < results.length; i++) { |
| 133 | 0 | results[i] = Short.parseShort((String) list.get(i)); |
| 134 | } | |
| 135 | 0 | return (results); |
| 136 | 0 | } catch (Exception e) { |
| 137 | 0 | if (useDefault) { |
| 138 | 0 | return (defaultValue); |
| 139 | } else { | |
| 140 | 0 | throw new ConversionException(value.toString(), e); |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | } | |
| 145 | ||
| 146 | ||
| 147 | } |