    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
    018
    019    package org.apache.commons.beanutils.converters;
    020
    021
    022    import java.util.List;
    023    import org.apache.commons.beanutils.ConversionException;
    024
    025
    026    /**
    027     * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
    028     * String into a primitive array of double.  On a conversion failure, returns
    029     * a specified default value or throws a {@link ConversionException} depending
    030     * on how this instance is constructed.</p>
    031     *
    032     * @author Craig R. McClanahan
    033     * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
    034     * @since 1.4
    035     * @deprecated Replaced by the new {@link ArrayConverter} implementation
    036     */
    037
    038    public final class DoubleArrayConverter extends AbstractArrayConverter {
    039
    040
    041        // ----------------------------------------------------------- Constructors
    042
    043
    044        /**
    045         * Create a {@link org.apache.commons.beanutils.Converter} that will throw
    046         * a {@link ConversionException} if a conversion error occurs.
    047         */
    048        public DoubleArrayConverter() {
    049
    050            this.defaultValue = null;
    051            this.useDefault = false;
    052
    053        }
    054
    055
    056        /**
    057         * Create a {@link org.apache.commons.beanutils.Converter} that will return
    058         * the specified default value if a conversion error occurs.
    059         *
    060         * @param defaultValue The default value to be returned
    061         */
    062        public DoubleArrayConverter(Object defaultValue) {
    063
    064            this.defaultValue = defaultValue;
    065            this.useDefault = true;
    066
    067        }
    068
    069
    070        // ------------------------------------------------------- Static Variables
    071
    072
    073        /**
    074         * <p>Model object for type comparisons.</p>
    075         */
    076        private static final double[] MODEL = new double[0];
    077
    078
    079        // --------------------------------------------------------- Public Methods
    080
    081
    082        /**
    083         * Convert the specified input object into an output object of the
    084         * specified type.
    085         *
    086         * @param type Data type to which this value should be converted
    087         * @param value The input value to be converted
    088         * @return the converted value
    089         * @exception ConversionException if conversion cannot be performed
    090         *  successfully
    091         */
    092        public Object convert(Class type, Object value) {
    093
    094            // Deal with a null value
    095            if (value == null) {
    096                if (useDefault) {
    097                    return (defaultValue);
    098                } else {
    099                    throw new ConversionException("No value specified");
    100                }
    101            }
    102
    103            // Deal with the no-conversion-needed case
    104            if (MODEL.getClass() == value.getClass()) {
    105                return (value);
    106            }
    107
    108            // Deal with input value as a String array
    109            if (strings.getClass() == value.getClass()) {
    110                try {
    111                    String[] values = (String[]) value;
    112                    double[] results = new double[values.length];
    113                    for (int i = 0; i < values.length; i++) {
    114                        results[i] = Double.parseDouble(values[i]);
    115                    }
    116                    return (results);
    117                } catch (Exception e) {
    118                    if (useDefault) {
    119                        return (defaultValue);
    120                    } else {
    121                        throw new ConversionException(value.toString(), e);
    122                    }
    123                }
    124            }
    125
    126            // Parse the input value as a String into elements
    127            // and convert to the appropriate type
    128            try {
    129                List list = parseElements(value.toString());
    130                double[] results = new double[list.size()];
    131                for (int i = 0; i < results.length; i++) {
    132                    results[i] = Double.parseDouble((String) list.get(i));
    133                }
    134                return (results);
    135            } catch (Exception e) {
    136                if (useDefault) {
    137                    return (defaultValue);
    138                } else {
    139                    throw new ConversionException(value.toString(), e);
    140                }
    141            }
    142
    143        }
    144
    145
    146    }