Coverage Report - org.apache.commons.convert1.string.SqlTimeConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
SqlTimeConverter
0%
0/21
0%
0/8
4.667
 
 1  
 /*
 2  
  *  Copyright 2003-2004 The Apache Software Foundation
 3  
  *
 4  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 5  
  *  you may not use this file except in compliance with the License.
 6  
  *  You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  *  Unless required by applicable law or agreed to in writing, software
 11  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 12  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  *  See the License for the specific language governing permissions and
 14  
  *  limitations under the License.
 15  
  */
 16  
 package org.apache.commons.convert1.string;
 17  
 
 18  
 
 19  
 import java.sql.Time;
 20  
 import org.apache.commons.convert1.ConversionException;
 21  
 import org.apache.commons.convert1.Converter;
 22  
 
 23  
 
 24  
 /**
 25  
  * <p>Standard {@link Converter} implementation that converts an incoming
 26  
  * String into a <code>java.sql.Time</code> object, optionally using a
 27  
  * default value or throwing a {@link ConversionException} if a conversion
 28  
  * error occurs.</p>
 29  
  *
 30  
  * @author Craig R. McClanahan
 31  
  * @version $Id: SqlTimeConverter.java 155441 2005-02-26 13:19:22Z dirkv $
 32  
  * @since 0.1
 33  
  */
 34  
 
 35  
 public final class SqlTimeConverter implements Converter {
 36  
 
 37  
 
 38  
     // ----------------------------------------------------------- Constructors
 39  
 
 40  
 
 41  
     /**
 42  
      * Create a {@link Converter} that will throw a {@link ConversionException}
 43  
      * if a conversion error occurs.
 44  
      */
 45  0
     public SqlTimeConverter() {
 46  
 
 47  0
         this.defaultValue = null;
 48  0
         this.useDefault = false;
 49  
 
 50  0
     }
 51  
 
 52  
 
 53  
     /**
 54  
      * Create a {@link Converter} that will return the specified default value
 55  
      * if a conversion error occurs.
 56  
      *
 57  
      * @param defaultValue The default value to be returned
 58  
      */
 59  0
     public SqlTimeConverter(Object defaultValue) {
 60  
 
 61  0
         this.defaultValue = defaultValue;
 62  0
         this.useDefault = true;
 63  
 
 64  0
     }
 65  
 
 66  
 
 67  
     // ----------------------------------------------------- Instance Variables
 68  
 
 69  
 
 70  
     /**
 71  
      * The default value specified to our Constructor, if any.
 72  
      */
 73  0
     private Object defaultValue = null;
 74  
 
 75  
 
 76  
     /**
 77  
      * Should we return the default value on conversion errors?
 78  
      */
 79  0
     private boolean useDefault = true;
 80  
 
 81  
 
 82  
     // --------------------------------------------------------- Public Methods
 83  
 
 84  
 
 85  
     /**
 86  
      * Convert the specified input object into an output object of the
 87  
      * specified type.
 88  
      *
 89  
      * @param type Data type to which this value should be converted
 90  
      * @param value The input value to be converted
 91  
      *
 92  
      * @exception ConversionException if conversion cannot be performed
 93  
      *  successfully
 94  
      */
 95  
     public Object convert(Class type, Object value) {
 96  
 
 97  0
         if (value == null) {
 98  0
             if (useDefault) {
 99  0
                 return (defaultValue);
 100  
             } else {
 101  0
                 throw new ConversionException("No value specified");
 102  
             }
 103  
         }
 104  
 
 105  0
         if (value instanceof Time) {
 106  0
             return (value);
 107  
         }
 108  
 
 109  
         try {
 110  0
             return (Time.valueOf(value.toString()));
 111  0
         } catch (Exception e) {
 112  0
             if (useDefault) {
 113  0
                 return (defaultValue);
 114  
             } else {
 115  0
                 throw new ConversionException(e);
 116  
             }
 117  
         }
 118  
 
 119  
     }
 120  
 
 121  
 
 122  
 }