Skip to content

Commit

Permalink
Added Javadocs to LazyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Sep 29, 2023
1 parent e149337 commit 7e59143
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,135 @@
import java.util.List;


/**
* A wrapper for {@link MongoCollection} with some useful shortcuts/methods
*
* @param <T> the type of the collection
*/
public class LazyCollection<T> {
/**
* The {@link MongoCollection} instance
*/
@NotNull public final MongoCollection<T> 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<T> 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<T> 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);
Expand Down

0 comments on commit 7e59143

Please sign in to comment.