Compare commits
3 commits
7d7cf8148a
...
905fc4ba35
Author | SHA1 | Date | |
---|---|---|---|
Augusto Dwenger J. | 905fc4ba35 | ||
Augusto Dwenger J. | 7e0cad6ddb | ||
Augusto Dwenger J. | 01f62303fc |
7
pom.xml
7
pom.xml
|
@ -13,6 +13,7 @@
|
|||
<maven.compiler.source>18</maven.compiler.source>
|
||||
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
|
||||
<junit.jupiter.version>5.9.0</junit.jupiter.version>
|
||||
<mockito.version>4.8.0</mockito.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
@ -46,20 +47,20 @@
|
|||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>4.7.0</version>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>4.7.0</version>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- to test my json implementation -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.13.3</version>
|
||||
<version>2.13.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -43,13 +43,8 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
|
|||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
final String fileContent = Files.readString(file.toPath());
|
||||
final Player player = serializationFactory.createDeserializer(fileContent);
|
||||
return Optional.of(player);
|
||||
} catch (IOException e) {
|
||||
throw new ReadPlayerException("Player not found with the uuid: " + uuid, e);
|
||||
}
|
||||
final Player player = readPlayerFromFile(file);
|
||||
return Optional.of(player);
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
|
@ -59,14 +54,9 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
|
|||
public Optional<Player> findByName(final String name) throws ReadPlayerException {
|
||||
final File[] files = getFiles();
|
||||
for (final var file : files) {
|
||||
try {
|
||||
final String fileContent = Files.readString(file.toPath());
|
||||
final Player player = serializationFactory.createDeserializer(fileContent);
|
||||
if (player.name().equals(name)) {
|
||||
return Optional.of(player);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ReadPlayerException("Player not found with the name: " + name, e);
|
||||
final Player player = readPlayerFromFile(file);
|
||||
if (player.name().equals(name)) {
|
||||
return Optional.of(player);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,13 +68,8 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
|
|||
final File[] files = getFiles();
|
||||
final var playerList = new ArrayList<Player>();
|
||||
for (final var file : files) {
|
||||
try {
|
||||
final String fileContent = Files.readString(file.toPath());
|
||||
final Player player = serializationFactory.createDeserializer(fileContent);
|
||||
playerList.add(player);
|
||||
} catch (IOException e) {
|
||||
throw new ReadPlayerException("Player not found with the path: " + file.getAbsolutePath(), e);
|
||||
}
|
||||
final Player player = readPlayerFromFile(file);
|
||||
playerList.add(player);
|
||||
}
|
||||
|
||||
return playerList;
|
||||
|
@ -99,4 +84,14 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
|
|||
return files;
|
||||
}
|
||||
|
||||
private Player readPlayerFromFile(final File playerFile) throws ReadPlayerException {
|
||||
try {
|
||||
final String fileContent = Files.readString(playerFile.toPath());
|
||||
return serializationFactory.createDeserializer(fileContent);
|
||||
} catch (IOException e) {
|
||||
final String fileName = playerFile.getName();
|
||||
throw new ReadPlayerException("Could not read file: " + fileName, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue