From 8871f788d2564ff383e5527a6db45aa5b3edcc79 Mon Sep 17 00:00:00 2001 From: Kamil Pietruszka Date: Fri, 10 Mar 2017 20:08:58 +0100 Subject: [PATCH] #497 Converter pattern implementation --- converter/README.md | 25 ++++++ converter/pom.xml | 21 +++++ .../main/java/com/iluwatar/converter/App.java | 44 ++++++++++ .../com/iluwatar/converter/Converter.java | 86 +++++++++++++++++++ .../java/com/iluwatar/converter/User.java | 69 +++++++++++++++ .../java/com/iluwatar/converter/UserDto.java | 69 +++++++++++++++ .../java/com/iluwatar/converter/AppTest.java | 35 ++++++++ pom.xml | 3 +- 8 files changed, 351 insertions(+), 1 deletion(-) create mode 100644 converter/README.md create mode 100644 converter/pom.xml create mode 100644 converter/src/main/java/com/iluwatar/converter/App.java create mode 100644 converter/src/main/java/com/iluwatar/converter/Converter.java create mode 100644 converter/src/main/java/com/iluwatar/converter/User.java create mode 100644 converter/src/main/java/com/iluwatar/converter/UserDto.java create mode 100644 converter/src/test/java/com/iluwatar/converter/AppTest.java diff --git a/converter/README.md b/converter/README.md new file mode 100644 index 000000000..cbca98e8f --- /dev/null +++ b/converter/README.md @@ -0,0 +1,25 @@ +--- +layout: pattern +title: Converter +folder: converter +permalink: /patterns/converter/ +categories: +tags: + - Java + - Difficulty-Beginner +--- + +## Intent +TODO + +![alt text](./etc/converter.png "TODO") + +## Applicability +TODO + +* TODO 1 +* TODO 2 + +## Credits + +* [Converter](http://todo.com) diff --git a/converter/pom.xml b/converter/pom.xml new file mode 100644 index 000000000..53eca720b --- /dev/null +++ b/converter/pom.xml @@ -0,0 +1,21 @@ + + + + java-design-patterns + com.iluwatar + 1.15.0-SNAPSHOT + + 4.0.0 + + + junit + junit + test + + + converter + + + diff --git a/converter/src/main/java/com/iluwatar/converter/App.java b/converter/src/main/java/com/iluwatar/converter/App.java new file mode 100644 index 000000000..593240b43 --- /dev/null +++ b/converter/src/main/java/com/iluwatar/converter/App.java @@ -0,0 +1,44 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.converter; + +/** + * + * + */ +public class App { + /** + * Program entry point + * + * @param args command line args + */ + public static void main(String[] args) { + Converter userConverter = new Converter<>( + userDto -> new User(userDto.getName(), userDto.getSurname(), userDto.isActive()), + user -> new UserDto(user.getName(), user.getSurname(), user.isActive())); + UserDto dtoUser = new UserDto("John", "Doe", true); + User user = userConverter.convertFromDTO(dtoUser); + UserDto dtoUserCopy = userConverter.convertFromEntity(user); + + } +} diff --git a/converter/src/main/java/com/iluwatar/converter/Converter.java b/converter/src/main/java/com/iluwatar/converter/Converter.java new file mode 100644 index 000000000..0b3531a22 --- /dev/null +++ b/converter/src/main/java/com/iluwatar/converter/Converter.java @@ -0,0 +1,86 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.converter; + +import java.util.Collection; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * @param + * @param + */ +public class Converter { + /** + * + */ + private final Function fromDTO; + /** + * + */ + private final Function fromEntity; + + /** + * @param fromDTO + * @param fromEntity + */ + public Converter(final Function fromDTO, final Function fromEntity) { + this.fromDTO = fromDTO; + this.fromEntity = fromEntity; + } + + /** + * @param arg + * @return + */ + public U convertFromDTO(final T arg) { + return fromDTO.apply(arg); + } + + /** + * @param arg + * @return + */ + public T convertFromEntity(final U arg) { + return fromEntity.apply(arg); + } + + /** + * @param arg + * @return + */ + public List createFromDTOs(final Collection arg) { + return arg.stream().map(this::convertFromDTO).collect(Collectors.toList()); + } + + /** + * @param arg + * @return + */ + public List createFromEntities(final Collection arg) { + return arg.stream().map(this::convertFromEntity).collect(Collectors.toList()); + } + +} diff --git a/converter/src/main/java/com/iluwatar/converter/User.java b/converter/src/main/java/com/iluwatar/converter/User.java new file mode 100644 index 000000000..bcd09c1cc --- /dev/null +++ b/converter/src/main/java/com/iluwatar/converter/User.java @@ -0,0 +1,69 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.converter; + +/** + * Created by crossy on 2017-03-10. + */ +public class User { + private String name; + private String surname; + private boolean isActive; + + /** + * + * @param name + * @param surname + * @param isActive + */ + public User(String name, String surname, boolean isActive) { + this.name = name; + this.surname = surname; + this.isActive = isActive; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public boolean isActive() { + return isActive; + } + + public void setActive(boolean active) { + isActive = active; + } +} diff --git a/converter/src/main/java/com/iluwatar/converter/UserDto.java b/converter/src/main/java/com/iluwatar/converter/UserDto.java new file mode 100644 index 000000000..aaff1e623 --- /dev/null +++ b/converter/src/main/java/com/iluwatar/converter/UserDto.java @@ -0,0 +1,69 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.converter; + +/** + * + */ +public class UserDto { + private String name; + private String surname; + private boolean isActive; + + /** + * + * @param name + * @param surname + * @param isActive + */ + public UserDto(String name, String surname, boolean isActive) { + this.name = name; + this.surname = surname; + this.isActive = isActive; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public boolean isActive() { + return isActive; + } + + public void setActive(boolean active) { + isActive = active; + } +} diff --git a/converter/src/test/java/com/iluwatar/converter/AppTest.java b/converter/src/test/java/com/iluwatar/converter/AppTest.java new file mode 100644 index 000000000..5198827d2 --- /dev/null +++ b/converter/src/test/java/com/iluwatar/converter/AppTest.java @@ -0,0 +1,35 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.converter; + +import org.junit.Test; + +public class AppTest { + + @Test + public void testMain() { + String[] args = {}; + App.main(args); + } + +} diff --git a/pom.xml b/pom.xml index 5ddd3bf98..8a012665d 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,7 @@ event-asynchronous queue-load-leveling object-mother + converter @@ -466,4 +467,4 @@ - \ No newline at end of file +