Coverage Report - org.dozer.converters.CustomConverterContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
CustomConverterContainer
92%
25/27
93%
15/16
3
 
 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.lang3.ClassUtils;
 19  
 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
 20  
 import org.apache.commons.lang3.builder.ToStringStyle;
 21  
 import org.dozer.cache.Cache;
 22  
 import org.dozer.cache.CacheKeyFactory;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * Internal class for holding custom converter definitions. Only intended for internal use.
 29  
  *
 30  
  * @author sullins.ben
 31  
  * @author dmitry.buzdin
 32  
  */
 33  711
 public class CustomConverterContainer {
 34  
 
 35  711
   private List<CustomConverterDescription> converters = new ArrayList<CustomConverterDescription>();
 36  
 
 37  
   public List<CustomConverterDescription> getConverters() {
 38  1765
     return converters;
 39  
   }
 40  
 
 41  
   public void setConverters(List<CustomConverterDescription> converters) {
 42  10580
     if (converters == null) {
 43  0
       throw new NullPointerException("Converters can not be null!");
 44  
     }
 45  10580
     this.converters = converters;
 46  10580
   }
 47  
 
 48  
   public void addConverter(CustomConverterDescription converter) {
 49  396
     getConverters().add(converter);
 50  396
   }
 51  
 
 52  
   public Class getCustomConverter(Class<?> srcClass, Class<?> destClass, Cache converterTypeCache) {
 53  660485
     if (converters.isEmpty()) {
 54  529339
       return null;
 55  
     }
 56  
 
 57  
     // Check cache first
 58  131146
     final Object cacheKey = CacheKeyFactory.createKey(destClass, srcClass);
 59  131146
     if (converterTypeCache.containsKey(cacheKey)) { // even null
 60  128693
       return (Class) converterTypeCache.get(cacheKey);
 61  
     }
 62  
 
 63  
     // Let's see if the incoming class is a primitive:
 64  2453
     final Class src = ClassUtils.primitiveToWrapper(srcClass);
 65  2453
     final Class dest = ClassUtils.primitiveToWrapper(destClass);
 66  
 
 67  2453
     Class appropriateConverter = findConverter(src, dest);
 68  2453
     converterTypeCache.put(cacheKey, appropriateConverter);
 69  
 
 70  2453
     return appropriateConverter;
 71  
   }
 72  
 
 73  
   private Class findConverter(Class src, Class dest) {
 74  
     // Otherwise, loop through custom converters and look for a match. Also, store the result in the cache
 75  2453
     for (CustomConverterDescription customConverter : converters) {
 76  7009
       final Class classA = customConverter.getClassA();
 77  7009
       final Class classB = customConverter.getClassB();
 78  
 
 79  
       // we check to see if the destination class is the same as classA defined in the converter mapping xml.
 80  
       // we next check if the source class is the same as classA defined in the converter mapping xml.
 81  
       // we also to check to see if it is assignable to either. We then perform these checks in the other direction for classB
 82  7009
       if ((classA.isAssignableFrom(dest) && classB.isAssignableFrom(src))
 83  
           || (classA.isAssignableFrom(src) && classB.isAssignableFrom(dest))) {
 84  75
         return customConverter.getType();
 85  
       }
 86  6934
     }
 87  2378
     return null;
 88  
   }
 89  
 
 90  
   @Override
 91  
   public String toString() {
 92  0
     return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
 93  
   }
 94  
 
 95  
 }