-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
247 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/xyz/srnyx/magicmongo/builders/IndexBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package xyz.srnyx.magicmongo.builders; | ||
|
||
import com.mongodb.client.model.Indexes; | ||
|
||
import org.bson.conversions.Bson; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
|
||
/** | ||
* A simple builder for {@link Indexes MongoDB indexes} | ||
*/ | ||
public class IndexBuilder extends MongoBsonBuilder<IndexBuilder> { | ||
/** | ||
* Creates a new {@link IndexBuilder} instance with the given indexes | ||
* | ||
* @param indexes the {@link Bson indexes} to start with | ||
*/ | ||
public IndexBuilder(@NotNull Bson... indexes) { | ||
super(Indexes.compoundIndex(indexes)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link IndexBuilder} instance with the given {@link List} of indexes | ||
* | ||
* @param indexes the {@link List} of {@link Bson indexes} to start with | ||
*/ | ||
public IndexBuilder(@NotNull List<Bson> indexes) { | ||
super(Indexes.compoundIndex(indexes)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link IndexBuilder} instance with the given {@link IndexBuilder} | ||
* | ||
* @param indexBuilder the {@link IndexBuilder} to start with | ||
*/ | ||
public IndexBuilder(@NotNull IndexBuilder indexBuilder) { | ||
super(indexBuilder.bson); | ||
} | ||
|
||
/** | ||
* Combines the given {@link Bson index} with the {@link #bson current index} using {@link Indexes#compoundIndex(Bson...)} | ||
* | ||
* @param newIndex the {@link Bson index} to add | ||
* | ||
* @return the current {@link IndexBuilder} instance | ||
*/ | ||
@NotNull | ||
public IndexBuilder add(@NotNull Bson newIndex) { | ||
return set(bson == null ? newIndex : Indexes.compoundIndex(bson, newIndex)); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/xyz/srnyx/magicmongo/builders/MongoBsonBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package xyz.srnyx.magicmongo.builders; | ||
|
||
import org.bson.conversions.Bson; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
|
||
/** | ||
* A simple builder for {@link Bson MongoDB Bson} objects | ||
* | ||
* @param <T> the type of the builder | ||
* | ||
* @see FilterBuilder | ||
* @see UpdateBuilder | ||
* @see SortBuilder | ||
*/ | ||
public abstract class MongoBsonBuilder<T extends MongoBsonBuilder<T>> { | ||
/** | ||
* The {@link Bson} to build | ||
*/ | ||
@Nullable protected Bson bson; | ||
|
||
/** | ||
* Creates a new {@link MongoBsonBuilder} instance with no starting {@link #bson} | ||
*/ | ||
public MongoBsonBuilder() {} | ||
|
||
/** | ||
* Creates a new {@link MongoBsonBuilder} instance with the given {@link Bson} | ||
* | ||
* @param bson the {@link Bson} to start with | ||
*/ | ||
public MongoBsonBuilder(@Nullable Bson bson) { | ||
this.bson = bson; | ||
} | ||
|
||
/** | ||
* Builds the {@link Bson} object | ||
* | ||
* @return the built {@link Bson} object | ||
* | ||
* @throws IllegalStateException if the {@link #bson} is null | ||
*/ | ||
@NotNull | ||
public Bson build() { | ||
if (bson == null) throw new IllegalStateException("bson cannot be null!"); | ||
return bson; | ||
} | ||
|
||
/** | ||
* Sets the {@link Bson} to the given {@link Bson} | ||
* | ||
* @param bson the {@link Bson} to set | ||
* | ||
* @return the current {@link MongoBsonBuilder} instance casted to the type of the builder | ||
*/ | ||
@NotNull | ||
public T set(@Nullable Bson bson) { | ||
this.bson = bson; | ||
return (T) this; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/xyz/srnyx/magicmongo/builders/ProjectionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package xyz.srnyx.magicmongo.builders; | ||
|
||
import com.mongodb.client.model.Projections; | ||
|
||
import org.bson.conversions.Bson; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
|
||
/** | ||
* A simple builder for {@link Projections MongoDB projections} | ||
*/ | ||
public class ProjectionBuilder extends MongoBsonBuilder<ProjectionBuilder> { | ||
/** | ||
* Creates a new {@link ProjectionBuilder} instance with the given projections | ||
* | ||
* @param projections the {@link Bson projections} to start with | ||
*/ | ||
public ProjectionBuilder(@NotNull Bson... projections) { | ||
super(Projections.fields(projections)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ProjectionBuilder} instance with the given {@link List} of projections | ||
* | ||
* @param projections the {@link List} of {@link Bson projections} to start with | ||
*/ | ||
public ProjectionBuilder(@NotNull List<Bson> projections) { | ||
super(Projections.fields(projections)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ProjectionBuilder} instance with the given {@link ProjectionBuilder} | ||
* | ||
* @param projectionBuilder the {@link ProjectionBuilder} to start with | ||
*/ | ||
public ProjectionBuilder(@NotNull ProjectionBuilder projectionBuilder) { | ||
super(projectionBuilder.bson); | ||
} | ||
|
||
/** | ||
* Combines the given {@link Bson projection} with the {@link #bson current projection} using {@link Projections#fields(Bson...)} | ||
* | ||
* @param newProjection the {@link Bson projection} to add | ||
* | ||
* @return the current {@link ProjectionBuilder} instance | ||
*/ | ||
@NotNull | ||
public ProjectionBuilder add(@NotNull Bson newProjection) { | ||
return set(bson == null ? newProjection : Projections.fields(bson, newProjection)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/xyz/srnyx/magicmongo/builders/SortBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package xyz.srnyx.magicmongo.builders; | ||
|
||
import com.mongodb.client.model.Sorts; | ||
|
||
import org.bson.conversions.Bson; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
|
||
/** | ||
* A simple builder for {@link Sorts MongoDB sorts} | ||
*/ | ||
public class SortBuilder extends MongoBsonBuilder<SortBuilder> { | ||
/** | ||
* Creates a new {@link SortBuilder} instance with the given sorts | ||
* | ||
* @param sorts the {@link Bson sorts} to start with | ||
*/ | ||
public SortBuilder(@NotNull Bson... sorts) { | ||
super(Sorts.orderBy(sorts)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link SortBuilder} instance with the given {@link List} of sorts | ||
* | ||
* @param sorts the {@link List} of {@link Bson sorts} to start with | ||
*/ | ||
public SortBuilder(@NotNull List<Bson> sorts) { | ||
super(Sorts.orderBy(sorts)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link SortBuilder} instance with the given {@link SortBuilder} | ||
* | ||
* @param sortBuilder the {@link SortBuilder} to start with | ||
*/ | ||
public SortBuilder(@NotNull SortBuilder sortBuilder) { | ||
super(sortBuilder.bson); | ||
} | ||
|
||
/** | ||
* Combines the given {@link Bson sort} with the {@link #bson current sort} using {@link Sorts#orderBy(Bson...)} | ||
* | ||
* @param newSort the {@link Bson sort} to add | ||
* | ||
* @return the current {@link SortBuilder} instance | ||
*/ | ||
@NotNull | ||
public SortBuilder add(@NotNull Bson newSort) { | ||
return set(bson == null ? newSort : Sorts.orderBy(bson, newSort)); | ||
} | ||
} |
Oops, something went wrong.