Coverage Report - org.dozer.converters.PrimitiveOrWrapperConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimitiveOrWrapperConverter
100%
34/34
97%
35/36
8.333
 
 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  
 import org.apache.commons.beanutils.converters.BigDecimalConverter;
 20  
 import org.apache.commons.beanutils.converters.BigIntegerConverter;
 21  
 import org.apache.commons.beanutils.converters.BooleanConverter;
 22  
 import org.apache.commons.beanutils.converters.CharacterConverter;
 23  
 import org.apache.commons.beanutils.converters.ClassConverter;
 24  
 import org.apache.commons.beanutils.converters.DoubleConverter;
 25  
 import org.apache.commons.beanutils.converters.FloatConverter;
 26  
 import org.apache.commons.lang3.ClassUtils;
 27  
 import org.dozer.util.MappingUtils;
 28  
 
 29  
 import javax.xml.datatype.XMLGregorianCalendar;
 30  
 import java.math.BigDecimal;
 31  
 import java.math.BigInteger;
 32  
 import java.util.Calendar;
 33  
 import java.util.HashMap;
 34  
 import java.util.Map;
 35  
 
 36  
 /**
 37  
  * Internal class for converting between wrapper types(including primitives). Only intended for internal use.
 38  
  * 
 39  
  * @author tierney.matt
 40  
  * @author garsombke.franz
 41  
  * @author benson.matt
 42  
  * @author dmitry.buzdin
 43  
  */
 44  132077
 public class PrimitiveOrWrapperConverter {
 45  
 
 46  1
   private static final Map<Class, Converter> CONVERTER_MAP = new HashMap<Class, Converter>();
 47  
 
 48  
   static {
 49  1
     CONVERTER_MAP.put(Integer.class, new IntegerConverter());
 50  1
     CONVERTER_MAP.put(Double.class, new DoubleConverter());
 51  1
     CONVERTER_MAP.put(Short.class, new ShortConverter());
 52  1
     CONVERTER_MAP.put(Character.class, new CharacterConverter());
 53  1
     CONVERTER_MAP.put(Long.class, new LongConverter());
 54  1
     CONVERTER_MAP.put(Boolean.class, new BooleanConverter());
 55  1
     CONVERTER_MAP.put(Byte.class, new ByteConverter());
 56  1
     CONVERTER_MAP.put(Float.class, new FloatConverter());
 57  1
     CONVERTER_MAP.put(BigDecimal.class, new BigDecimalConverter());
 58  1
     CONVERTER_MAP.put(BigInteger.class, new BigIntegerConverter());
 59  1
     CONVERTER_MAP.put(Class.class, new ClassConverter());
 60  1
   }
 61  
 
 62  
   public Object convert(Object srcFieldValue, Class destFieldClass, DateFormatContainer dateFormatContainer) {
 63  46559
     if (srcFieldValue == null || destFieldClass == null || (srcFieldValue.equals("") && !destFieldClass.equals(String.class))) {
 64  62
       return null;
 65  
     }
 66  46497
     Converter converter = getPrimitiveOrWrapperConverter(destFieldClass, dateFormatContainer);
 67  
     try {
 68  46490
       return converter.convert(destFieldClass, srcFieldValue);
 69  3
     } catch (org.apache.commons.beanutils.ConversionException e) {
 70  3
       throw new org.dozer.converters.ConversionException(e);
 71  
     }
 72  
   }
 73  
 
 74  
   private Converter getPrimitiveOrWrapperConverter(Class destClass, DateFormatContainer dateFormatContainer) {
 75  46497
     if (String.class.equals(destClass)) {
 76  44371
       return new StringConverter(dateFormatContainer);
 77  
     }
 78  
 
 79  2126
     Converter result = CONVERTER_MAP.get(ClassUtils.primitiveToWrapper(destClass));
 80  
 
 81  2126
     if (result == null) {
 82  382
       if (java.util.Date.class.isAssignableFrom(destClass)) {
 83  95
         result = new DateConverter(dateFormatContainer.getDateFormat());
 84  287
       } else if (Calendar.class.isAssignableFrom(destClass) ) {
 85  115
         result = new CalendarConverter(dateFormatContainer.getDateFormat());
 86  172
       } else if (XMLGregorianCalendar.class.isAssignableFrom(destClass)){
 87  15
         result = new XMLGregorianCalendarConverter(dateFormatContainer.getDateFormat());
 88  157
       } else if (MappingUtils.isEnumType(destClass)){
 89  38
         result = new EnumConverter();
 90  
       }
 91  
     }
 92  2119
     return result == null ? new StringConstructorConverter(dateFormatContainer) : result;
 93  
   }
 94  
 
 95  
   public boolean accepts(Class<?> aClass) {
 96  129676
     return aClass.isPrimitive()
 97  
             || Number.class.isAssignableFrom(aClass)
 98  
             || String.class.equals(aClass)
 99  
             || Character.class.equals(aClass)
 100  
             || Boolean.class.equals(aClass)
 101  
             || java.util.Date.class.isAssignableFrom(aClass)
 102  
             || java.util.Calendar.class.isAssignableFrom(aClass);
 103  
   }
 104  
 
 105  
 }