diff --git a/.gitignore b/.gitignore index c602080..6e6d0a9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ test-output # Gradle .gradle +.DS_Store diff --git a/mapstruct-non-iterable-to-iterable/pom.xml b/mapstruct-non-iterable-to-iterable/pom.xml new file mode 100644 index 0000000..5eac8aa --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/pom.xml @@ -0,0 +1,39 @@ + + + + mapstruct-examples + org.mapstruct.examples + 1.0.0-SNAPSHOT + + 4.0.0 + + mapstruct-non-iterable-to-iterable + + + + + org.mapstruct + mapstruct + 1.2.0.Final + + + + org.mapstruct + mapstruct-processor + 1.2.0.Final + provided + + + + junit + junit + 4.12 + test + + + + + + \ No newline at end of file diff --git a/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Iterable.java b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Iterable.java new file mode 100644 index 0000000..97fa6c3 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Iterable.java @@ -0,0 +1,26 @@ +package com.mycompany.mapper; + +import java.util.List; + +public class Iterable { + + private List myIntegers; + private List myStrings; + + public List getMyIntegers() { + return myIntegers; + } + + public void setMyIntegers(List myIntegers) { + this.myIntegers = myIntegers; + } + + public List getMyStrings() { + return myStrings; + } + + public void setMyStrings(List myStrings) { + this.myStrings = myStrings; + } + +} diff --git a/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Main.java b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Main.java new file mode 100644 index 0000000..13b0eac --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Main.java @@ -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)); + } +} diff --git a/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/NonIterable.java b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/NonIterable.java new file mode 100644 index 0000000..1f10c2e --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/NonIterable.java @@ -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; + } + +} diff --git a/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/SourceTargetMapper.java b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/SourceTargetMapper.java new file mode 100644 index 0000000..ce2caa6 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/SourceTargetMapper.java @@ -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); +} diff --git a/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/NonIterableToIterable.java b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/NonIterableToIterable.java new file mode 100644 index 0000000..227c1d9 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/NonIterableToIterable.java @@ -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 List toList(T in) { + if (in != null) return Collections.singletonList(in); + else return new ArrayList(); + } +} diff --git a/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/ToList.java b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/ToList.java new file mode 100644 index 0000000..18eeb02 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/ToList.java @@ -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 { +} diff --git a/mapstruct-non-iterable-to-iterable/src/test/java/SourceToTargetMapperTest.java b/mapstruct-non-iterable-to-iterable/src/test/java/SourceToTargetMapperTest.java new file mode 100644 index 0000000..0621c70 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/test/java/SourceToTargetMapperTest.java @@ -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()); + } +} diff --git a/pom.xml b/pom.xml index 6348696..1dd2faf 100644 --- a/pom.xml +++ b/pom.xml @@ -39,5 +39,6 @@ mapstruct-protobuf3 mapstruct-updatemethods-1 mapstruct-kotlin + mapstructnoniterabletoiterable