com.mawujun.util.ObjectConvert.java Source code

Java tutorial

Introduction

Here is the source code for com.mawujun.util.ObjectConvert.java

Source

/**
 * Copyright (c) 2005-2011 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: ObjectMapper.java 1627 2011-05-23 16:23:18Z calvinxiu $
 */
package com.mawujun.util;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.dozer.DozerBeanMapper;

import com.google.common.collect.Lists;

/**
 * ?.
 * 1.?Dozer, ?
 * 2.?Apache Commons BeanUtils, ?.
 * 
 * @author calvin
 */
public abstract class ObjectConvert {

    /**
     * ?Dozer?, ????DozerMapper?.
     */
    private static DozerBeanMapper dozer = new DozerBeanMapper();

    /**
     * Dozer?.
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        return dozer.map(source, destinationClass);
    }

    /**
     * Dozer?Collection.
     */
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        List<T> destinationList = Lists.newArrayList();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    static {
        //??yyyy-MM-dd  yyyy-MM-dd HH:mm:ss
        registerDateConverter("yyyy-MM-dd,yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Apache BeanUtilsConverter?,??,','
     */
    public static void registerDateConverter(String patterns) {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(StringUtils.split(patterns, ","));
        ConvertUtils.register(dc, Date.class);
    }

    /**
     * Apache BeanUtils?.
     * 
     * @param value ?.
     * @param toType ?.
     */
    public static Object convertToObject(String value, Class<?> toType) {
        try {
            return ConvertUtils.convert(value, toType);
        } catch (Exception e) {
            throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
        }
    }

    public static boolean isWrapClass(Class clz) {
        try {
            return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) {
            return false;
        }
    }

}