From 1f58f53388995804f46ae8f14a25551e3d975a66 Mon Sep 17 00:00:00 2001 From: snaplucas Date: Sat, 21 Oct 2017 19:59:14 -0200 Subject: [PATCH 1/2] Add non iterable to iterable example --- .gitignore | 1 + mapstruct-non-iterable-to-iterable/pom.xml | 39 +++++++++++++++++++ .../java/com/mycompany/mapper/Iterable.java | 28 +++++++++++++ .../main/java/com/mycompany/mapper/Main.java | 17 ++++++++ .../com/mycompany/mapper/NonIterable.java | 25 ++++++++++++ .../mycompany/mapper/SourceTargetMapper.java | 39 +++++++++++++++++++ .../mapper/util/NonIterableToIterable.java | 17 ++++++++ .../com/mycompany/mapper/util/ToList.java | 14 +++++++ .../test/java/SourceToTargetMapperTest.java | 22 +++++++++++ pom.xml | 1 + 10 files changed, 203 insertions(+) create mode 100644 mapstruct-non-iterable-to-iterable/pom.xml create mode 100644 mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Iterable.java create mode 100644 mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Main.java create mode 100644 mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/NonIterable.java create mode 100644 mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/SourceTargetMapper.java create mode 100644 mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/NonIterableToIterable.java create mode 100644 mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/ToList.java create mode 100644 mapstruct-non-iterable-to-iterable/src/test/java/SourceToTargetMapperTest.java 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..94d826c --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Iterable.java @@ -0,0 +1,28 @@ +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..5292151 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/Main.java @@ -0,0 +1,17 @@ +package com.mycompany.mapper; + +public class Main { + + private 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..af2e7f1 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/SourceTargetMapper.java @@ -0,0 +1,39 @@ +/** + * 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 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..2ba2d39 --- /dev/null +++ b/mapstruct-non-iterable-to-iterable/src/main/java/com/mycompany/mapper/util/NonIterableToIterable.java @@ -0,0 +1,17 @@ +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 From 34982f42f5f6e76fb168b4a5685e02e176503170 Mon Sep 17 00:00:00 2001 From: snaplucas Date: Sun, 22 Oct 2017 21:41:20 -0200 Subject: [PATCH 2/2] reformating nonIterableToIterable example --- .../java/com/mycompany/mapper/Iterable.java | 2 -- .../main/java/com/mycompany/mapper/Main.java | 3 --- .../mycompany/mapper/SourceTargetMapper.java | 22 ++----------------- .../mapper/util/NonIterableToIterable.java | 7 ++---- 4 files changed, 4 insertions(+), 30 deletions(-) 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 index 94d826c..97fa6c3 100644 --- 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 @@ -4,7 +4,6 @@ public class Iterable { - private List myIntegers; private List myStrings; @@ -24,5 +23,4 @@ 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 index 5292151..13b0eac 100644 --- 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 @@ -2,9 +2,6 @@ public class Main { - private Main() { - } - public static void main(String[] args) { NonIterable nonIterable = new NonIterable(); nonIterable.setMyInteger(1); 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 index af2e7f1..ce2caa6 100644 --- 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 @@ -1,21 +1,3 @@ -/** - * 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 com.mycompany.mapper; import com.mycompany.mapper.util.NonIterableToIterable; @@ -26,10 +8,10 @@ import org.mapstruct.factory.Mappers; -@Mapper( uses = NonIterableToIterable.class ) +@Mapper(uses = NonIterableToIterable.class) public interface SourceTargetMapper { - SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class ); + SourceTargetMapper MAPPER = Mappers.getMapper(SourceTargetMapper.class); @Mappings( { @Mapping( target = "myIntegers", source = "myInteger", qualifiedBy = ToList.class ), 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 index 2ba2d39..227c1d9 100644 --- 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 @@ -8,10 +8,7 @@ public class NonIterableToIterable { @ToList public List toList(T in) { - if (in != null) { - return Collections.singletonList(in); - } else { - return new ArrayList(); - } + if (in != null) return Collections.singletonList(in); + else return new ArrayList(); } }