    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.beanutils.converters;
    019
    020import java.util.List;
    021
    022import org.apache.commons.beanutils.ConversionException;
    023
    024/**
    025 * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
    026 * String into a primitive array of boolean.  On a conversion failure, returns
    027 * a specified default value or throws a {@link ConversionException} depending
    028 * on how this instance is constructed.</p>
    029 *
    030 * <p>By default, the values to be converted are expected to be those
    031 * recognized by a default instance of BooleanConverter. A customized
    032 * BooleanConverter can be provided in order to recognise alternative values
    033 * as true/false. </p>
    034 *
    035 * @since 1.4
    036 * @deprecated Replaced by the new {@link ArrayConverter} implementation
    037 */
    038
    039@Deprecated
    040public final class BooleanArrayConverter extends AbstractArrayConverter {
    041
    042    /**
    043     * Type which this class converts its input to. This value can be
    044     * used as a parameter to the ConvertUtils.register method.
    045     * @since 1.8.0
    046     */
    047    public static final Class MODEL = new boolean[0].getClass();
    048
    049    /**
    050     * The converter that all instances of this class will use to
    051     * do individual string to boolean conversions, unless overridden
    052     * in the constructor.
    053     */
    054    private static final BooleanConverter DEFAULT_CONVERTER
    055        = new BooleanConverter();
    056
    057    /**
    058     * This object is used to perform the conversion of individual strings
    059     * into Boolean/boolean values.
    060     */
    061    protected final BooleanConverter booleanConverter;
    062
    063    /**
    064     * Create a {@link org.apache.commons.beanutils.Converter} that will throw
    065     * a {@link ConversionException} if a conversion error occurs.
    066     *
    067     * <p>Conversion of strings to boolean values will be done via a default
    068     * instance of class BooleanConverter.</p>
    069     */
    070    public BooleanArrayConverter() {
    071
    072        this.booleanConverter = DEFAULT_CONVERTER;
    073
    074    }
    075
    076    /**
    077     * Create a {@link org.apache.commons.beanutils.Converter} that will return
    078     * the specified default value if a conversion error occurs.
    079     *
    080     * <p>Conversion of strings to boolean values will be done via the
    081     * specified converter.</p>
    082     *
    083     * @param converter is the converter object that will be used to
    084     *  convert each input string-value into a boolean.
    085     *
    086     * @param defaultValue is the default value to be returned by method
    087     * convert if conversion fails; null is a valid default value. See the
    088     * documentation for method "convert" for more information.
    089     * The value BooleanArrayConverter.NO_DEFAULT may be passed here to
    090     * specify that an exception should be thrown on conversion failure.
    091     *
    092     */
    093    public BooleanArrayConverter(final BooleanConverter converter, final Object defaultValue) {
    094
    095        super(defaultValue);
    096        this.booleanConverter = converter;
    097
    098    }
    099
    100    /**
    101     * Create a {@link org.apache.commons.beanutils.Converter} that will return
    102     * the specified default value if a conversion error occurs.
    103     *
    104     * <p>Conversion of strings to boolean values will be done via a default
    105     * instance of class BooleanConverter.</p>
    106     *
    107     * @param defaultValue The default value to be returned
    108     */
    109    public BooleanArrayConverter(final Object defaultValue) {
    110
    111        super(defaultValue);
    112        this.booleanConverter = DEFAULT_CONVERTER;
    113
    114    }
    115
    116    /**
    117     * Convert the specified input object into an output object of type
    118     * array-of-boolean.
    119     *
    120     * <p>If the input value is null, then the default value specified in the
    121     * constructor is returned. If no such value was provided, then a
    122     * ConversionException is thrown instead.</p>
    123     *
    124     * <p>If the input value is of type String[] then the returned array shall
    125     * be of the same size as this array, with a true or false value in each
    126     * array element depending on the result of applying method
    127     * BooleanConverter.convert to each string.</p>
    128     *
    129     * <p>For all other types of value, the object's toString method is
    130     * expected to return a string containing a comma-separated list of
    131     * values, eg "true, false, true". See the documentation for
    132     * {@link AbstractArrayConverter#parseElements} for more information on
    133     * the exact formats supported.</p>
    134     *
    135     * <p>If the result of value.toString() cannot be split into separate
    136     * words, then the default value is also returned (or an exception thrown).
    137     * </p>
    138     *
    139     * <p>If any of the elements in the value array (or the elements resulting
    140     * from splitting up value.toString) are not recognized by the
    141     * BooleanConverter associated with this object, then what happens depends
    142     * on whether that BooleanConverter has a default value or not: if it does,
    143     * then that unrecognized element is converted into the BooleanConverter's
    144     * default value. If the BooleanConverter does <em>not</em> have a default
    145     * value, then the default value for this object is returned as the
    146     * <em>complete</em> conversion result (not just for the element), or an
    147     * exception is thrown if this object has no default value defined.</p>
    148     *
    149     * @param type is the type to which this value should be converted. In the
    150     *  case of this BooleanArrayConverter class, this value is ignored.
    151     *
    152     * @param value is the input value to be converted.
    153     * @return an object of type boolean[], or the default value if there was
    154     *  any sort of error during conversion and the constructor
    155     *  was provided with a default value.
    156     *
    157     * @throws ConversionException if conversion cannot be performed
    158     *  successfully and the constructor was not provided with a default
    159     *  value to return on conversion failure.
    160     *
    161     * @throws NullPointerException if value is an array, and any of the
    162     * array elements are null.
    163     */
    164    @Override
    165    public Object convert(final Class type, final Object value) {
    166
    167        // Deal with a null value
    168        if (value == null) {
    169            if (useDefault) {
    170                return defaultValue;
    171            }
    172            throw new ConversionException("No value specified");
    173        }
    174
    175        // Deal with the no-conversion-needed case
    176        if (MODEL == value.getClass()) {
    177            return value;
    178        }
    179
    180        // Deal with input value as a String array
    181        //
    182        // TODO: use if (value.getClass().isArray() instead...
    183        //  this requires casting to Object[], then using values[i].toString()
    184        if (strings.getClass() == value.getClass()) {
    185            try {
    186                final String[] values = (String[]) value;
    187                final boolean[] results = new boolean[values.length];
    188                for (int i = 0; i < values.length; i++) {
    189                    final String stringValue = values[i];
    190                    final Object result = booleanConverter.convert(Boolean.class, stringValue);
    191                    results[i] = ((Boolean) result).booleanValue();
    192                }
    193                return results;
    194            } catch (final Exception e) {
    195                if (useDefault) {
    196                    return defaultValue;
    197                }
    198                throw new ConversionException(value.toString(), e);
    199            }
    200        }
    201
    202        // We only get here if the input value is not of type String[].
    203        // In this case, we assume value.toString() returns a comma-separated
    204        // sequence of values; see method AbstractArrayConverter.parseElements
    205        // for more information.
    206        try {
    207            final List<String> list = parseElements(value.toString());
    208            final boolean[] results = new boolean[list.size()];
    209            for (int i = 0; i < results.length; i++) {
    210                final String stringValue = list.get(i);
    211                final Object result = booleanConverter.convert(Boolean.class, stringValue);
    212                results[i] = ((Boolean) result).booleanValue();
    213            }
    214            return results;
    215        } catch (final Exception e) {
    216            if (useDefault) {
    217                return defaultValue;
    218            }
    219            throw new ConversionException(value.toString(), e);
    220        }
    221
    222    }
    223
    224}