BAEL 4092 - code samples for the article (#11235)

* BAEL-4092 initial import for code samples

* BAEL-4092 update code samples

* BAEL-4092 align code samples with the article

* BAEL-4092 some naming and formatting update

* BAEL-4092 fix tests

* BAEL-4092 remove url line in readme

* BAEL-4092 change formatting to fix indentations

* BAEL-4092 fix the typo in the package name

* BAEL-4092 fix pmd violations

* BAEL-4092 fix the test method namings according to convention

* BAEL-4092 add empty spaces on tests to make it more readable

* BAEL-4092 move the code samples from its own module into libraries6

* BAEL-4092 use the latest version of ModelMapper

Co-authored-by: Yavuz Tas <ytas@vwd.com>
This commit is contained in:
Yavuz Tas
2021-10-09 05:04:39 +02:00
committed by GitHub
parent b04e68e10b
commit 9532553421
9 changed files with 581 additions and 1 deletions
@@ -0,0 +1,74 @@
package com.baeldung.modelmapper.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Game {
private Long id;
private String name;
private Long timestamp;
private Player creator;
private final List<Player> players = new ArrayList<>();
private GameSettings settings;
public Game() {
}
public Game(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Long getTimestamp() {
return this.timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Player getCreator() {
return this.creator;
}
public void setCreator(Player creator) {
this.creator = creator;
addPlayer(creator);
}
public void addPlayer(Player player) {
this.players.add(player);
}
public List<Player> getPlayers() {
return Collections.unmodifiableList(this.players);
}
public GameSettings getSettings() {
return this.settings;
}
public void setSettings(GameSettings settings) {
this.settings = settings;
}
}
@@ -0,0 +1,5 @@
package com.baeldung.modelmapper.domain;
public enum GameMode {
NORMAL, TURBO
}
@@ -0,0 +1,31 @@
package com.baeldung.modelmapper.domain;
public class GameSettings {
private GameMode mode;
private int maxPlayers;
public GameSettings() {
}
public GameSettings(GameMode mode, int maxPlayers) {
this.mode = mode;
this.maxPlayers = maxPlayers;
}
public GameMode getMode() {
return this.mode;
}
public void setMode(GameMode mode) {
this.mode = mode;
}
public int getMaxPlayers() {
return this.maxPlayers;
}
public void setMaxPlayers(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
}
@@ -0,0 +1,41 @@
package com.baeldung.modelmapper.domain;
public class Player {
private Long id;
private String name;
private Game currentGame;
public Player() {
}
public Player(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Game getCurrentGame() {
return this.currentGame;
}
public void setCurrentGame(Game currentGame) {
this.currentGame = currentGame;
}
}
@@ -0,0 +1,92 @@
package com.baeldung.modelmapper.dto;
import com.baeldung.modelmapper.domain.GameMode;
import java.util.List;
public class GameDTO {
private Long id;
private String name;
private Long creationTime;
private String creatorId;
private String creator;
private int totalPlayers;
private List<PlayerDTO> players;
private GameMode mode;
private int maxPlayers;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Long getCreationTime() {
return this.creationTime;
}
public void setCreationTime(Long creationTime) {
this.creationTime = creationTime;
}
public String getCreatorId() {
return this.creatorId;
}
public void setCreatorId(String creatorId) {
this.creatorId = creatorId;
}
public String getCreator() {
return this.creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public int getTotalPlayers() {
return this.totalPlayers;
}
public void setTotalPlayers(int totalPlayers) {
this.totalPlayers = totalPlayers;
}
public GameMode getMode() {
return this.mode;
}
public void setMode(GameMode mode) {
this.mode = mode;
}
public int getMaxPlayers() {
return this.maxPlayers;
}
public void setMaxPlayers(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
public List<PlayerDTO> getPlayers() {
return this.players;
}
public void setPlayers(List<PlayerDTO> players) {
this.players = players;
}
}
@@ -0,0 +1,33 @@
package com.baeldung.modelmapper.dto;
public class PlayerDTO {
private Long id;
private String name;
private GameDTO currentGame;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public GameDTO getCurrentGame() {
return this.currentGame;
}
public void setCurrentGame(GameDTO currentGame) {
this.currentGame = currentGame;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.modelmapper.repository;
import com.baeldung.modelmapper.domain.Game;
import java.util.ArrayList;
import java.util.List;
/**
* Sample in-memory Game Repository
*/
public class GameRepository {
private final List<Game> gameStore = new ArrayList<>();
public GameRepository() {
// initialize some test data
this.gameStore.add(new Game(1L, "Game 1"));
this.gameStore.add(new Game(2L, "Game 2"));
this.gameStore.add(new Game(3L, "Game 3"));
}
public Game findById(Long id) {
return this.gameStore.stream()
.filter(g -> g.getId().equals(id))
.findFirst()
.orElseThrow(() -> new RuntimeException("No Game found"));
}
}