001    /*
002     *  Copyright 2003-2004 The Apache Software Foundation
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.apache.commons.convert1.string;
017    
018    
019    import java.sql.Time;
020    import org.apache.commons.convert1.ConversionException;
021    import org.apache.commons.convert1.Converter;
022    
023    
024    /**
025     * <p>Standard {@link Converter} implementation that converts an incoming
026     * String into a <code>java.sql.Time</code> object, optionally using a
027     * default value or throwing a {@link ConversionException} if a conversion
028     * error occurs.</p>
029     *
030     * @author Craig R. McClanahan
031     * @version $Id: SqlTimeConverter.java 155441 2005-02-26 13:19:22Z dirkv $
032     * @since 0.1
033     */
034    
035    public final class SqlTimeConverter implements Converter {
036    
037    
038        // ----------------------------------------------------------- Constructors
039    
040    
041        /**
042         * Create a {@link Converter} that will throw a {@link ConversionException}
043         * if a conversion error occurs.
044         */
045        public SqlTimeConverter() {
046    
047            this.defaultValue = null;
048            this.useDefault = false;
049    
050        }
051    
052    
053        /**
054         * Create a {@link Converter} that will return the specified default value
055         * if a conversion error occurs.
056         *
057         * @param defaultValue The default value to be returned
058         */
059        public SqlTimeConverter(Object defaultValue) {
060    
061            this.defaultValue = defaultValue;
062            this.useDefault = true;
063    
064        }
065    
066    
067        // ----------------------------------------------------- Instance Variables
068    
069    
070        /**
071         * The default value specified to our Constructor, if any.
072         */
073        private Object defaultValue = null;
074    
075    
076        /**
077         * Should we return the default value on conversion errors?
078         */
079        private boolean useDefault = true;
080    
081    
082        // --------------------------------------------------------- Public Methods
083    
084    
085        /**
086         * Convert the specified input object into an output object of the
087         * specified type.
088         *
089         * @param type Data type to which this value should be converted
090         * @param value The input value to be converted
091         *
092         * @exception ConversionException if conversion cannot be performed
093         *  successfully
094         */
095        public Object convert(Class type, Object value) {
096    
097            if (value == null) {
098                if (useDefault) {
099                    return (defaultValue);
100                } else {
101                    throw new ConversionException("No value specified");
102                }
103            }
104    
105            if (value instanceof Time) {
106                return (value);
107            }
108    
109            try {
110                return (Time.valueOf(value.toString()));
111            } catch (Exception e) {
112                if (useDefault) {
113                    return (defaultValue);
114                } else {
115                    throw new ConversionException(e);
116                }
117            }
118    
119        }
120    
121    
122    }