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    package org.apache.commons.beanutils.converters;
018    
019    import java.sql.Time;
020    import java.text.DateFormat;
021    import java.util.Locale;
022    import java.util.TimeZone;
023    
024    /**
025     * {@link DateTimeConverter} implementation that handles conversion to
026     * and from <b>java.sql.Time</b> objects.
027     * <p>
028     * This implementation can be configured to handle conversion either
029     * by using java.sql.Time's default String conversion, or by using a
030     * Locale's default format or by specifying a set of format patterns.
031     * See the {@link DateTimeConverter} documentation for further details.
032     * <p>
033     * Can be configured to either return a <i>default value</i> or throw a
034     * <code>ConversionException</code> if a conversion error occurs.
035     *
036     * @author Craig R. McClanahan
037     * @version $Revision: 690380 $ $Date: 2008-08-29 21:04:38 +0100 (Fri, 29 Aug 2008) $
038     * @since 1.3
039     */
040    public final class SqlTimeConverter extends DateTimeConverter {
041    
042        /**
043         * Construct a <b>java.sql.Time</b> <i>Converter</i> that throws
044         * a <code>ConversionException</code> if an error occurs.
045         */
046        public SqlTimeConverter() {
047            super();
048        }
049    
050        /**
051         * Construct a <b>java.sql.Time</b> <i>Converter</i> that returns
052         * a default value if an error occurs.
053         *
054         * @param defaultValue The default value to be returned
055         * if the value to be converted is missing or an error
056         * occurs converting the value.
057         */
058        public SqlTimeConverter(Object defaultValue) {
059            super(defaultValue);
060        }
061    
062        /**
063         * Return the default type this <code>Converter</code> handles.
064         *
065         * @return The default type this <code>Converter</code> handles.
066         * @since 1.8.0
067         */
068        protected Class getDefaultType() {
069            return Time.class;
070        }
071    
072        /**
073         * Return a <code>DateFormat<code> for the Locale.
074         * @param locale TODO
075         * @param timeZone TODO
076         *
077         * @return The DateFormat.
078         * @since 1.8.0
079         */
080        protected DateFormat getFormat(Locale locale, TimeZone timeZone) {
081            DateFormat format = null;
082            if (locale == null) {
083                format = DateFormat.getTimeInstance(DateFormat.SHORT);
084            } else {
085                format = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
086            }
087            if (timeZone != null) {
088                format.setTimeZone(timeZone);
089            }
090            return format;
091        }
092    
093    }