⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customconvertercontainer.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 JAVA
字号:
/*
 * Copyright 2005-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.dozer.util.mapping.converters;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.dozer.util.mapping.cache.Cache;
import net.sf.dozer.util.mapping.cache.CacheEntry;
import net.sf.dozer.util.mapping.cache.CacheKeyFactory;
import net.sf.dozer.util.mapping.cache.CacheManagerIF;
import net.sf.dozer.util.mapping.util.MapperConstants;

/**
 * @author sullins.ben
 */
public class CustomConverterContainer {

  private List converters = new ArrayList();

  public List getConverters() {
    return converters;
  }

  public void setConverters(List in) {
    this.converters = in;
  }

  public void addConverter(CustomConverterDescription converter) {
    getConverters().add(converter);
  }

  public Class getConverterByDestinationType(Class destinationClass, Class sourceClass, CacheManagerIF cacheMgr)
      throws ClassNotFoundException {
    // Check cache first
    Cache cache = cacheMgr.getCache(MapperConstants.CONVERTER_BY_DEST_TYPE_CACHE);
    Object cacheKey = CacheKeyFactory.createKey(new Object[] { destinationClass, sourceClass });
    CacheEntry cacheEntry = cache.get(cacheKey);
    if (cacheEntry != null) {
      return (Class) cacheEntry.getValue();
    }

    // Otherwise, determine the custom converter type. Also, store the result in the cache
    Iterator iter = getConverters().iterator();
    Class classA = null;
    Class classB = null;
    Class result = null;
    Class src = null;
    Class dest = null;
    while (iter.hasNext()) {
      CustomConverterDescription customConverter = (CustomConverterDescription) iter.next();
      classA = Thread.currentThread().getContextClassLoader().loadClass(customConverter.getClassA());
      classB = Thread.currentThread().getContextClassLoader().loadClass(customConverter.getClassB());

      // Let's see if the incoming class is a primitive:
      Class c = getWrapper(sourceClass);
      if (c != null) {
        src = c;
      } else {
        src = sourceClass;
      }
      Class c2 = getWrapper(destinationClass);
      if (c2 != null) {
        dest = c2;
      } else {
        dest = destinationClass;
      }
      // we check to see if the destination class is the same as classA defined in the converter mapping xml.
      // we next check if the source class is the same as classA defined in the converter mapping xml.
      // we also to check to see if it is assignable to either. We then perform these checks in the other direction for
      // classB
      if ((classA.isAssignableFrom(dest) || classA.isAssignableFrom(src))
          && (classB.isAssignableFrom(dest) || classB.isAssignableFrom(src))) {
        result = Thread.currentThread().getContextClassLoader().loadClass(customConverter.getType());
      }
    }
    // Add to cache - create a new key in case we had a primitive
    Object cacheKeyMod = CacheKeyFactory.createKey(new Object[] { dest, src });
    cacheEntry = new CacheEntry(cacheKeyMod, result);
    cache.put(cacheEntry);
    return result;
  }

  private Class getWrapper(Class c) {
    Class clazz = null;
    if (c.equals(Integer.TYPE)) {
      clazz = Integer.class;
    } else if (c.equals(Double.TYPE)) {
      clazz = Double.class;
    } else if (c.equals(Short.TYPE)) {
      clazz = Short.class;
    } else if (c.equals(Long.TYPE)) {
      clazz = Long.class;
    } else if (c.equals(Boolean.TYPE)) {
      clazz = Boolean.class;
    } else if (c.equals(Float.TYPE)) {
      clazz = Float.class;
    }
    return clazz;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -