Skip to content

Commit 8871f78

Browse files
committed
iluwatar#497 Converter pattern implementation
1 parent e9c5401 commit 8871f78

8 files changed

Lines changed: 351 additions & 1 deletion

File tree

converter/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
layout: pattern
3+
title: Converter
4+
folder: converter
5+
permalink: /patterns/converter/
6+
categories:
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
TODO
14+
15+
![alt text](./etc/converter.png "TODO")
16+
17+
## Applicability
18+
TODO
19+
20+
* TODO 1
21+
* TODO 2
22+
23+
## Credits
24+
25+
* [Converter](http://todo.com)

converter/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>com.iluwatar</groupId>
8+
<version>1.15.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
<artifactId>converter</artifactId>
19+
20+
21+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.converter;
24+
25+
/**
26+
*
27+
*
28+
*/
29+
public class App {
30+
/**
31+
* Program entry point
32+
*
33+
* @param args command line args
34+
*/
35+
public static void main(String[] args) {
36+
Converter<UserDto, User> userConverter = new Converter<>(
37+
userDto -> new User(userDto.getName(), userDto.getSurname(), userDto.isActive()),
38+
user -> new UserDto(user.getName(), user.getSurname(), user.isActive()));
39+
UserDto dtoUser = new UserDto("John", "Doe", true);
40+
User user = userConverter.convertFromDTO(dtoUser);
41+
UserDto dtoUserCopy = userConverter.convertFromEntity(user);
42+
43+
}
44+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.converter;
25+
26+
import java.util.Collection;
27+
import java.util.List;
28+
import java.util.function.Function;
29+
import java.util.stream.Collectors;
30+
31+
/**
32+
* @param <T>
33+
* @param <U>
34+
*/
35+
public class Converter<T, U> {
36+
/**
37+
*
38+
*/
39+
private final Function<T, U> fromDTO;
40+
/**
41+
*
42+
*/
43+
private final Function<U, T> fromEntity;
44+
45+
/**
46+
* @param fromDTO
47+
* @param fromEntity
48+
*/
49+
public Converter(final Function<T, U> fromDTO, final Function<U, T> fromEntity) {
50+
this.fromDTO = fromDTO;
51+
this.fromEntity = fromEntity;
52+
}
53+
54+
/**
55+
* @param arg
56+
* @return
57+
*/
58+
public U convertFromDTO(final T arg) {
59+
return fromDTO.apply(arg);
60+
}
61+
62+
/**
63+
* @param arg
64+
* @return
65+
*/
66+
public T convertFromEntity(final U arg) {
67+
return fromEntity.apply(arg);
68+
}
69+
70+
/**
71+
* @param arg
72+
* @return
73+
*/
74+
public List<U> createFromDTOs(final Collection<T> arg) {
75+
return arg.stream().map(this::convertFromDTO).collect(Collectors.toList());
76+
}
77+
78+
/**
79+
* @param arg
80+
* @return
81+
*/
82+
public List<T> createFromEntities(final Collection<U> arg) {
83+
return arg.stream().map(this::convertFromEntity).collect(Collectors.toList());
84+
}
85+
86+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.converter;
25+
26+
/**
27+
* Created by crossy on 2017-03-10.
28+
*/
29+
public class User {
30+
private String name;
31+
private String surname;
32+
private boolean isActive;
33+
34+
/**
35+
*
36+
* @param name
37+
* @param surname
38+
* @param isActive
39+
*/
40+
public User(String name, String surname, boolean isActive) {
41+
this.name = name;
42+
this.surname = surname;
43+
this.isActive = isActive;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public String getSurname() {
55+
return surname;
56+
}
57+
58+
public void setSurname(String surname) {
59+
this.surname = surname;
60+
}
61+
62+
public boolean isActive() {
63+
return isActive;
64+
}
65+
66+
public void setActive(boolean active) {
67+
isActive = active;
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.converter;
25+
26+
/**
27+
*
28+
*/
29+
public class UserDto {
30+
private String name;
31+
private String surname;
32+
private boolean isActive;
33+
34+
/**
35+
*
36+
* @param name
37+
* @param surname
38+
* @param isActive
39+
*/
40+
public UserDto(String name, String surname, boolean isActive) {
41+
this.name = name;
42+
this.surname = surname;
43+
this.isActive = isActive;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public String getSurname() {
55+
return surname;
56+
}
57+
58+
public void setSurname(String surname) {
59+
this.surname = surname;
60+
}
61+
62+
public boolean isActive() {
63+
return isActive;
64+
}
65+
66+
public void setActive(boolean active) {
67+
isActive = active;
68+
}
69+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.converter;
24+
25+
import org.junit.Test;
26+
27+
public class AppTest {
28+
29+
@Test
30+
public void testMain() {
31+
String[] args = {};
32+
App.main(args);
33+
}
34+
35+
}

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<module>event-asynchronous</module>
135135
<module>queue-load-leveling</module>
136136
<module>object-mother</module>
137+
<module>converter</module>
137138
</modules>
138139

139140
<dependencyManagement>
@@ -466,4 +467,4 @@
466467
</plugins>
467468
</reporting>
468469

469-
</project>
470+
</project>

0 commit comments

Comments
 (0)