From 7e591430e73c0af09e03946bc0cfaa83f2e29d17 Mon Sep 17 00:00:00 2001 From: srnyx <25808801+srnyx@users.noreply.github.com> Date: Fri, 29 Sep 2023 15:54:40 -0400 Subject: [PATCH] Added Javadocs to `LazyCollection` --- .../xyz/srnyx/lazylibrary/LazyCollection.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java b/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java index e71d036..c437668 100644 --- a/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java +++ b/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java @@ -15,55 +15,135 @@ import java.util.List; +/** + * A wrapper for {@link MongoCollection} with some useful shortcuts/methods + * + * @param the type of the collection + */ public class LazyCollection { + /** + * The {@link MongoCollection} instance + */ @NotNull public final MongoCollection collection; + /** + * Constructs a new {@link LazyCollection} instance + * + * @param database the {@link MongoDatabase} instance + * @param name the name of the collection + * @param clazz the class of the collection + */ public LazyCollection(@NotNull MongoDatabase database, @NotNull String name, @NotNull Class clazz) { collection = database.getCollection(name, clazz); } + /** + * Finds one document in the collection + * + * @param filter the filter to apply + * + * @return the document found, or null if none was found + */ @Nullable public T findOne(@NotNull Bson filter) { return collection.find(filter).first(); } + /** + * Finds one document in the collection + * + * @param field the field to filter by + * @param value the value of the field to filter by + * + * @return the document found, or null if none was found + */ @Nullable public T findOne(@NotNull String field, @Nullable Object value) { return collection.find(Filters.eq(field, value)).first(); } + /** + * Finds multiple documents in the collection + * + * @param filter the filter to apply + * + * @return the documents found + */ @NotNull public List findMany(@NotNull Bson filter) { return collection.find(filter).into(new ArrayList<>()); } + /** + * Updates a document in the collection + * + * @param filter the filter to apply + * @param update the update to apply + */ public void updateOne(@NotNull Bson filter, @NotNull Bson update) { collection.updateOne(filter, update); } + /** + * Updates a document in the collection and returns the updated document + * + * @param filter the filter to apply + * @param update the update to apply + * + * @return the updated document + */ @Nullable public T findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) { return collection.findOneAndUpdate(filter, update, getReturnAfter()); } + /** + * Updates a document in the collection (or inserts it if it doesn't exist) and returns the updated document + * + * @param filter the filter to apply + * @param update the update to apply + * + * @return the updated or inserted document + */ @Nullable public T findOneAndUpsert(@NotNull Bson filter, @NotNull Bson update) { return collection.findOneAndUpdate(filter, update, getUpsert()); } + /** + * Deletes a document in the collection + * + * @param filter the filter to apply + */ public void deleteOne(@NotNull Bson filter) { collection.deleteOne(filter); } + /** + * Deletes a document in the collection + * + * @param field the field to filter by + * @param value the value of the field to filter by + */ public void deleteOne(@NotNull String field, @Nullable Object value) { deleteOne(Filters.eq(field, value)); } + /** + * Returns a new {@link FindOneAndUpdateOptions} instance with the {@link ReturnDocument#AFTER} option set + * + * @return the {@link FindOneAndUpdateOptions} instance + */ @NotNull private static FindOneAndUpdateOptions getReturnAfter() { return new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER); } + /** + * Returns a new {@link FindOneAndUpdateOptions} instance with the {@link ReturnDocument#AFTER} and {@link FindOneAndUpdateOptions#upsert(boolean)} options set + * + * @return the {@link FindOneAndUpdateOptions} instance + */ @NotNull private static FindOneAndUpdateOptions getUpsert() { return getReturnAfter().upsert(true);