diff --git a/src/main/java/de/oliver/fancylib/jdb/JDB.java b/src/main/java/de/oliver/fancylib/jdb/JDB.java index f2e8869..20aa8bb 100644 --- a/src/main/java/de/oliver/fancylib/jdb/JDB.java +++ b/src/main/java/de/oliver/fancylib/jdb/JDB.java @@ -16,14 +16,14 @@ * The JDB class provides a simple JSON document-based storage system in a specified directory. */ public class JDB { - private final static Gson GSON = new GsonBuilder() .serializeNulls() .setPrettyPrinting() .create(); + private static final String FILE_EXTENSION = ".json"; private final @NotNull String basePath; - private final @NotNull File baseFolder; + private final @NotNull File baseDirectory; /** * Constructs a new JDB instance with the specified base path. @@ -32,7 +32,7 @@ public class JDB { */ public JDB(@NotNull String basePath) { this.basePath = basePath; - this.baseFolder = new File(basePath); + this.baseDirectory = new File(basePath); } /** @@ -45,13 +45,11 @@ public JDB(@NotNull String basePath) { * @throws IOException if an I/O error occurs during file reading */ public T get(@NotNull String path, @NotNull Class clazz) throws IOException { - File file = new File(baseFolder, basePath + path + ".json"); - if (!file.exists()) { + File documentFile = new File(baseDirectory, createFilePath(path)); + if (!documentFile.exists()) { return null; } - - BufferedReader bufferedReader = Files.newBufferedReader(file.toPath()); - + BufferedReader bufferedReader = Files.newBufferedReader(documentFile.toPath()); return GSON.fromJson(bufferedReader, clazz); } @@ -63,14 +61,12 @@ public T get(@NotNull String path, @NotNull Class clazz) throws IOExcepti * @throws IOException if an I/O error occurs during file reading */ public JDocument getDocument(@NotNull String path) throws IOException { - File file = new File(baseFolder, basePath + path + ".json"); - if (!file.exists()) { + File documentFile = new File(baseDirectory, createFilePath(path)); + if (!documentFile.exists()) { return null; } - - BufferedReader bufferedReader = Files.newBufferedReader(file.toPath()); - - Map data = (Map) GSON.fromJson(bufferedReader, JDocument.class); + BufferedReader bufferedReader = Files.newBufferedReader(documentFile.toPath()); + Map data = (Map) GSON.fromJson(bufferedReader, Map.class); return new JDocument(data); } @@ -84,21 +80,18 @@ public JDocument getDocument(@NotNull String path) throws IOException { * @throws IOException if an I/O error occurs during file reading */ public List getAll(@NotNull String path, @NotNull Class clazz) throws IOException { - File folder = new File(baseFolder, basePath + path); - if (!folder.exists()) { + File directory = new File(baseDirectory, basePath + path); + if (!directory.exists()) { return new ArrayList<>(); } - - File[] files = folder.listFiles(); + File[] files = directory.listFiles(); if (files == null) { return new ArrayList<>(); } - List documents = new ArrayList<>(files.length); for (File file : files) { - documents.add(get(path + "/" + file.getName().replace(".json", ""), clazz)); + documents.add(get(path + "/" + file.getName().replace(FILE_EXTENSION, ""), clazz)); } - return documents; } @@ -111,16 +104,13 @@ public List getAll(@NotNull String path, @NotNull Class clazz) throws * @throws IOException if an I/O error occurs during file writing */ public void set(@NotNull String path, @NotNull T value) throws IOException { - File file = new File(baseFolder, basePath + path + ".json"); - - if (!file.exists()) { - file.getParentFile().mkdirs(); - file.createNewFile(); + File documentFile = new File(baseDirectory, createFilePath(path)); + if (!documentFile.exists()) { + documentFile.getParentFile().mkdirs(); + documentFile.createNewFile(); } - String json = GSON.toJson(value); - - Files.write(file.toPath(), json.getBytes()); + Files.write(documentFile.toPath(), json.getBytes()); } /** @@ -129,9 +119,19 @@ public void set(@NotNull String path, @NotNull T value) throws IOException { * @param path the relative path (excluding .json extension) where the document(s) are located */ public void delete(@NotNull String path) { - File file = new File(baseFolder, basePath + path + ".json"); - if (file.exists()) { - file.delete(); + File documentFile = new File(baseDirectory, createFilePath(path)); + if (documentFile.exists()) { + documentFile.delete(); } } + + /** + * Creates the file path by appending the base path, provided path, and the file extension. + * + * @param path the relative path (excluding .json extension) + * @return the full file path + */ + private String createFilePath(@NotNull String path) { + return basePath + path + FILE_EXTENSION; + } } diff --git a/src/main/java/de/oliver/fancylib/jdb/JDocument.java b/src/main/java/de/oliver/fancylib/jdb/JDocument.java index 62ce4a5..d23082b 100644 --- a/src/main/java/de/oliver/fancylib/jdb/JDocument.java +++ b/src/main/java/de/oliver/fancylib/jdb/JDocument.java @@ -10,7 +10,6 @@ * Represents a document that holds a map of key-value pairs with support for nested keys. */ public class JDocument { - private final @NotNull Map data; public JDocument(@NotNull Map data) { @@ -24,27 +23,7 @@ public JDocument(@NotNull Map data) { * @return the value associated with the given key, or null if the key is not found */ public Object get(String key) { - String[] parts = key.split("\\."); - - Map current = data; - for (int i = 0; i < parts.length; i++) { - Object obj = current.get(parts[i]); - if (obj == null) { - return null; - } - - if (i == parts.length - 1) { - return obj; - } - - if (!(obj instanceof Map)) { - return null; - } - - current = (Map) obj; - } - - return null; + return getValue(key, Object.class); } /** @@ -65,18 +44,8 @@ public boolean contains(String key) { * is not found or the value is not a map */ public Set getKeys(String key) { - Object obj = get(key); - if (obj == null) { - return new HashSet<>(); - } - - if (!isType(obj, Map.class)) { - return new HashSet<>(); - } - - Map map = (Map) data.get(key); - - return map.keySet(); + Map map = (Map) getValue(key, Map.class); + return map != null ? map.keySet() : new HashSet<>(); } /** @@ -86,12 +55,7 @@ public Set getKeys(String key) { * @return the string value associated with the given key, or an empty string if the key is not found or the value is not a string */ public String getString(String key) { - Object obj = get(key); - if (obj == null) { - return ""; - } - - return (String) obj; + return (String) getValueOrDefault(key, String.class, ""); } /** @@ -101,16 +65,7 @@ public String getString(String key) { * @return the boolean value associated with the given key, or false if the key is not found or the value is not a boolean */ public boolean getBoolean(String key) { - Object obj = get(key); - if (obj == null) { - return false; - } - - if (!isType(obj, Boolean.class)) { - return false; - } - - return (boolean) obj; + return (boolean) getValueOrDefault(key, Boolean.class, false); } /** @@ -120,16 +75,7 @@ public boolean getBoolean(String key) { * @return the byte value associated with the given key, or 0 if the key is not found or the value is not a byte */ public byte getByte(String key) { - Object obj = get(key); - if (obj == null) { - return 0; - } - - if (!isType(obj, Byte.class)) { - return 0; - } - - return (byte) obj; + return (byte) getValueOrDefault(key, Byte.class, (byte) 0); } /** @@ -139,16 +85,7 @@ public byte getByte(String key) { * @return the short value associated with the given key, or 0 if the key is not found or the value is not a short */ public short getShort(String key) { - Object obj = get(key); - if (obj == null) { - return 0; - } - - if (!isType(obj, Short.class)) { - return 0; - } - - return (short) obj; + return (short) getValueOrDefault(key, Short.class, (short) 0); } /** @@ -158,16 +95,7 @@ public short getShort(String key) { * @return the integer value associated with the given key, or 0 if the key is not found or the value is not an integer */ public int getInt(String key) { - Object obj = get(key); - if (obj == null) { - return 0; - } - - if (!isType(obj, Integer.class)) { - return 0; - } - - return (int) obj; + return (int) getValueOrDefault(key, Integer.class, 0); } /** @@ -177,36 +105,17 @@ public int getInt(String key) { * @return the long value associated with the given key, or 0 if the key is not found or the value is not a long */ public long getLong(String key) { - Object obj = get(key); - if (obj == null) { - return 0; - } - - if (!isType(obj, Long.class)) { - return 0; - } - - return (long) obj; + return (long) getValueOrDefault(key, Long.class, 0L); } /** * Retrieves a float value associated with the given key. * * @param key the dot-separated key used to locate the value in the document (e.g. "foo.bar.baz") - * @return the float value associated with the given key, or 0 if the key is not found - * or the value is not a float + * @return the float value associated with the given key, or 0 if the key is not found or the value is not a float */ public float getFloat(String key) { - Object obj = get(key); - if (obj == null) { - return 0; - } - - if (!isType(obj, Float.class)) { - return 0; - } - - return (float) obj; + return (float) getValueOrDefault(key, Float.class, 0f); } /** @@ -216,19 +125,28 @@ public float getFloat(String key) { * @return the double value associated with the given key, or 0 if the key is not found or the value is not a double */ public double getDouble(String key) { - Object obj = get(key); - if (obj == null) { - return 0; - } + return (double) getValueOrDefault(key, Double.class, 0d); + } - if (!isType(obj, Double.class)) { - return 0; - } + private Object getValue(String key, Class clazz) { + String[] parts = key.split("\\."); + Map current = data; - return (double) obj; + for (int i = 0; i < parts.length; i++) { + Object value = current.get(parts[i]); + if (value == null || (i < parts.length - 1 && !(value instanceof Map))) { + return null; + } + if (i == parts.length - 1) { + return clazz.isInstance(value) ? value : null; + } + current = (Map) value; + } + return null; } - private boolean isType(@NotNull Object obj, Class clazz) { - return clazz.isInstance(obj); + private T getValueOrDefault(String key, Class clazz, T defaultValue) { + T value = (T) getValue(key, clazz); + return value != null ? value : defaultValue; } } \ No newline at end of file diff --git a/src/test/java/de/oliver/fancylib/jdb/JDBTest.java b/src/test/java/de/oliver/fancylib/jdb/JDBTest.java index 76349f1..32a0034 100644 --- a/src/test/java/de/oliver/fancylib/jdb/JDBTest.java +++ b/src/test/java/de/oliver/fancylib/jdb/JDBTest.java @@ -201,7 +201,7 @@ public void testGetDocumentWhenFileExists() throws IOException { String basePath = "./test_files/"; JDB jdb = new JDB(basePath); String path = "existing_file"; - String value = "Test message"; + TestObject value = new TestObject("Test message"); jdb.set(path, value); // Act @@ -209,7 +209,7 @@ public void testGetDocumentWhenFileExists() throws IOException { // Assert assertNotNull(document); - assertEquals(value, document.getString("message")); + assertEquals(value.message(), document.getString("message")); } // Testing the getDocument method when the file does not exist @@ -226,4 +226,7 @@ public void testGetDocumentWhenFileDoesNotExist() throws IOException { // Assert assertNull(document); } + + record TestObject(String message) { + } }