Skip to content

Commit cf2c678

Browse files
committed
Added a simpler way to persist a not annotated object
1 parent 5eb18a5 commit cf2c678

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/main/java/io/github/filelize/FilelizerObject.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import org.slf4j.LoggerFactory;
88

99
import java.io.IOException;
10-
import java.nio.file.NoSuchFileException;
11-
import java.util.HashMap;
1210
import java.util.List;
1311
import java.util.Map;
12+
import java.util.stream.Collectors;
1413

1514
public class FilelizerObject implements IFilelizer {
1615
private final Logger log = LoggerFactory.getLogger(FilelizerObject.class);
@@ -43,15 +42,10 @@ public <T> T find(String id, Class<T> valueType) {
4342

4443
@Override
4544
public <T> Map<String, T> findAll(Class<T> valueType) {
46-
var fullPath = pathHandler.getFullPath(valueType);
47-
try {
48-
return fileHandler.readFileMap(fullPath, valueType);
49-
} catch (NoSuchFileException e) {
50-
return new HashMap<>();
51-
} catch (IOException e) {
52-
log.error("Error occurred when trying to get " + fullPath, e);
53-
return new HashMap<>();
54-
}
45+
List<T> t = find(valueType.getSimpleName() + "_all", List.class);
46+
Map<String, T> resultMap = t.stream()
47+
.collect(Collectors.toMap(Object::toString, item -> item));
48+
return resultMap;
5549
}
5650

5751
@Override
@@ -62,7 +56,7 @@ public <T> String save(T object) {
6256
@Override
6357
public <T> String save(String id, T object) {
6458
try {
65-
var fullPath = pathHandler.getFullPath(id, object);
59+
var fullPath = pathHandler.getFullPath2(id, object);
6660
fileHandler.writeFile(fullPath, object);
6761
return id;
6862
} catch (IOException e) {

src/main/java/io/github/filelize/path/PathHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ public String getFullPath(Object object) {
2727
return getDirectoryPath(object) + "/" + filename;
2828
}
2929

30-
public String getFullPath(String id, Object object) {
30+
public String getFullPath2(String id, Object object) {
3131
return getDirectoryPath(object) + "/" + id + ".json";
3232
}
3333

3434
public <T> String getFullPath(String id, Class<T> valueType) {
3535
if(filelizeType == FilelizeType.MULTIPLE_FILES) {
3636
var name = getFilelizeName(valueType);
3737
return getDirectoryPath(valueType) + "/" + getFilename(id, name);
38-
} else {
38+
} else if(filelizeType == FilelizeType.SINGLE_FILE) {
3939
return getFullPath(valueType);
4040
}
41+
return getFullPath2(id, valueType);
4142
}
4243

4344
public <T> String getFullPathOfMap(Map<String, T> objects) {

0 commit comments

Comments
 (0)