Skip to content

Expressions final #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion driver-core/src/main/com/mongodb/MongoClientSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.mongodb.annotations.Immutable;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.client.gridfs.codecs.GridFSFileCodecProvider;
import com.mongodb.client.model.expressions.ExpressionCodecProvider;
import com.mongodb.client.model.mql.ExpressionCodecProvider;
import com.mongodb.client.model.geojson.codecs.GeoJsonCodecProvider;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ConnectionPoolSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.mongodb.client.model.expressions;
package com.mongodb.client.model.mql;

import com.mongodb.annotations.Beta;
import com.mongodb.assertions.Assertions;
Expand All @@ -23,10 +23,10 @@
import java.util.List;
import java.util.function.Function;

import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.TYPE_ARGUMENT;
import static com.mongodb.client.model.mql.MqlUnchecked.Unchecked.TYPE_ARGUMENT;

/**
* Branches are used in {@linkplain Expression#switchOn}, and
* Branches are used in {@linkplain MqlValue#switchOn}, and
* define a sequence of checks that will be performed. The first check
* to succeed will produce the value that it specifies. If no check succeeds,
* then the operation
Expand All @@ -37,18 +37,18 @@
* @since 4.9.0
*/
@Beta(Beta.Reason.CLIENT)
public final class Branches<T extends Expression> {
public final class Branches<T extends MqlValue> {

Branches() {
}

private static <T extends Expression, R extends Expression> BranchesIntermediary<T, R> with(final Function<T, SwitchCase<R>> switchCase) {
private static <T extends MqlValue, R extends MqlValue> BranchesIntermediary<T, R> with(final Function<T, SwitchCase<R>> switchCase) {
List<Function<T, SwitchCase<R>>> v = new ArrayList<>();
v.add(switchCase);
return new BranchesIntermediary<>(v);
}

private static <T extends Expression> MqlExpression<?> mqlEx(final T value) {
private static <T extends MqlValue> MqlExpression<?> mqlEx(final T value) {
return (MqlExpression<?>) value;
}

Expand All @@ -63,7 +63,7 @@ private static <T extends Expression> MqlExpression<?> mqlEx(final T value) {
* @param <R> the type of the produced value.
* @return the appended sequence of checks.
*/
public <R extends Expression> BranchesIntermediary<T, R> is(final Function<? super T, BooleanExpression> predicate, final Function<? super T, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> is(final Function<? super T, MqlBoolean> predicate, final Function<? super T, ? extends R> mapping) {
Assertions.notNull("predicate", predicate);
Assertions.notNull("mapping", mapping);
return with(value -> new SwitchCase<>(predicate.apply(value), mapping.apply(value)));
Expand All @@ -72,23 +72,23 @@ public <R extends Expression> BranchesIntermediary<T, R> is(final Function<? sup
// eq lt lte

/**
* A successful check for {@linkplain Expression#eq equality}
* A successful check for {@linkplain MqlValue#eq equality}
* produces a value specified by the {@code mapping}.
*
* @param v the value to check against.
* @param mapping the mapping.
* @param <R> the type of the produced value.
* @return the appended sequence of checks.
*/
public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Function<? super T, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> eq(final T v, final Function<? super T, ? extends R> mapping) {
Assertions.notNull("v", v);
Assertions.notNull("mapping", mapping);
return is(value -> value.eq(v), mapping);
}

/**
* A successful check for being
* {@linkplain Expression#lt less than}
* {@linkplain MqlValue#lt less than}
* the provided value {@code v}
* produces a value specified by the {@code mapping}.
*
Expand All @@ -97,15 +97,15 @@ public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Fun
* @param <R> the type of the produced value.
* @return the appended sequence of checks.
*/
public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Function<? super T, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> lt(final T v, final Function<? super T, ? extends R> mapping) {
Assertions.notNull("v", v);
Assertions.notNull("mapping", mapping);
return is(value -> value.lt(v), mapping);
}

/**
* A successful check for being
* {@linkplain Expression#lte less than or equal to}
* {@linkplain MqlValue#lte less than or equal to}
* the provided value {@code v}
* produces a value specified by the {@code mapping}.
*
Expand All @@ -114,7 +114,7 @@ public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Fun
* @param <R> the type of the produced value.
* @return the appended sequence of checks.
*/
public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Function<? super T, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> lte(final T v, final Function<? super T, ? extends R> mapping) {
Assertions.notNull("v", v);
Assertions.notNull("mapping", mapping);
return is(value -> value.lte(v), mapping);
Expand All @@ -124,79 +124,79 @@ public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Fu

/**
* A successful check for
* {@linkplain Expression#isBooleanOr(BooleanExpression) being a boolean}
* {@linkplain MqlValue#isBooleanOr(MqlBoolean) being a boolean}
* produces a value specified by the {@code mapping}.
*
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isBoolean(final Function<? super BooleanExpression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isBoolean(final Function<? super MqlBoolean, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isBoolean(), v -> mapping.apply((BooleanExpression) v));
return is(v -> mqlEx(v).isBoolean(), v -> mapping.apply((MqlBoolean) v));
}

/**
* A successful check for
* {@linkplain Expression#isNumberOr(NumberExpression) being a number}
* {@linkplain MqlValue#isNumberOr(MqlNumber) being a number}
* produces a value specified by the {@code mapping}.
*
* @mongodb.server.release 4.4
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isNumber(final Function<? super NumberExpression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isNumber(final Function<? super MqlNumber, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isNumber(), v -> mapping.apply((NumberExpression) v));
return is(v -> mqlEx(v).isNumber(), v -> mapping.apply((MqlNumber) v));
}

/**
* A successful check for
* {@linkplain Expression#isIntegerOr(IntegerExpression) being an integer}
* {@linkplain MqlValue#isIntegerOr(MqlInteger) being an integer}
* produces a value specified by the {@code mapping}.
*
* @mongodb.server.release 4.4
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isInteger(final Function<? super IntegerExpression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isInteger(final Function<? super MqlInteger, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isInteger(), v -> mapping.apply((IntegerExpression) v));
return is(v -> mqlEx(v).isInteger(), v -> mapping.apply((MqlInteger) v));
}

/**
* A successful check for
* {@linkplain Expression#isStringOr(StringExpression) being a string}
* {@linkplain MqlValue#isStringOr(MqlString) being a string}
* produces a value specified by the {@code mapping}.
*
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isString(final Function<? super StringExpression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isString(final Function<? super MqlString, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isString(), v -> mapping.apply((StringExpression) v));
return is(v -> mqlEx(v).isString(), v -> mapping.apply((MqlString) v));
}

/**
* A successful check for
* {@linkplain Expression#isDateOr(DateExpression) being a date}
* {@linkplain MqlValue#isDateOr(MqlDate) being a date}
* produces a value specified by the {@code mapping}.
*
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<? super DateExpression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isDate(final Function<? super MqlDate, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isDate(), v -> mapping.apply((DateExpression) v));
return is(v -> mqlEx(v).isDate(), v -> mapping.apply((MqlDate) v));
}

/**
* A successful check for
* {@linkplain Expression#isArrayOr(ArrayExpression) being an array}
* {@linkplain MqlValue#isArrayOr(MqlArray) being an array}
* produces a value specified by the {@code mapping}.
*
* <p>Warning: The type argument of the array is not
Expand All @@ -209,32 +209,32 @@ public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<?
* @param <Q> the type of the array.
*/
@SuppressWarnings("unchecked")
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isArray(final Function<? super ArrayExpression<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> mapping) {
public <R extends MqlValue, Q extends MqlValue> BranchesIntermediary<T, R> isArray(final Function<? super MqlArray<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isArray(), v -> mapping.apply((ArrayExpression<Q>) v));
return is(v -> mqlEx(v).isArray(), v -> mapping.apply((MqlArray<Q>) v));
}

/**
* A successful check for
* {@linkplain Expression#isDocumentOr(DocumentExpression) being a document}
* {@linkplain MqlValue#isDocumentOr(MqlDocument) being a document}
* (or document-like value, see
* {@link MapExpression} and {@link EntryExpression})
* {@link MqlMap} and {@link MqlEntry})
* produces a value specified by the {@code mapping}.
*
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Function<? super DocumentExpression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isDocument(final Function<? super MqlDocument, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isDocumentOrMap(), v -> mapping.apply((DocumentExpression) v));
return is(v -> mqlEx(v).isDocumentOrMap(), v -> mapping.apply((MqlDocument) v));
}

/**
* A successful check for
* {@linkplain Expression#isMapOr(MapExpression) being a map}
* {@linkplain MqlValue#isMapOr(MqlMap) being a map}
* (or map-like value, see
* {@link DocumentExpression} and {@link EntryExpression})
* {@link MqlDocument} and {@link MqlEntry})
* produces a value specified by the {@code mapping}.
*
* <p>Warning: The type argument of the map is not
Expand All @@ -247,21 +247,21 @@ public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Functi
* @param <Q> the type of the array.
*/
@SuppressWarnings("unchecked")
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isMap(final Function<? super MapExpression<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> mapping) {
public <R extends MqlValue, Q extends MqlValue> BranchesIntermediary<T, R> isMap(final Function<? super MqlMap<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isDocumentOrMap(), v -> mapping.apply((MapExpression<Q>) v));
return is(v -> mqlEx(v).isDocumentOrMap(), v -> mapping.apply((MqlMap<Q>) v));
}

/**
* A successful check for
* {@linkplain Expressions#ofNull()} being the null value}
* {@linkplain MqlValues#ofNull()} being the null value}
* produces a value specified by the {@code mapping}.
*
* @param mapping the mapping.
* @return the appended sequence of checks.
* @param <R> the type of the produced value.
*/
public <R extends Expression> BranchesIntermediary<T, R> isNull(final Function<? super Expression, ? extends R> mapping) {
public <R extends MqlValue> BranchesIntermediary<T, R> isNull(final Function<? super MqlValue, ? extends R> mapping) {
Assertions.notNull("mapping", mapping);
return is(v -> mqlEx(v).isNull(), v -> mapping.apply(v));
}
Expand Down
Loading