KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > converters > CustomConverterContainer


1 /*
2  * Copyright 2005-2007 the original author or authors.
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 net.sf.dozer.util.mapping.converters;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
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 /**
26  * @author sullins.ben
27  */

28 public class CustomConverterContainer {
29
30   private List JavaDoc converters = new ArrayList JavaDoc();
31
32   public List JavaDoc getConverters() {
33     return converters;
34   }
35
36   public void setConverters(List JavaDoc in) {
37     this.converters = in;
38   }
39
40   public void addConverter(CustomConverterDescription converter) {
41     getConverters().add(converter);
42   }
43
44   public Class JavaDoc getCustomConverter(Class JavaDoc pSourceClass, Class JavaDoc pDestinationClass, Cache converterByDestTypeCache)
45       throws ClassNotFoundException JavaDoc {
46     //If no converters have been specified, no point in continuing. Just return.
47
if (converters == null || converters.size() < 1) {
48       return null;
49     }
50
51     // Let's see if the incoming class is a primitive:
52
Class JavaDoc src = pSourceClass;
53     Class JavaDoc c = getWrapper(pSourceClass);
54     if (c != null) {
55       src = c;
56     }
57     
58     Class JavaDoc dest = pDestinationClass;
59     c = getWrapper(pDestinationClass);
60     if (c != null) {
61       dest = c;
62     }
63     
64     // Check cache first
65
Object JavaDoc cacheKey = CacheKeyFactory.createKey(new Object JavaDoc[] { dest, src });
66     CacheEntry cacheEntry = converterByDestTypeCache.get(cacheKey);
67     if (cacheEntry != null) {
68       return (Class JavaDoc) cacheEntry.getValue();
69     }
70
71     // Otherwise, loop through custom converters and look for a match. Also, store the result in the cache
72
Class JavaDoc result = null;
73     long size = converters.size();
74     for (int i = 0; i < size; i++) {
75       CustomConverterDescription customConverter = (CustomConverterDescription) converters.get(i);
76       Class JavaDoc classA = customConverter.getClassA();
77       Class JavaDoc 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
82
// classB
83
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 JavaDoc getWrapper(Class JavaDoc c) {
94     Class JavaDoc clazz = null;
95     if (c.equals(Integer.TYPE)) {
96       clazz = Integer JavaDoc.class;
97     } else if (c.equals(Double.TYPE)) {
98       clazz = Double JavaDoc.class;
99     } else if (c.equals(Short.TYPE)) {
100       clazz = Short JavaDoc.class;
101     } else if (c.equals(Long.TYPE)) {
102       clazz = Long JavaDoc.class;
103     } else if (c.equals(Boolean.TYPE)) {
104       clazz = Boolean JavaDoc.class;
105     } else if (c.equals(Float.TYPE)) {
106       clazz = Float JavaDoc.class;
107     }
108     return clazz;
109   }
110 }
Popular Tags