Coverage Report - org.dozer.converters.CalendarConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
CalendarConverter
92%
25/27
90%
9/10
4
 
 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.text.DateFormat;
 22  
 import java.text.ParseException;
 23  
 import java.util.Calendar;
 24  
 import java.util.Date;
 25  
 import java.util.GregorianCalendar;
 26  
 
 27  
 /**
 28  
  * Internal class for converting Supported Data Types --> Calendar. Supported source data types include Date, Calendar,
 29  
  * String, Objects that return a long from their toString(). Only intended for internal use.
 30  
  * 
 31  
  * @author tierney.matt
 32  
  */
 33  
 public class CalendarConverter implements Converter {
 34  
 
 35  
   private DateFormat dateFormat;
 36  
 
 37  115
   public CalendarConverter(DateFormat dateFormat) {
 38  115
     this.dateFormat = dateFormat;
 39  115
   }
 40  
 
 41  
   public Object convert(Class destClass, Object srcObj) {
 42  114
     Calendar result = new GregorianCalendar();
 43  114
     Class srcFieldClass = srcObj.getClass();
 44  
     // Convert from Date to Calendar
 45  114
     if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
 46  43
       result.setTime((java.util.Date) srcObj);
 47  
     }
 48  
     //  Convert from Calendar to Calendar
 49  71
     else if (Calendar.class.isAssignableFrom(srcFieldClass)) {
 50  50
       Calendar c = (Calendar) srcObj;
 51  50
       result.setTime(c.getTime());
 52  50
       result.setTimeZone(c.getTimeZone());
 53  50
     } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
 54  2
       Calendar c = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
 55  2
       result.setTime(c.getTime());
 56  2
       result.setTimeZone(c.getTimeZone());
 57  2
     }
 58  
     // String to Calendar
 59  19
     else if (dateFormat != null && String.class.isAssignableFrom(srcFieldClass)) {
 60  
       try {
 61  17
         result.setTime(new Date(dateFormat.parse((String) srcObj).getTime()));
 62  1
       } catch (ParseException e) {
 63  1
         throw new ConversionException("Unable to parse source object using specified date format", e);
 64  16
       }
 65  
       // Default conversion
 66  
     } else {
 67  
       try {
 68  2
         result.setTime(new Date(Long.parseLong(srcObj.toString())));
 69  0
       } catch (NumberFormatException e) {
 70  0
         throw new ConversionException("Unable to determine time in millis of source object", e);
 71  2
       }
 72  
     }
 73  113
     return result;
 74  
   }
 75  
 
 76  
   public DateFormat getDateFormat() {
 77  1
     return dateFormat;
 78  
   }
 79  
 }