Class JDB

java.lang.Object
de.oliver.fancylib.jdb.JDB

public class JDB extends Object
The JDB class provides a simple JSON document-based storage system in a specified directory.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JDB(@NotNull String basePath)
    Constructs a new JDB instance with the specified base path.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    delete(@NotNull String path)
    Deletes the document(s) at the specified path.
    <T> T
    get(@NotNull String path, @NotNull Class<T> clazz)
    Retrieves a document from the specified path, deserializing it into the given class type.
    <T> List<T>
    getAll(@NotNull String path, @NotNull Class<T> clazz)
    Retrieves all documents from the specified directory path, deserializing them into the given class type.
    getDocument(@NotNull String path)
    Retrieves a document from the specified path.
    <T> void
    set(@NotNull String path, T value)
    Saves the given value as a document at the specified path.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JDB

      public JDB(@NotNull @NotNull String basePath)
      Constructs a new JDB instance with the specified base path.
      Parameters:
      basePath - the base directory path where documents will be stored
  • Method Details

    • get

      public <T> T get(@NotNull @NotNull String path, @NotNull @NotNull Class<T> clazz) throws IOException
      Retrieves a document from the specified path, deserializing it into the given class type.
      Type Parameters:
      T - the type of the object to be returned
      Parameters:
      path - the relative path (excluding .json extension) where the document is located
      clazz - the class type to which the document should be deserialized
      Returns:
      a JDocument containing the deserialized object and its path, or null if the file does not exist
      Throws:
      IOException - if an I/O error occurs during file reading
    • getDocument

      public JDocument getDocument(@NotNull @NotNull String path) throws IOException
      Retrieves a document from the specified path.
      Parameters:
      path - the relative path (excluding .json extension) where the document is located
      Returns:
      a JDocument containing the deserialized data or null if the file does not exist
      Throws:
      IOException - if an I/O error occurs during file reading
    • getAll

      public <T> List<T> getAll(@NotNull @NotNull String path, @NotNull @NotNull Class<T> clazz) throws IOException
      Retrieves all documents from the specified directory path, deserializing them into the given class type.
      Type Parameters:
      T - the type of objects to be returned
      Parameters:
      path - the relative directory path containing the documents
      clazz - the class type to which the documents should be deserialized
      Returns:
      a List of JDocument objects containing the deserialized objects and their paths, or null if the directory or files do not exist
      Throws:
      IOException - if an I/O error occurs during file reading
    • set

      public <T> void set(@NotNull @NotNull String path, @NotNull T value) throws IOException
      Saves the given value as a document at the specified path.
      Type Parameters:
      T - the type of the object to be saved
      Parameters:
      path - the relative path (excluding .json extension) where the document will be saved
      value - the object to be saved as a JSON document
      Throws:
      IOException - if an I/O error occurs during file writing
    • delete

      public void delete(@NotNull @NotNull String path)
      Deletes the document(s) at the specified path.
      Parameters:
      path - the relative path (excluding .json extension) where the document(s) are located