001: /*
002: * Copyright 2005-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.dozer.util.mapping.converters;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import net.sf.dozer.util.mapping.cache.Cache;
022: import net.sf.dozer.util.mapping.cache.CacheEntry;
023: import net.sf.dozer.util.mapping.cache.CacheKeyFactory;
024:
025: import org.apache.commons.lang.builder.ReflectionToStringBuilder;
026: import org.apache.commons.lang.builder.ToStringStyle;
027:
028: /**
029: * Internal class for holding custom converter definitions. Only intended for internal use.
030: *
031: * @author sullins.ben
032: */
033: public class CustomConverterContainer {
034:
035: private List converters = new ArrayList();
036:
037: public List getConverters() {
038: return converters;
039: }
040:
041: public void setConverters(List converters) {
042: this .converters = converters;
043: }
044:
045: public void addConverter(CustomConverterDescription converter) {
046: getConverters().add(converter);
047: }
048:
049: public Class getCustomConverter(Class srcClass, Class destClass,
050: Cache converterByDestTypeCache) {
051: // If no converters have been specified, no point in continuing. Just return.
052: if (converters == null || converters.size() < 1) {
053: return null;
054: }
055:
056: // Let's see if the incoming class is a primitive:
057: Class src = srcClass;
058: if (srcClass.isPrimitive()) {
059: Class c = getWrapper(srcClass);
060: if (c != null) {
061: src = c;
062: }
063: }
064:
065: Class dest = destClass;
066: if (dest.isPrimitive()) {
067: Class c = getWrapper(destClass);
068: if (c != null) {
069: dest = c;
070: }
071: }
072:
073: // Check cache first
074: Object cacheKey = CacheKeyFactory.createKey(new Object[] {
075: dest, src });
076: CacheEntry cacheEntry = converterByDestTypeCache.get(cacheKey);
077: if (cacheEntry != null) {
078: return (Class) cacheEntry.getValue();
079: }
080:
081: // Otherwise, loop through custom converters and look for a match. Also, store the result in the cache
082: Class result = null;
083: long size = converters.size();
084: for (int i = 0; i < size; i++) {
085: CustomConverterDescription customConverter = (CustomConverterDescription) converters
086: .get(i);
087: Class classA = customConverter.getClassA();
088: Class classB = customConverter.getClassB();
089:
090: // we check to see if the destination class is the same as classA defined in the converter mapping xml.
091: // we next check if the source class is the same as classA defined in the converter mapping xml.
092: // we also to check to see if it is assignable to either. We then perform these checks in the other direction for classB
093: if ((classA.isAssignableFrom(dest) && classB
094: .isAssignableFrom(src))
095: || (classA.isAssignableFrom(src) && classB
096: .isAssignableFrom(dest))) {
097: result = customConverter.getType();
098: }
099: }
100: cacheEntry = new CacheEntry(cacheKey, result);
101: converterByDestTypeCache.put(cacheEntry);
102: return result;
103: }
104:
105: private Class getWrapper(Class c) {
106: Class clazz = null;
107: if (c.equals(Integer.TYPE)) {
108: clazz = Integer.class;
109: } else if (c.equals(Double.TYPE)) {
110: clazz = Double.class;
111: } else if (c.equals(Short.TYPE)) {
112: clazz = Short.class;
113: } else if (c.equals(Long.TYPE)) {
114: clazz = Long.class;
115: } else if (c.equals(Boolean.TYPE)) {
116: clazz = Boolean.class;
117: } else if (c.equals(Float.TYPE)) {
118: clazz = Float.class;
119: }
120: return clazz;
121: }
122:
123: public String toString() {
124: return ReflectionToStringBuilder.toString(this,
125: ToStringStyle.MULTI_LINE_STYLE);
126: }
127: }
|