Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non iterable to iterable example #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ test-output

# Gradle
.gradle
.DS_Store
39 changes: 39 additions & 0 deletions mapstruct-non-iterable-to-iterable/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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>
<artifactId>mapstruct-examples</artifactId>
<groupId>org.mapstruct.examples</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mapstruct-non-iterable-to-iterable</artifactId>

<dependencies>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.2.0.Final</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
<scope>provided</scope>
</dependency>

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

</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mycompany.mapper;

import java.util.List;

public class Iterable {

private List<Integer> myIntegers;
private List<String> myStrings;

public List<Integer> getMyIntegers() {
return myIntegers;
}

public void setMyIntegers(List<Integer> myIntegers) {
this.myIntegers = myIntegers;
}

public List<String> getMyStrings() {
return myStrings;
}

public void setMyStrings(List<String> myStrings) {
this.myStrings = myStrings;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mycompany.mapper;

public class Main {

public static void main(String[] args) {
NonIterable nonIterable = new NonIterable();
nonIterable.setMyInteger(1);
nonIterable.setMyString("hello");

Iterable iterable = SourceTargetMapper.MAPPER.toTarget(nonIterable);
System.out.println(iterable.getMyIntegers().get(0));
System.out.println(iterable.getMyStrings().get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mycompany.mapper;


public class NonIterable {

private Integer myInteger;
private String myString;

public Integer getMyInteger() {
return myInteger;
}

public void setMyInteger(Integer myInteger) {
this.myInteger = myInteger;
}

public String getMyString() {
return myString;
}

public void setMyString(String myString) {
this.myString = myString;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mycompany.mapper;

import com.mycompany.mapper.util.NonIterableToIterable;
import com.mycompany.mapper.util.ToList;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;


@Mapper(uses = NonIterableToIterable.class)
public interface SourceTargetMapper {

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

@Mappings( {
@Mapping( target = "myIntegers", source = "myInteger", qualifiedBy = ToList.class ),
@Mapping( target = "myStrings", source = "myString", qualifiedBy = ToList.class )
} )
Iterable toTarget(NonIterable s);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mycompany.mapper.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NonIterableToIterable {

@ToList
public <T> List<T> toList(T in) {
if (in != null) return Collections.singletonList(in);
else return new ArrayList<T>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mycompany.mapper.util;

import org.mapstruct.Qualifier;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface ToList {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import com.mycompany.mapper.Iterable;
import com.mycompany.mapper.NonIterable;
import com.mycompany.mapper.SourceTargetMapper;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class SourceToTargetMapperTest {

@Test
public void testToTarget() {

NonIterable nonIterable = new NonIterable();
nonIterable.setMyInteger(1);
nonIterable.setMyString("hello");

Iterable iterable = SourceTargetMapper.MAPPER.toTarget(nonIterable);

assertEquals(iterable.getMyIntegers().get(0), nonIterable.getMyInteger());
assertEquals(iterable.getMyStrings().get(0), nonIterable.getMyString());
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
<module>mapstruct-protobuf3</module>
<module>mapstruct-updatemethods-1</module>
<module>mapstruct-kotlin</module>
<module>mapstructnoniterabletoiterable</module>
</modules>
</project>