1 16 package net.sf.dozer.util.mapping.converters; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import net.sf.dozer.util.mapping.cache.Cache; 22 import net.sf.dozer.util.mapping.cache.CacheEntry; 23 import net.sf.dozer.util.mapping.cache.CacheKeyFactory; 24 25 28 public class CustomConverterContainer { 29 30 private List converters = new ArrayList (); 31 32 public List getConverters() { 33 return converters; 34 } 35 36 public void setConverters(List in) { 37 this.converters = in; 38 } 39 40 public void addConverter(CustomConverterDescription converter) { 41 getConverters().add(converter); 42 } 43 44 public Class getCustomConverter(Class pSourceClass, Class pDestinationClass, Cache converterByDestTypeCache) 45 throws ClassNotFoundException { 46 if (converters == null || converters.size() < 1) { 48 return null; 49 } 50 51 Class src = pSourceClass; 53 Class c = getWrapper(pSourceClass); 54 if (c != null) { 55 src = c; 56 } 57 58 Class dest = pDestinationClass; 59 c = getWrapper(pDestinationClass); 60 if (c != null) { 61 dest = c; 62 } 63 64 Object cacheKey = CacheKeyFactory.createKey(new Object [] { dest, src }); 66 CacheEntry cacheEntry = converterByDestTypeCache.get(cacheKey); 67 if (cacheEntry != null) { 68 return (Class ) cacheEntry.getValue(); 69 } 70 71 Class result = null; 73 long size = converters.size(); 74 for (int i = 0; i < size; i++) { 75 CustomConverterDescription customConverter = (CustomConverterDescription) converters.get(i); 76 Class classA = customConverter.getClassA(); 77 Class classB = customConverter.getClassB(); 78 79 if ((classA.isAssignableFrom(dest) && classB.isAssignableFrom(src)) 84 || (classA.isAssignableFrom(src) && classB.isAssignableFrom(dest))) { 85 result = customConverter.getType(); 86 } 87 } 88 cacheEntry = new CacheEntry(cacheKey, result); 89 converterByDestTypeCache.put(cacheEntry); 90 return result; 91 } 92 93 private Class getWrapper(Class c) { 94 Class clazz = null; 95 if (c.equals(Integer.TYPE)) { 96 clazz = Integer .class; 97 } else if (c.equals(Double.TYPE)) { 98 clazz = Double .class; 99 } else if (c.equals(Short.TYPE)) { 100 clazz = Short .class; 101 } else if (c.equals(Long.TYPE)) { 102 clazz = Long .class; 103 } else if (c.equals(Boolean.TYPE)) { 104 clazz = Boolean .class; 105 } else if (c.equals(Float.TYPE)) { 106 clazz = Float .class; 107 } 108 return clazz; 109 } 110 } | Popular Tags |