Skip to content

Commit

Permalink
Revamp builders and add more
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Apr 3, 2024
1 parent a8c92d5 commit 7d12e72
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 78 deletions.
50 changes: 14 additions & 36 deletions src/main/java/xyz/srnyx/magicmongo/builders/FilterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,38 @@
import org.bson.conversions.Bson;

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

import java.util.function.BinaryOperator;


public class FilterBuilder {
/**
* A simple builder for {@link Filters MongoDB filters}
*/
public class FilterBuilder extends MongoBsonBuilder<FilterBuilder> {
/**
* The {@link Bson filter} to build
*/
@Nullable private Bson filter;

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

/**
* Creates a new {@link FilterBuilder} instance with the given {@link #filter}
* Creates a new {@link FilterBuilder} instance with no starting filter
*
* @param filter the {@link Bson filter} to start with
* @see MongoBsonBuilder#MongoBsonBuilder()
*/
public FilterBuilder(@Nullable Bson filter) {
this.filter = filter;
}

/**
* Returns the final/built {@link Bson filter}
*
* @return the final/built {@link Bson filter}
*
* @throws IllegalStateException if the {@link #filter} is null (only ever happens if {@link FilterBuilder the builder} is created with no starting {@link #filter})
*/
@NotNull
public Bson build() {
if (filter == null) throw new IllegalStateException("filter cannot be null");
return filter;
public FilterBuilder() {
// Only exists to provide a Javadoc
}

/**
* Adds a new {@link Bson filter} to the current {@link #filter}
* Adds a new {@link Bson filter} to the current filter
*
* @param filterFunction the {@link BinaryOperator} to apply to the current {@link #filter} with the new {@link Bson filter}
* @param filterFunction the {@link BinaryOperator} to apply to the current filter with the new {@link Bson filter}
* @param newFilter the new {@link Bson filter} to add
*
* @return the current {@link FilterBuilder}
*/
@NotNull
public FilterBuilder add(@NotNull BinaryOperator<Bson> filterFunction, @NotNull Bson newFilter) {
filter = filterFunction.apply(filter, newFilter);
return this;
return set(bson == null ? newFilter : filterFunction.apply(bson, newFilter));
}

/**
* Adds a new {@link Bson filter} to the current {@link #filter} using {@link Filters#and(Bson...)}
* Adds a new {@link Bson filter} to the current filter using {@link Filters#and(Bson...)}
*
* @param newFilter the new {@link Bson filter} to add
*
Expand All @@ -70,7 +48,7 @@ public FilterBuilder and(@NotNull Bson newFilter) {
}

/**
* Adds a new {@link Bson filter} to the current {@link #filter} using {@link Filters#or(Bson...)}
* Adds a new {@link Bson filter} to the current filter using {@link Filters#or(Bson...)}
*
* @param newFilter the new {@link Bson filter} to add
*
Expand All @@ -82,7 +60,7 @@ public FilterBuilder or(@NotNull Bson newFilter) {
}

/**
* Adds a new {@link Bson filter} to the current {@link #filter} using {@link Filters#nor(Bson...)}
* Adds a new {@link Bson filter} to the current filter using {@link Filters#nor(Bson...)}
*
* @param newFilter the new {@link Bson filter} to add
*
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/xyz/srnyx/magicmongo/builders/IndexBuilder.java
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 src/main/java/xyz/srnyx/magicmongo/builders/MongoBsonBuilder.java
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 src/main/java/xyz/srnyx/magicmongo/builders/ProjectionBuilder.java
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 src/main/java/xyz/srnyx/magicmongo/builders/SortBuilder.java
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));
}
}
Loading

0 comments on commit 7d12e72

Please sign in to comment.