| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BooleanArrayConverter |
|
| 5.75;5.75 |
| 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 boolean. 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 | * <p>By default, the values to be converted are expected to be those | |
| 33 | * recognised by a default instance of BooleanConverter. A customised | |
| 34 | * BooleanConverter can be provided in order to recognise alternative values | |
| 35 | * as true/false. </p> | |
| 36 | * | |
| 37 | * @author Craig R. McClanahan | |
| 38 | * @version $Revision: 690380 $ $Date: 2008-08-29 16:04:38 -0400 (Fri, 29 Aug 2008) $ | |
| 39 | * @since 1.4 | |
| 40 | * @deprecated Replaced by the new {@link ArrayConverter} implementation | |
| 41 | */ | |
| 42 | ||
| 43 | public final class BooleanArrayConverter extends AbstractArrayConverter { | |
| 44 | ||
| 45 | ||
| 46 | // ----------------------------------------------------------- Constructors | |
| 47 | ||
| 48 | ||
| 49 | /** | |
| 50 | * Create a {@link org.apache.commons.beanutils.Converter} that will throw | |
| 51 | * a {@link ConversionException} if a conversion error occurs. | |
| 52 | * | |
| 53 | * <p>Conversion of strings to boolean values will be done via a default | |
| 54 | * instance of class BooleanConverter.</p> | |
| 55 | */ | |
| 56 | public BooleanArrayConverter() { | |
| 57 | ||
| 58 | 4 | super(); |
| 59 | 4 | this.booleanConverter = DEFAULT_CONVERTER; |
| 60 | ||
| 61 | 4 | } |
| 62 | ||
| 63 | ||
| 64 | /** | |
| 65 | * Create a {@link org.apache.commons.beanutils.Converter} that will return | |
| 66 | * the specified default value if a conversion error occurs. | |
| 67 | * | |
| 68 | * <p>Conversion of strings to boolean values will be done via a default | |
| 69 | * instance of class BooleanConverter.</p> | |
| 70 | * | |
| 71 | * @param defaultValue The default value to be returned | |
| 72 | */ | |
| 73 | public BooleanArrayConverter(Object defaultValue) { | |
| 74 | ||
| 75 | 2 | super(defaultValue); |
| 76 | 2 | this.booleanConverter = DEFAULT_CONVERTER; |
| 77 | ||
| 78 | 2 | } |
| 79 | ||
| 80 | ||
| 81 | /** | |
| 82 | * Create a {@link org.apache.commons.beanutils.Converter} that will return | |
| 83 | * the specified default value if a conversion error occurs. | |
| 84 | * | |
| 85 | * <p>Conversion of strings to boolean values will be done via the | |
| 86 | * specified converter.</p> | |
| 87 | * | |
| 88 | * @param converter is the converter object that will be used to | |
| 89 | * convert each input string-value into a boolean. | |
| 90 | * | |
| 91 | * @param defaultValue is the default value to be returned by method | |
| 92 | * convert if conversion fails; null is a valid default value. See the | |
| 93 | * documentation for method "convert" for more information. | |
| 94 | * The value BooleanArrayConverter.NO_DEFAULT may be passed here to | |
| 95 | * specify that an exception should be thrown on conversion failure. | |
| 96 | * | |
| 97 | */ | |
| 98 | public BooleanArrayConverter(BooleanConverter converter, Object defaultValue) { | |
| 99 | ||
| 100 | 3 | super(defaultValue); |
| 101 | 3 | this.booleanConverter = converter; |
| 102 | ||
| 103 | 3 | } |
| 104 | ||
| 105 | // ------------------------------------------------------- Static Variables | |
| 106 | ||
| 107 | /** | |
| 108 | * Type which this class converts its input to. This value can be | |
| 109 | * used as a parameter to the ConvertUtils.register method. | |
| 110 | * @since 1.8.0 | |
| 111 | */ | |
| 112 | 1 | public static final Class MODEL = new boolean[0].getClass(); |
| 113 | ||
| 114 | /** | |
| 115 | * The converter that all instances of this class will use to | |
| 116 | * do individual string->boolean conversions, unless overridden | |
| 117 | * in the constructor. | |
| 118 | */ | |
| 119 | 1 | private static final BooleanConverter DEFAULT_CONVERTER |
| 120 | = new BooleanConverter(); | |
| 121 | ||
| 122 | // ---------------------------------------------------- Instance Variables | |
| 123 | ||
| 124 | /** | |
| 125 | * This object is used to perform the conversion of individual strings | |
| 126 | * into Boolean/boolean values. | |
| 127 | */ | |
| 128 | protected final BooleanConverter booleanConverter; | |
| 129 | ||
| 130 | // --------------------------------------------------------- Public Methods | |
| 131 | ||
| 132 | ||
| 133 | /** | |
| 134 | * Convert the specified input object into an output object of type | |
| 135 | * array-of-boolean. | |
| 136 | * | |
| 137 | * <p>If the input value is null, then the default value specified in the | |
| 138 | * constructor is returned. If no such value was provided, then a | |
| 139 | * ConversionException is thrown instead.</p> | |
| 140 | * | |
| 141 | * <p>If the input value is of type String[] then the returned array shall | |
| 142 | * be of the same size as this array, with a true or false value in each | |
| 143 | * array element depending on the result of applying method | |
| 144 | * BooleanConverter.convert to each string.</p> | |
| 145 | * | |
| 146 | * <p>For all other types of value, the object's toString method is | |
| 147 | * expected to return a string containing a comma-separated list of | |
| 148 | * values, eg "true, false, true". See the documentation for | |
| 149 | * {@link AbstractArrayConverter#parseElements} for more information on | |
| 150 | * the exact formats supported.</p> | |
| 151 | * | |
| 152 | * <p>If the result of value.toString() cannot be split into separate | |
| 153 | * words, then the default value is also returned (or an exception thrown). | |
| 154 | * </p> | |
| 155 | * | |
| 156 | * <p>If any of the elements in the value array (or the elements resulting | |
| 157 | * from splitting up value.toString) are not recognised by the | |
| 158 | * BooleanConverter associated with this object, then what happens depends | |
| 159 | * on whether that BooleanConverter has a default value or not: if it does, | |
| 160 | * then that unrecognised element is converted into the BooleanConverter's | |
| 161 | * default value. If the BooleanConverter does <i>not</i> have a default | |
| 162 | * value, then the default value for this object is returned as the | |
| 163 | * <i>complete</i> conversion result (not just for the element), or an | |
| 164 | * exception is thrown if this object has no default value defined.</p> | |
| 165 | * | |
| 166 | * @param type is the type to which this value should be converted. In the | |
| 167 | * case of this BooleanArrayConverter class, this value is ignored. | |
| 168 | * | |
| 169 | * @param value is the input value to be converted. | |
| 170 | * | |
| 171 | * @return an object of type boolean[], or the default value if there was | |
| 172 | * any sort of error during conversion and the constructor | |
| 173 | * was provided with a default value. | |
| 174 | * | |
| 175 | * @exception ConversionException if conversion cannot be performed | |
| 176 | * successfully and the constructor was not provided with a default | |
| 177 | * value to return on conversion failure. | |
| 178 | * | |
| 179 | * @exception NullPointerException if value is an array, and any of the | |
| 180 | * array elements are null. | |
| 181 | */ | |
| 182 | public Object convert(Class type, Object value) { | |
| 183 | ||
| 184 | // Deal with a null value | |
| 185 | 14 | if (value == null) { |
| 186 | 0 | if (useDefault) { |
| 187 | 0 | return (defaultValue); |
| 188 | } else { | |
| 189 | 0 | throw new ConversionException("No value specified"); |
| 190 | } | |
| 191 | } | |
| 192 | ||
| 193 | // Deal with the no-conversion-needed case | |
| 194 | 14 | if (MODEL == value.getClass()) { |
| 195 | 0 | return (value); |
| 196 | } | |
| 197 | ||
| 198 | // Deal with input value as a String array | |
| 199 | // | |
| 200 | // TODO: use if (value.getClass().isArray() instead... | |
| 201 | // this requires casting to Object[], then using values[i].toString() | |
| 202 | 14 | if (strings.getClass() == value.getClass()) { |
| 203 | try { | |
| 204 | 1 | String[] values = (String[]) value; |
| 205 | 1 | boolean[] results = new boolean[values.length]; |
| 206 | 9 | for (int i = 0; i < values.length; i++) { |
| 207 | 8 | String stringValue = values[i]; |
| 208 | 8 | Object result = booleanConverter.convert(Boolean.class, stringValue); |
| 209 | 8 | results[i] = ((Boolean) result).booleanValue(); |
| 210 | } | |
| 211 | 1 | return (results); |
| 212 | 0 | } catch (Exception e) { |
| 213 | 0 | if (useDefault) { |
| 214 | 0 | return (defaultValue); |
| 215 | } else { | |
| 216 | 0 | throw new ConversionException(value.toString(), e); |
| 217 | } | |
| 218 | } | |
| 219 | } | |
| 220 | ||
| 221 | // We only get here if the input value is not of type String[]. | |
| 222 | // In this case, we assume value.toString() returns a comma-separated | |
| 223 | // sequence of values; see method AbstractArrayConverter.parseElements | |
| 224 | // for more information. | |
| 225 | try { | |
| 226 | 13 | List list = parseElements(value.toString()); |
| 227 | 13 | boolean[] results = new boolean[list.size()]; |
| 228 | 43 | for (int i = 0; i < results.length; i++) { |
| 229 | 35 | String stringValue = (String) list.get(i); |
| 230 | 35 | Object result = booleanConverter.convert(Boolean.class, stringValue); |
| 231 | 30 | results[i] = ((Boolean) result).booleanValue(); |
| 232 | } | |
| 233 | 8 | return (results); |
| 234 | 5 | } catch (Exception e) { |
| 235 | 5 | if (useDefault) { |
| 236 | 2 | return (defaultValue); |
| 237 | } else { | |
| 238 | 3 | throw new ConversionException(value.toString(), e); |
| 239 | } | |
| 240 | } | |
| 241 | ||
| 242 | } | |
| 243 | ||
| 244 | ||
| 245 | } |