Coverage Report - org.dozer.converters.DateConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
DateConverter
94%
37/39
95%
19/20
10.5
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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.dozer.converters;
 17  
 
 18  
 import org.apache.commons.beanutils.Converter;
 19  
 
 20  
 import javax.xml.datatype.XMLGregorianCalendar;
 21  
 import java.lang.reflect.Constructor;
 22  
 import java.sql.Timestamp;
 23  
 import java.text.DateFormat;
 24  
 import java.text.ParseException;
 25  
 import java.util.Calendar;
 26  
 
 27  
 /**
 28  
  * Internal convertor for handling Date/Time conversions.
 29  
  *
 30  
  * Supported source data types include java.util.Date,
 31  
  * java.sql.Date, java.sql.Time, java.sql.Timestamp, java.util.Calendar, javax.xml.datatype.XMLGregorianCalendar,
 32  
  * java.lang.String and any objects that return a number of milliseconds applicable to java.lang.Long
 33  
  * format in their toString() form.
 34  
  *
 35  
  * Supported return data types are all Date/Time types, which are based on a timestamp constructor
 36  
  * (e.g. new MyDate(new Long(1))). Calendar return type is also supported.
 37  
  *
 38  
  * Only intended for internal use.
 39  
  * 
 40  
  * @author tierney.matt
 41  
  * @author dmitry.buzdin
 42  
  */
 43  
 public class DateConverter implements Converter {
 44  
 
 45  
   private DateFormat dateFormat;
 46  
 
 47  103
   public DateConverter(DateFormat dateFormat) {
 48  103
     this.dateFormat = dateFormat;
 49  103
   }
 50  
 
 51  
   public Object convert(Class destClass, Object srcObj) {
 52  102
     final Class srcFieldClass = srcObj.getClass();
 53  
 
 54  
     long time;
 55  102
     int nanos = 0;
 56  102
     if (Calendar.class.isAssignableFrom(srcFieldClass)) {
 57  22
       Calendar inVal = (Calendar) srcObj;
 58  22
       time = inVal.getTime().getTime();
 59  22
     } else if (Timestamp.class.isAssignableFrom(srcFieldClass)) {
 60  4
       Timestamp timestamp = (Timestamp) srcObj;
 61  4
       time = timestamp.getTime();
 62  4
       nanos = timestamp.getNanos();
 63  4
     } else if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
 64  36
       time = ((java.util.Date) srcObj).getTime();
 65  40
     } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
 66  7
       time = ((XMLGregorianCalendar) srcObj).toGregorianCalendar().getTimeInMillis();
 67  33
     } else if (dateFormat != null && String.class.isAssignableFrom(srcObj.getClass())) {
 68  
       try {
 69  26
         if ("".equals(srcObj)) {
 70  1
           return null;
 71  
         }
 72  25
         time = dateFormat.parse((String) srcObj).getTime();
 73  2
       } catch (ParseException e) {
 74  2
         throw new ConversionException("Unable to parse source object using specified date format", e);
 75  23
       }
 76  
       // Default conversion
 77  
     } else {
 78  
       try {
 79  7
         time = Long.parseLong(srcObj.toString());
 80  0
       } catch (NumberFormatException e) {
 81  0
         throw new ConversionException("Unable to determine time in millis of source object", e);
 82  7
       }
 83  
     }
 84  
 
 85  
     try {
 86  99
       if (Calendar.class.isAssignableFrom(destClass)) {
 87  1
         Constructor constructor = destClass.getConstructor();
 88  1
         Calendar result = (Calendar) constructor.newInstance();
 89  1
         result.setTimeInMillis(time);
 90  1
         return result;
 91  
       }
 92  98
       Constructor constructor = destClass.getConstructor(Long.TYPE);
 93  97
       Object result = constructor.newInstance(time);
 94  97
       if (nanos != 0 && (Timestamp.class.isAssignableFrom(destClass))) {
 95  1
         ((Timestamp) result).setNanos(nanos);
 96  
       }
 97  97
       return result;
 98  1
     } catch (Exception e) {
 99  1
       throw new ConversionException(e);
 100  
     }
 101  
   }
 102  
 
 103  
 }