#497 Converter pattern implementation
This commit is contained in:
		
							
								
								
									
										25
									
								
								converter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								converter/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
---
 | 
			
		||||
layout: pattern
 | 
			
		||||
title: Converter
 | 
			
		||||
folder: converter
 | 
			
		||||
permalink: /patterns/converter/
 | 
			
		||||
categories:
 | 
			
		||||
tags:
 | 
			
		||||
 - Java
 | 
			
		||||
 - Difficulty-Beginner
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## Intent
 | 
			
		||||
TODO
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
## Applicability
 | 
			
		||||
TODO
 | 
			
		||||
 | 
			
		||||
* TODO 1
 | 
			
		||||
* TODO 2
 | 
			
		||||
 | 
			
		||||
## Credits
 | 
			
		||||
 | 
			
		||||
* [Converter](http://todo.com)
 | 
			
		||||
							
								
								
									
										21
									
								
								converter/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								converter/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
<?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>java-design-patterns</artifactId>
 | 
			
		||||
        <groupId>com.iluwatar</groupId>
 | 
			
		||||
        <version>1.15.0-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>junit</groupId>
 | 
			
		||||
            <artifactId>junit</artifactId>
 | 
			
		||||
            <scope>test</scope>
 | 
			
		||||
        </dependency>
 | 
			
		||||
    </dependencies>
 | 
			
		||||
    <artifactId>converter</artifactId>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</project>
 | 
			
		||||
							
								
								
									
										44
									
								
								converter/src/main/java/com/iluwatar/converter/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								converter/src/main/java/com/iluwatar/converter/App.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
/**
 | 
			
		||||
 * The MIT License
 | 
			
		||||
 * Copyright (c) 2014-2016 Ilkka Seppälä
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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:
 | 
			
		||||
 * <p>
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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<UserDto, User> 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);
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,86 @@
 | 
			
		||||
/**
 | 
			
		||||
 * The MIT License
 | 
			
		||||
 * Copyright (c) 2014-2016 Ilkka Seppälä
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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:
 | 
			
		||||
 * <p>
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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 <T>
 | 
			
		||||
 * @param <U>
 | 
			
		||||
 */
 | 
			
		||||
public class Converter<T, U> {
 | 
			
		||||
  /**
 | 
			
		||||
   *
 | 
			
		||||
   */
 | 
			
		||||
  private final Function<T, U> fromDTO;
 | 
			
		||||
  /**
 | 
			
		||||
   *
 | 
			
		||||
   */
 | 
			
		||||
  private final Function<U, T> fromEntity;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @param fromDTO
 | 
			
		||||
   * @param fromEntity
 | 
			
		||||
   */
 | 
			
		||||
  public Converter(final Function<T, U> fromDTO, final Function<U, T> 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<U> createFromDTOs(final Collection<T> arg) {
 | 
			
		||||
    return arg.stream().map(this::convertFromDTO).collect(Collectors.toList());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @param arg
 | 
			
		||||
   * @return
 | 
			
		||||
   */
 | 
			
		||||
  public List<T> createFromEntities(final Collection<U> arg) {
 | 
			
		||||
    return arg.stream().map(this::convertFromEntity).collect(Collectors.toList());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										69
									
								
								converter/src/main/java/com/iluwatar/converter/User.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								converter/src/main/java/com/iluwatar/converter/User.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
/**
 | 
			
		||||
 * The MIT License
 | 
			
		||||
 * Copyright (c) 2014-2016 Ilkka Seppälä
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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:
 | 
			
		||||
 * <p>
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										69
									
								
								converter/src/main/java/com/iluwatar/converter/UserDto.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								converter/src/main/java/com/iluwatar/converter/UserDto.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
/**
 | 
			
		||||
 * The MIT License
 | 
			
		||||
 * Copyright (c) 2014-2016 Ilkka Seppälä
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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:
 | 
			
		||||
 * <p>
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 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;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								converter/src/test/java/com/iluwatar/converter/AppTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								converter/src/test/java/com/iluwatar/converter/AppTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user