Skip to content

Commit

Permalink
adding deep cloner (mapstruct#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaakd authored Jan 31, 2019
1 parent 8dd8efc commit d73e3c6
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Currently, the following examples exist:
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
* _mapstruct-suppress-unmapped_: Shows how mapping to target properties can be ignored without warning by default in a mixed scenario. However bean property mappings that have the same name will still be applied.
* _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method.
* _mapstruct-clone_: Shows how an object can be deeply cloned by defining all mapping methods.
* _mapstruct-metadata-annotations_: Demonstrates how to read annotations and use them as mapping instruction.

## License
Expand Down
78 changes: 78 additions & 0 deletions mapstruct-clone/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.
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.
-->
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>org.mapstruct.examples.clone</groupId>
<artifactId>mapstruct-deep-clone</artifactId>
<version>1.0.0</version>

<properties>
<org.mapstruct.version>1.3.0.Beta2</org.mapstruct.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.6.2</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.mapstruct.example.dto;

import java.util.List;
import java.util.Map;

/**
* @author Sjaak Derksen
*/
public class CustomerDto {

private Long id;
private String customerName;
private List<OrderItemDto> orders;
private Map<OrderItemKeyDto, OrderItemDto> stock;


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public List<OrderItemDto> getOrders() {
return orders;
}

public void setOrders(List<OrderItemDto> orders) {
this.orders = orders;
}

public Map<OrderItemKeyDto, OrderItemDto> getStock() {
return stock;
}

public void setStock(Map<OrderItemKeyDto, OrderItemDto> stock) {
this.stock = stock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.mapstruct.example.dto;

/**
* @author Sjaak Derksen
*/
public class OrderItemDto {

private String name;
private Long quantity;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Long getQuantity() {
return quantity;
}

public void setQuantity(Long quantity) {
this.quantity = quantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mapstruct.example.dto;

/**
* @author Sjaak Derksen
*/
public class OrderItemKeyDto {

private long stockNumber;

public long getStockNumber() {
return stockNumber;
}

public void setStockNumber(long stockNumber) {
this.stockNumber = stockNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.mapstruct.example.mapper;

import java.util.List;
import java.util.Map;

import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.example.dto.CustomerDto;
import org.mapstruct.example.dto.OrderItemDto;
import org.mapstruct.example.dto.OrderItemKeyDto;
import org.mapstruct.factory.Mappers;

/**
* @author Sjaak Derksen
*
* By defining all methods, we force MapStruct to generate new objects for each mapper in stead of
* taking shortcuts by mapping an object directly.
*/
@Mapper
public interface Cloner {

Cloner MAPPER = Mappers.getMapper( Cloner.class );

CustomerDto clone(CustomerDto customerDto);

List<OrderItemDto> clone(List<OrderItemDto> orders);

OrderItemDto clone(OrderItemDto order);

Map<OrderItemKeyDto, OrderItemDto> clone(Map<OrderItemKeyDto, OrderItemDto> stock);

OrderItemKeyDto clone( OrderItemKeyDto orderItemKeyDto );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* 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 org.mapstruct.example;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

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

import org.junit.Test;
import org.mapstruct.example.dto.CustomerDto;
import org.mapstruct.example.dto.OrderItemDto;
import org.mapstruct.example.dto.OrderItemKeyDto;
import org.mapstruct.example.mapper.Cloner;

/**
*
* @author Sjaak Derksen
*/
public class ClonerTest {

@Test
public void testMapDtoToEntity() {

CustomerDto customerDto = new CustomerDto();
customerDto.setId( 10L );
customerDto.setCustomerName("Jaques" );
OrderItemDto order1 = new OrderItemDto();
order1.setName ("Table" );
order1.setQuantity( 2L );
customerDto.setOrders( new ArrayList<>( Collections.singleton( order1 ) ) );
OrderItemKeyDto key = new OrderItemKeyDto();
key.setStockNumber( 5 );
Map stock = new HashMap( );
stock.put( key, order1 );
customerDto.setStock( stock );

CustomerDto customer = Cloner.MAPPER.clone( customerDto );

// check if cloned
assertThat( customer.getId() ).isEqualTo( 10 );
assertThat( customer.getCustomerName() ).isEqualTo( "Jaques" );
assertThat( customer.getOrders() )
.extracting( "name", "quantity" )
.containsExactly( tuple( "Table", 2L ) );
assertThat( customer.getStock() ).isNotNull();
assertThat( customer.getStock() ).hasSize( 1 );

Map.Entry<OrderItemKeyDto, OrderItemDto> entry = customer.getStock().entrySet().iterator().next();
assertThat( entry.getKey().getStockNumber() ).isEqualTo( 5 );
assertThat( entry.getValue().getName() ).isEqualTo( "Table" );
assertThat( entry.getValue().getQuantity() ).isEqualTo( 2L );

// check mapper really created new objects
assertThat( customer ).isNotSameAs( customerDto );
assertThat( customer.getOrders().get( 0 ) ).isNotEqualTo( order1 );
assertThat( entry.getKey() ).isNotEqualTo( key );
assertThat( entry.getValue() ).isNotEqualTo( order1 );
assertThat( entry.getValue() ).isNotEqualTo( customer.getOrders().get( 0 ) );
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
<module>mapstruct-lookup-entity-with-id</module>
<module>mapstruct-metadata-with-annotations</module>
<module>mapstruct-suppress-unmapped</module>
<module>mapstruct-clone</module>
</modules>
</project>

0 comments on commit d73e3c6

Please sign in to comment.