Skip to content

Commit

Permalink
bean copy封装,用于client Object等之间的copy
Browse files Browse the repository at this point in the history
  • Loading branch information
SoftwareKing committed Jul 7, 2018
1 parent d1b53a0 commit 2bf7789
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
44 changes: 44 additions & 0 deletions halo-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.xujin.halo</groupId>
<artifactId>halo</artifactId>
<version>1.0.4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>halo-utils</artifactId>
<version>1.0.4</version>
<name>halo-utils</name>

<dependencies>

<!--bean copy-->
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<optional>true</optional>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.xujin.halo.utils.reflect;

import java.util.List;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeFactory;

/**
* @author xujin</br>
* 简单封装orika, 实现深度的BeanOfClasssA<->BeanOfClassB复制 不要是用Apache Common
* BeanUtils进行类复制,每次就行反射查询对象的属性列表, 非常缓慢.
*/
public class BeanMapper {

private static MapperFacade mapper;

static {
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapper = mapperFactory.getMapperFacade();
}

/**
* 简单的复制出新类型对象.
*
* 通过source.getClass() 获得源Class
*/
public static <S, D> D map(S source, Class<D> destinationClass) {
return mapper.map(source, destinationClass);
}

/**
* 极致性能的复制出新类型对象.
*
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入
*/
public static <S, D> D map(S source, Type<S> sourceType, Type<D> destinationType) {
return mapper.map(source, sourceType, destinationType);
}

/**
* 简单的复制出新对象列表到ArrayList
*
* 不建议使用mapper.mapAsList(Iterable<S>,Class<D>)接口, sourceClass需要反射,实在有点慢
*/
public static <S, D> List<D> mapList(Iterable<S> sourceList, Class<S> sourceClass,
Class<D> destinationClass) {
return mapper.mapAsList(sourceList, TypeFactory.valueOf(sourceClass),
TypeFactory.valueOf(destinationClass));
}

/**
* 极致性能的复制出新类型对象到ArrayList.
*
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入
*/
public static <S, D> List<D> mapList(Iterable<S> sourceList, Type<S> sourceType,
Type<D> destinationType) {
return mapper.mapAsList(sourceList, sourceType, destinationType);
}

/**
* 简单复制出新对象列表到数组
*
* 通过source.getComponentType() 获得源Class
*/
public static <S, D> D[] mapArray(final D[] destination, final S[] source,
final Class<D> destinationClass) {
return mapper.mapAsArray(destination, source, destinationClass);
}

/**
* 极致性能的复制出新类型对象到数组
*
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入
*/
public static <S, D> D[] mapArray(D[] destination, S[] source, Type<S> sourceType,
Type<D> destinationType) {
return mapper.mapAsArray(destination, source, sourceType, destinationType);
}

/**
* 预先获取orika转换所需要的Type,避免每次转换.
*/
public static <E> Type<E> getType(final Class<E> rawType) {
return TypeFactory.valueOf(rawType);
}
}
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<halo.framework.version>1.0.4</halo.framework.version>
<toolkit.common.lang.version>1.0</toolkit.common.lang.version>
<jakarta.commons.beanutils.version>1.8.3</jakarta.commons.beanutils.version>
<orika.version>1.5.0</orika.version>
</properties>

<modules>
Expand All @@ -43,6 +44,7 @@
<module>halo-event</module>
<module>halo-collection</module>
<module>halo-base</module>
<module>halo-utils</module>
</modules>

<dependencyManagement>
Expand Down Expand Up @@ -188,6 +190,14 @@
<version>2.2.6</version>
</dependency>
<!--Validation API End -->

<!--bean copy-->
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>${orika.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 2bf7789

Please sign in to comment.