Skip to content

Commit

Permalink
feat(database): Change getLoadedEntities to ObservableObjectMap (and …
Browse files Browse the repository at this point in the history
…correct some javadocs)
  • Loading branch information
GeorgeV220 committed Jun 25, 2024
1 parent c74e2a3 commit d68db5a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,47 +24,56 @@ public interface EntityRepository<V extends Entity> {

/**
* 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<V> 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<V> 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<V> 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<Boolean> 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<Void> 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<BigInteger> loadAll();

Expand Down Expand Up @@ -91,6 +101,13 @@ default void checkForConstructorWithSingleString(@NotNull Class<V> 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<Method> getMethods(@NotNull Class<?> entityClass) {
return Arrays.stream(entityClass.getDeclaredMethods())
.filter(
Expand All @@ -108,6 +125,13 @@ default List<Method> 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<Field> getFields(@NotNull Class<?> entityClass) {
return Arrays.stream(entityClass.getDeclaredFields())
.filter(
Expand All @@ -121,6 +145,12 @@ default List<Field> 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<String, Object> getValuesMap(@NotNull V entity) {
ObjectMap<String, Object> values = new HashObjectMap<>();

Expand Down Expand Up @@ -157,7 +187,17 @@ default ObjectMap<String, Object> getValuesMap(@NotNull V entity) {
return values;
}

/**
* Gets the logger associated with this repository.
*
* @return The logger.
*/
Logger getLogger();

List<V> getLoadedEntities();
/**
* Returns an observable map of all loaded entities.
*
* @return an ObservableObjectMap containing all loaded entities
*/
ObservableObjectMap<String, V> getLoadedEntities();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,7 +22,7 @@
*/
public class JsonEntityRepository<V extends Entity> implements EntityRepository<V> {

private final ObjectMap<String, V> loadedEntities = new HashObjectMap<>();
private final ObservableObjectMap<String, V> loadedEntities = new ObservableObjectMap<>();
private final File dataFolder;
private final Logger logger;
private final Gson gson;
Expand Down Expand Up @@ -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<V> getLoadedEntities() {
return this.loadedEntities.values().stream().toList();
@Override
public ObservableObjectMap<String, V> getLoadedEntities() {
return this.loadedEntities;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,7 +26,7 @@
*/
public class MongoDBEntityRepository<V extends Entity> implements EntityRepository<V> {

private final ObjectMap<String, V> loadedEntities = new HashObjectMap<>();
private final ObservableObjectMap<String, V> loadedEntities = new ObservableObjectMap<>();
private final MongoDatabase mongoDatabase;
private final Logger logger;
private final Class<V> entityClass;
Expand Down Expand Up @@ -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<V> getLoadedEntities() {
return this.loadedEntities.values().stream().toList();
@Override
public ObservableObjectMap<String, V> getLoadedEntities() {
return this.loadedEntities;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +28,7 @@
*/
public class MySQLEntityRepository<V extends Entity> implements EntityRepository<V> {

private final ObjectMap<String, V> loadedEntities = new HashObjectMap<>();
private final ObservableObjectMap<String, V> loadedEntities = new ObservableObjectMap<>();
private final Database database;
private final Logger logger;
private final Class<V> entityClass;
Expand Down Expand Up @@ -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<V> getLoadedEntities() {
return this.loadedEntities.values().stream().toList();
@Override
public ObservableObjectMap<String, V> getLoadedEntities() {
return this.loadedEntities;
}

private String escapeSql(String input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +24,7 @@
*/
public class YamlEntityRepository<V extends Entity> implements EntityRepository<V> {

private final ObjectMap<String, V> loadedEntities = new HashObjectMap<>();
private final ObservableObjectMap<String, V> loadedEntities = new ObservableObjectMap<>();
private final File dataFolder;
private final Logger logger;
private final Class<V> entityClass;
Expand Down Expand Up @@ -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<V> getLoadedEntities() {
return this.loadedEntities.values().stream().toList();
@Override
public ObservableObjectMap<String, V> getLoadedEntities() {
return this.loadedEntities;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class ObservableObjectMap<K, V> extends ConcurrentObjectMap<K, V> {

private List<MapChangeListener<K, V>> listeners = new ArrayList<>();
private final List<MapChangeListener<K, V>> listeners = new ArrayList<>();

/**
* Adds a {@link MapChangeListener} to this map.
Expand Down

0 comments on commit d68db5a

Please sign in to comment.