Skip to content

Commit

Permalink
Create UpdateBuilder.java
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Apr 1, 2024
1 parent b653adb commit 450091d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/xyz/srnyx/magicmongo/UpdateBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package xyz.srnyx.magicmongo;

import com.mongodb.client.model.Updates;

import org.bson.conversions.Bson;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;


public class UpdateBuilder {
@Nullable private Bson update;

public UpdateBuilder(@NotNull Bson update) {
this.update = update;
}

public UpdateBuilder(@NotNull Bson... updates) {
update = Updates.combine(updates);
}

public UpdateBuilder(@NotNull List<Bson> updates) {
update = Updates.combine(updates);
}

public UpdateBuilder(@NotNull UpdateBuilder updateBuilder) {
this.update = updateBuilder.update;
}

public UpdateBuilder() {}

@NotNull
public UpdateBuilder add(@NotNull Bson newUpdate) {
update = Updates.combine(update, newUpdate);
return this;
}

@NotNull
public Bson build() {
if (update == null) throw new IllegalStateException("update cannot be null");
return update;
}
}

0 comments on commit 450091d

Please sign in to comment.