Skip to content

Commit

Permalink
Add UpdateBuilder Javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Apr 1, 2024
1 parent 450091d commit 4457846
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/xyz/srnyx/magicmongo/UpdateBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,76 @@
import java.util.List;


/**
* A simple builder for {@link Updates MongoDB updates}
*/
public class UpdateBuilder {
/**
* The {@link Bson update} to build
*/
@Nullable private Bson update;

/**
* Creates a new {@link UpdateBuilder} instance with the given {@link #update}
*
* @param update the {@link Bson update} to start with
*/
public UpdateBuilder(@NotNull Bson update) {
this.update = update;
}

/**
* Creates a new {@link UpdateBuilder} instance with the given {@link #update updates}
*
* @param updates the {@link Bson updates} to start with
*/
public UpdateBuilder(@NotNull Bson... updates) {
update = Updates.combine(updates);
}

/**
* Creates a new {@link UpdateBuilder} instance with the given {@link List} of {@link #update}
*
* @param updates the {@link List} of {@link Bson updates} to start with
*/
public UpdateBuilder(@NotNull List<Bson> updates) {
update = Updates.combine(updates);
}

/**
* Creates a new {@link UpdateBuilder} instance with the given {@link UpdateBuilder}
*
* @param updateBuilder the {@link UpdateBuilder} to start with
*/
public UpdateBuilder(@NotNull UpdateBuilder updateBuilder) {
this.update = updateBuilder.update;
}

/**
* Creates a new {@link UpdateBuilder} instance with no starting {@link #update}
*/
public UpdateBuilder() {}

/**
* Combines the given {@link Bson update} with the current {@link #update}
*
* @param newUpdate the {@link Bson update} to add
*
* @return the current {@link UpdateBuilder} instance
*/
@NotNull
public UpdateBuilder add(@NotNull Bson newUpdate) {
update = Updates.combine(update, newUpdate);
return this;
}

/**
* Returns the final/built {@link Bson update}
*
* @return the final/built {@link Bson update}
*
* @throws IllegalStateException if the {@link #update} is null (only ever happens if {@link UpdateBuilder the builder} is created with no starting {@link #update})
*/
@NotNull
public Bson build() {
if (update == null) throw new IllegalStateException("update cannot be null");
Expand Down

0 comments on commit 4457846

Please sign in to comment.