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

📄 mappingutils.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.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.sf.dozer.util.mapping.MappingException;
import net.sf.dozer.util.mapping.cache.CacheManagerIF;
import net.sf.dozer.util.mapping.converters.CustomConverterContainer;
import net.sf.dozer.util.mapping.fieldmap.Field;
import net.sf.dozer.util.mapping.fieldmap.FieldMap;
import net.sf.dozer.util.mapping.fieldmap.GenericFieldMap;

import org.apache.commons.lang.StringUtils;

/**
 * @author garsombke.franz
 * @author sullins.ben
 * @author tierney.matt
 * 
 */
public class MappingUtils {

  //only making public temporarily while refactoring.  This static data should be relocated
  public static Map storedFactories = Collections.synchronizedMap(new HashMap());
  
  private final CollectionUtils collectionUtils = new CollectionUtils();
  
  public String getClassNameWithoutPackage(Class clazz) {
    Package pckage = clazz.getPackage();
    int pckageIndex = 0;
    if (pckage != null) {
      pckageIndex = pckage.getName().length() + 1;
    }
    return clazz.getName().substring(pckageIndex);
  }

  public boolean isSupportedCollection(Class aClass) {
    boolean collection = false;
    if (collectionUtils.isArray(aClass)) {
      collection = true;
    } else if (collectionUtils.isList(aClass)) {
      collection = true;
    } else if (collectionUtils.isSet(aClass)) {
      collection = true;
    } else if (collectionUtils.isIterator(aClass)) {
      collection = true;
    } else if (collectionUtils.isCollection(aClass)) {
      collection = true;
    }
    return collection;
  }

  public boolean isSupportedMap(Class aClass) {
    return Map.class.isAssignableFrom(aClass);
  }

  public boolean isCustomMapMethod(FieldMap fieldMap) {
    return (fieldMap instanceof GenericFieldMap && ((GenericFieldMap) fieldMap).isCustomMap()) ? true : false;
  }

  /**
   * Check if the given class represents a primitive array, i.e. boolean, byte, char, short, int, long, float, or
   * double.
   */
  public boolean isPrimitiveArray(Class clazz) {
    return boolean[].class.equals(clazz) || byte[].class.equals(clazz) || char[].class.equals(clazz)
        || short[].class.equals(clazz) || int[].class.equals(clazz) || long[].class.equals(clazz)
        || float[].class.equals(clazz) || double[].class.equals(clazz);
  }

  public boolean isPrimitiveOrWrapper(Class aClass) {
    return (aClass.isPrimitive() || Number.class.isAssignableFrom(aClass) || aClass.equals(String.class)
        || aClass.equals(Character.class) || aClass.equals(Boolean.class)
        || java.util.Date.class.isAssignableFrom(aClass) || java.util.Calendar.class.isAssignableFrom(aClass));
  }

  public void throwMappingException(Throwable e) throws MappingException {
    if (e instanceof MappingException) {
      // in this case we do not want to re-wrap an existing mapping exception
      throw (MappingException) e;
    } else {
      throw new MappingException(e);
    }
  }

  public boolean isBlankOrNull(String value) {
    return value == null || value.trim().length() < 1 ? true : false;
  }

  public void addFactories(Map factories) {
    storedFactories.putAll(factories);
  }

  public void isMethodMap(FieldMap fieldMap) {
    boolean methodMap = false;
    if (fieldMap.getSourceField().getTheGetMethod() != null || fieldMap.getSourceField().getTheSetMethod() != null
        || fieldMap.getDestField().getTheGetMethod() != null || fieldMap.getDestField().getTheSetMethod() != null) {
      methodMap = true;
    }
    if (methodMap && fieldMap instanceof GenericFieldMap) {
      ((GenericFieldMap) fieldMap).setMethodMap(true);
    }
  }

