Java tutorial
/** * 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 cn.hxh.springside.mapper; 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.apache.velocity.util.StringUtils; import org.dozer.DozerBeanMapper; import cn.hxh.springside.utils.ReflectionUtils; import com.google.common.collect.Lists; /** * ?. * 1.?Dozer, ? * 2.?Apache Commons BeanUtils, ?. * * @author calvin */ public abstract class ObjectMapper { /** * ?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); } } }