From d68db5ab6068d0ebd7455bc105da6089f4bf0eca Mon Sep 17 00:00:00 2001 From: "George V." Date: Tue, 25 Jun 2024 09:15:01 +0300 Subject: [PATCH] feat(database): Change getLoadedEntities to ObservableObjectMap (and correct some javadocs) --- .../library/utilities/EntityRepository.java | 54 ++++++++++++++++--- .../utilities/JsonEntityRepository.java | 15 +++--- .../utilities/MongoDBEntityRepository.java | 14 ++--- .../utilities/MySQLEntityRepository.java | 13 ++--- .../utilities/YamlEntityRepository.java | 15 +++--- .../library/maps/ObservableObjectMap.java | 2 +- 6 files changed, 76 insertions(+), 37 deletions(-) diff --git a/database/src/main/java/com/georgev22/library/utilities/EntityRepository.java b/database/src/main/java/com/georgev22/library/utilities/EntityRepository.java index 23fcc03..2ef25fd 100644 --- a/database/src/main/java/com/georgev22/library/utilities/EntityRepository.java +++ b/database/src/main/java/com/georgev22/library/utilities/EntityRepository.java @@ -2,6 +2,7 @@ import com.georgev22.library.maps.HashObjectMap; import com.georgev22.library.maps.ObjectMap; +import com.georgev22.library.maps.ObservableObjectMap; import com.georgev22.library.utilities.annotations.Column; import com.georgev22.library.utilities.exceptions.NoSuchConstructorException; import org.jetbrains.annotations.NotNull; @@ -23,47 +24,56 @@ public interface EntityRepository { /** * Saves the given entity. - * Returns the saved entity or null if an error occurs. + * Returns a CompletableFuture containing the saved entity, or completing with null if an error occurs. * * @param entity The entity to be saved. + * @return a CompletableFuture containing the saved entity, or completing with null if an error occurs */ CompletableFuture save(V entity); /** * Loads an entity based on the specified entity ID. - * Returns the loaded entity or null if the entity does not exist or if an error occurs. + * Returns a CompletableFuture containing the loaded entity, or completing with null if the entity does not exist or if an error occurs. * * @param entityId The ID of the entity to be loaded. + * @return a CompletableFuture containing the loaded entity, or completing with null if the entity does not exist or if an error occurs */ CompletableFuture load(@NotNull String entityId); /** * Retrieves the loaded entity with the specified ID. + * Returns a CompletableFuture containing the loaded entity, or completing with null if not found (implementation-dependent). * * @param entityId The ID of the entity to be retrieved. - * @return The loaded entity, or null if not found (implementation-dependent). + * @return a CompletableFuture containing the loaded entity, or completing with null if not found */ CompletableFuture getEntity(@NotNull String entityId); /** * Checks if an entity with the specified ID exists. + * Returns a CompletableFuture containing true if the entity exists, false otherwise. * - * @param entityId The ID of the entity to check for existence. - * @return True if the entity exists, false otherwise. + * @param entityId The ID of the entity to check for existence. + * @param checkDb Whether to check the database for the entity's existence. + * @param forceLoad Whether to force loading the entity from the database. + * @return a CompletableFuture containing true if the entity exists, false otherwise */ CompletableFuture exists(@NotNull String entityId, boolean checkDb, boolean forceLoad); /** * Deletes the entity with the specified ID. + * Returns a CompletableFuture that completes when the deletion is done. * * @param entityId The ID of the entity to be deleted. + * @return a CompletableFuture that completes when the deletion is done */ CompletableFuture delete(@NotNull String entityId); /** * Loads all entities from the database. + * Returns a CompletableFuture containing the number of loaded entities. * - * @return The number of loaded entities. + * @return a CompletableFuture containing the number of loaded entities */ CompletableFuture loadAll(); @@ -91,6 +101,13 @@ default void checkForConstructorWithSingleString(@NotNull Class entityClass) throw new NoSuchConstructorException("No constructor with a single vararg String parameter found in class " + entityClass.getSimpleName()); } + /** + * Retrieves a list of methods in the specified class that are annotated with {@link Column}, + * have no parameters, return a non-void type, do not start with "set", and are not named "_id". + * + * @param entityClass The class to retrieve the methods from. + * @return a list of methods that match the criteria + */ default List getMethods(@NotNull Class entityClass) { return Arrays.stream(entityClass.getDeclaredMethods()) .filter( @@ -108,6 +125,13 @@ default List getMethods(@NotNull Class entityClass) { .toList(); } + /** + * Retrieves a list of fields in the specified class that are annotated with {@link Column} + * and are not named "_id". + * + * @param entityClass The class to retrieve the fields from. + * @return a list of fields that match the criteria + */ default List getFields(@NotNull Class entityClass) { return Arrays.stream(entityClass.getDeclaredFields()) .filter( @@ -121,6 +145,12 @@ default List getFields(@NotNull Class entityClass) { }).toList(); } + /** + * Creates an ObjectMap containing the values of the specified entity's annotated fields and methods. + * + * @param entity The entity to extract values from. + * @return an ObjectMap containing the values of the entity's annotated fields and methods + */ default ObjectMap getValuesMap(@NotNull V entity) { ObjectMap values = new HashObjectMap<>(); @@ -157,7 +187,17 @@ default ObjectMap getValuesMap(@NotNull V entity) { return values; } + /** + * Gets the logger associated with this repository. + * + * @return The logger. + */ Logger getLogger(); - List getLoadedEntities(); + /** + * Returns an observable map of all loaded entities. + * + * @return an ObservableObjectMap containing all loaded entities + */ + ObservableObjectMap getLoadedEntities(); } diff --git a/database/src/main/java/com/georgev22/library/utilities/JsonEntityRepository.java b/database/src/main/java/com/georgev22/library/utilities/JsonEntityRepository.java index e83bd16..fbf6f76 100644 --- a/database/src/main/java/com/georgev22/library/utilities/JsonEntityRepository.java +++ b/database/src/main/java/com/georgev22/library/utilities/JsonEntityRepository.java @@ -1,10 +1,8 @@ package com.georgev22.library.utilities; -import com.georgev22.library.maps.HashObjectMap; -import com.georgev22.library.maps.ObjectMap; +import com.georgev22.library.maps.ObservableObjectMap; import com.google.gson.Gson; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.UnmodifiableView; import java.io.*; import java.math.BigInteger; @@ -24,7 +22,7 @@ */ public class JsonEntityRepository implements EntityRepository { - private final ObjectMap loadedEntities = new HashObjectMap<>(); + private final ObservableObjectMap loadedEntities = new ObservableObjectMap<>(); private final File dataFolder; private final Logger logger; private final Gson gson; @@ -211,11 +209,12 @@ public Logger getLogger() { } /** - * Returns a list of all loaded entities + * Returns an observable map of all loaded entities. * - * @return The list of loaded entities + * @return an ObservableObjectMap containing all loaded entities */ - @UnmodifiableView @Override public List getLoadedEntities() { - return this.loadedEntities.values().stream().toList(); + @Override + public ObservableObjectMap getLoadedEntities() { + return this.loadedEntities; } } diff --git a/database/src/main/java/com/georgev22/library/utilities/MongoDBEntityRepository.java b/database/src/main/java/com/georgev22/library/utilities/MongoDBEntityRepository.java index a6bf390..1b12a16 100644 --- a/database/src/main/java/com/georgev22/library/utilities/MongoDBEntityRepository.java +++ b/database/src/main/java/com/georgev22/library/utilities/MongoDBEntityRepository.java @@ -1,7 +1,6 @@ package com.georgev22.library.utilities; -import com.georgev22.library.maps.HashObjectMap; -import com.georgev22.library.maps.ObjectMap; +import com.georgev22.library.maps.ObservableObjectMap; import com.georgev22.library.utilities.exceptions.NoSuchConstructorException; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; @@ -27,7 +26,7 @@ */ public class MongoDBEntityRepository implements EntityRepository { - private final ObjectMap loadedEntities = new HashObjectMap<>(); + private final ObservableObjectMap loadedEntities = new ObservableObjectMap<>(); private final MongoDatabase mongoDatabase; private final Logger logger; private final Class entityClass; @@ -230,11 +229,12 @@ public Logger getLogger() { } /** - * Returns a list of all loaded entities + * Returns an observable map of all loaded entities. * - * @return The list of loaded entities + * @return an ObservableObjectMap containing all loaded entities */ - @UnmodifiableView @Override public List getLoadedEntities() { - return this.loadedEntities.values().stream().toList(); + @Override + public ObservableObjectMap getLoadedEntities() { + return this.loadedEntities; } } diff --git a/database/src/main/java/com/georgev22/library/utilities/MySQLEntityRepository.java b/database/src/main/java/com/georgev22/library/utilities/MySQLEntityRepository.java index fdaa589..2d302f9 100644 --- a/database/src/main/java/com/georgev22/library/utilities/MySQLEntityRepository.java +++ b/database/src/main/java/com/georgev22/library/utilities/MySQLEntityRepository.java @@ -3,10 +3,10 @@ import com.georgev22.library.database.sql.Database; import com.georgev22.library.maps.HashObjectMap; import com.georgev22.library.maps.ObjectMap; +import com.georgev22.library.maps.ObservableObjectMap; import com.georgev22.library.utilities.exceptions.NoSuchConstructorException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.UnmodifiableView; import java.lang.reflect.InvocationTargetException; import java.math.BigInteger; @@ -28,7 +28,7 @@ */ public class MySQLEntityRepository implements EntityRepository { - private final ObjectMap loadedEntities = new HashObjectMap<>(); + private final ObservableObjectMap loadedEntities = new ObservableObjectMap<>(); private final Database database; private final Logger logger; private final Class entityClass; @@ -307,12 +307,13 @@ public Logger getLogger() { } /** - * Returns a list of all loaded entities + * Returns an observable map of all loaded entities. * - * @return The list of loaded entities + * @return an ObservableObjectMap containing all loaded entities */ - @UnmodifiableView @Override public List getLoadedEntities() { - return this.loadedEntities.values().stream().toList(); + @Override + public ObservableObjectMap getLoadedEntities() { + return this.loadedEntities; } private String escapeSql(String input) { diff --git a/database/src/main/java/com/georgev22/library/utilities/YamlEntityRepository.java b/database/src/main/java/com/georgev22/library/utilities/YamlEntityRepository.java index 3c29b4e..9139961 100644 --- a/database/src/main/java/com/georgev22/library/utilities/YamlEntityRepository.java +++ b/database/src/main/java/com/georgev22/library/utilities/YamlEntityRepository.java @@ -1,11 +1,9 @@ package com.georgev22.library.utilities; -import com.georgev22.library.maps.HashObjectMap; -import com.georgev22.library.maps.ObjectMap; +import com.georgev22.library.maps.ObservableObjectMap; import com.georgev22.library.utilities.exceptions.NoSuchConstructorException; import com.georgev22.library.yaml.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.UnmodifiableView; import java.io.File; import java.io.IOException; @@ -26,7 +24,7 @@ */ public class YamlEntityRepository implements EntityRepository { - private final ObjectMap loadedEntities = new HashObjectMap<>(); + private final ObservableObjectMap loadedEntities = new ObservableObjectMap<>(); private final File dataFolder; private final Logger logger; private final Class entityClass; @@ -224,11 +222,12 @@ public Logger getLogger() { } /** - * Returns a list of all loaded entities + * Returns an observable map of all loaded entities. * - * @return The list of loaded entities + * @return an ObservableObjectMap containing all loaded entities */ - @UnmodifiableView @Override public List getLoadedEntities() { - return this.loadedEntities.values().stream().toList(); + @Override + public ObservableObjectMap getLoadedEntities() { + return this.loadedEntities; } } diff --git a/maps/src/main/java/com/georgev22/library/maps/ObservableObjectMap.java b/maps/src/main/java/com/georgev22/library/maps/ObservableObjectMap.java index fd9e39b..1499405 100644 --- a/maps/src/main/java/com/georgev22/library/maps/ObservableObjectMap.java +++ b/maps/src/main/java/com/georgev22/library/maps/ObservableObjectMap.java @@ -15,7 +15,7 @@ */ public class ObservableObjectMap extends ConcurrentObjectMap { - private List> listeners = new ArrayList<>(); + private final List> listeners = new ArrayList<>(); /** * Adds a {@link MapChangeListener} to this map.