  public void isCustomMap(FieldMap fieldMap) {
    boolean customMap = false;
    if (fieldMap.getSourceField().getMapGetMethod() != null || fieldMap.getSourceField().getMapSetMethod() != null
        || fieldMap.getDestField().getMapGetMethod() != null || fieldMap.getDestField().getMapSetMethod() != null) {
      customMap = true;
    }
    if (customMap && fieldMap instanceof GenericFieldMap) {
      ((GenericFieldMap) fieldMap).setCustomMap(true);
    }
  }

  public Throwable getRootCause(Throwable ex) {
    Throwable rootCause = ex;
    while (rootCause.getCause() != null) {
      rootCause = rootCause.getCause();
    }
    return rootCause;
  }

  public String getParentFieldNameKey(String parentSourceField, Object srcObj, String sourceClassName, String srcFieldReadMethodName,
      String destFieldReadMethodName, String srcFieldName, String destFieldName) {
    StringBuffer buf = new StringBuffer(150);
    buf.append(parentSourceField);
    buf.append(System.identityHashCode(srcObj));
    buf.append(sourceClassName);
    buf.append(srcFieldReadMethodName);
    buf.append(destFieldReadMethodName);
    buf.append(srcFieldName);
    buf.append(destFieldName);
    return buf.toString();
  }
 
  public Class getCustomConverterFromContainer(CacheManagerIF cacheMgr, CustomConverterContainer customConverterContainer, Class sourceClass, Class destClass) throws ClassNotFoundException {
    // first test if this top level might just be handled by a customConverter, this is rare
    Class converterClass = null;
    if (customConverterContainer != null) {
      converterClass = customConverterContainer.getConverterByDestinationType(destClass, sourceClass, cacheMgr);
    }
    return converterClass;
  }
  
  public void reverseFields(FieldMap source, FieldMap destination) {
    // reverse the fields
    Field df = new Field(source.getSourceField().getName(), source.getSourceField().getType());
    df.setIndexed(source.getSourceField().isIndexed());
    df.setIndex(source.getSourceField().getIndex());

    Field sf = new Field(source.getDestField().getName(), source.getDestField().getType());
    sf.setIndexed(source.getDestField().isIndexed());
    sf.setIndex(source.getDestField().getIndex());

    destination.setDestField(df);
    destination.setSourceField(sf);

    destination.getDestField().setDateFormat(source.getSourceField().getDateFormat());
    destination.getSourceField().setDateFormat(source.getDestField().getDateFormat());

    destination.getDestField().setTheGetMethod(source.getSourceField().getTheGetMethod());
    destination.getDestField().setTheSetMethod(source.getSourceField().getTheSetMethod());
    destination.getSourceField().setTheGetMethod(source.getDestField().getTheGetMethod());
    destination.getSourceField().setTheSetMethod(source.getDestField().getTheSetMethod());
    destination.getDestField().setKey(source.getSourceField().getKey());
    destination.getSourceField().setKey(source.getDestField().getKey());
    destination.getDestField().setMapGetMethod(source.getSourceField().getMapGetMethod());
    destination.getDestField().setMapSetMethod(source.getSourceField().getMapSetMethod());
    destination.getSourceField().setMapGetMethod(source.getDestField().getMapGetMethod());
    destination.getSourceField().setMapSetMethod(source.getDestField().getMapSetMethod());
    destination.getSourceField().setAccessible(source.getDestField().isAccessible());
    if (StringUtils.isNotEmpty(destination.getMapId())) {
      destination.setMapId(source.getMapId());
    }
    destination.getDestField().setCreateMethod(source.getSourceField().getCreateMethod());
    destination.getSourceField().setCreateMethod(source.getDestField().getCreateMethod());
  }
  
  public boolean validateMap(Class sourceClass, Class destClass, FieldMap fieldMap) {
    if (Map.class.isAssignableFrom(sourceClass) || fieldMap.getSourceField().getMapGetMethod() != null ||
        Map.class.isAssignableFrom(destClass) || fieldMap.getDestField().getMapGetMethod() != null) {
      return true;
    } else {
      return false;
    }
  }
}

⌨️ 快捷键说明

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