-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement boolean expressions #1025
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0723a9d
Implement boolean expressions
katcharov f7caf2d
Update docs, minor PR fixes, split tests
katcharov 87c0675
Use MqlExpression, checkstyle, rename abstract test class
katcharov 329edf9
Doc fixes, minor refactor, expand test
katcharov 8134e13
Use `of`, update docs, minor fixes
katcharov 188ba46
Various PR fixes
katcharov 65b8722
Wording
katcharov a069c8e
PR fixes
katcharov 86d3041
PR fixes
katcharov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
driver-core/src/main/com/mongodb/client/model/expressions/ArrayExpression.java
This file contains hidden or 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,27 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
/** | ||
* Expresses an array value. An array value is a finite, ordered collection of | ||
* elements of a certain type. | ||
* | ||
* @param <T> the type of the elements in the array | ||
*/ | ||
public interface ArrayExpression<T extends Expression> extends Expression { | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} |
61 changes: 61 additions & 0 deletions
61
driver-core/src/main/com/mongodb/client/model/expressions/BooleanExpression.java
This file contains hidden or 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,61 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
/** | ||
* Expresses a boolean value. | ||
*/ | ||
public interface BooleanExpression extends Expression { | ||
|
||
/** | ||
* Returns logical true if this expression evaluates to logical false. | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Returns logical false if this expression evaluates to logical true. | ||
* | ||
* @return True if false; false if true. | ||
*/ | ||
BooleanExpression not(); | ||
|
||
/** | ||
* Returns logical true if this or the other expression evaluates to logical | ||
* true. Returns logical false if both evaluate to logical false. | ||
* | ||
* @param or the other boolean expression. | ||
* @return True if either true, false if both false. | ||
*/ | ||
BooleanExpression or(BooleanExpression or); | ||
|
||
/** | ||
* Returns logical true if both this and the other expression evaluate to | ||
* logical true. Returns logical false if either evaluate to logical false. | ||
* | ||
* @param and the other boolean expression. | ||
* @return true if both true, false if either false. | ||
*/ | ||
BooleanExpression and(BooleanExpression and); | ||
|
||
/** | ||
* If this expression evaluates to logical true, returns the result of the | ||
* evaluated left branch expression. If this evaluates to logical false, | ||
* returns the result of the evaluated right branch expression. | ||
* | ||
* @param left the left branch expression | ||
* @param right the right branch expression | ||
* @return left if true, right if false. | ||
* @param <T> The type of the resulting expression. | ||
*/ | ||
<T extends Expression> T cond(T left, T right); | ||
} |
24 changes: 24 additions & 0 deletions
24
driver-core/src/main/com/mongodb/client/model/expressions/DateExpression.java
This file contains hidden or 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,24 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
/** | ||
* Expresses a date value. | ||
*/ | ||
public interface DateExpression extends Expression { | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
driver-core/src/main/com/mongodb/client/model/expressions/DocumentExpression.java
This file contains hidden or 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,25 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
/** | ||
* Expresses a document value. A document is an ordered set of fields, where the | ||
* key is a string value, mapping to a value of any other expression type. | ||
*/ | ||
public interface DocumentExpression extends Expression { | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
driver-core/src/main/com/mongodb/client/model/expressions/Expression.java
This file contains hidden or 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,47 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
import com.mongodb.annotations.Evolving; | ||
|
||
/** | ||
* Expressions express values that may be represented in (or computations that | ||
* may be performed within) a MongoDB server. Each expression evaluates to some | ||
* value, much like any Java expression evaluates to some value. Expressions may | ||
* be thought of as boxed values. Evaluation of an expression will usually occur | ||
* on a MongoDB server. | ||
* | ||
* <p>Users should treat these interfaces as sealed, and must not implement any | ||
* expression interfaces. | ||
* | ||
* <p>Expressions are typed. It is possible to execute expressions against data | ||
* that is of the wrong type, such as by applying the "not" boolean expression | ||
* to a document field that is an integer, null, or missing. This API does not | ||
* define the output in such cases (though the output may be defined within the | ||
* execution context - the server - where the expression is evaluated). Users of | ||
* this API must mitigate any risk of applying an expression to some type where | ||
* resulting behaviour is not defined by this API (for example, by checking for | ||
* null, by ensuring that field types are correctly specified). Likewise, unless | ||
* otherwise specified, this API does not define the order of evaluation for all | ||
* arguments, and whether all arguments to some expression will be evaluated. | ||
* | ||
* @see Expressions | ||
*/ | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Evolving | ||
public interface Expression { | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
driver-core/src/main/com/mongodb/client/model/expressions/ExpressionCodecProvider.java
This file contains hidden or 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,53 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
import com.mongodb.annotations.Beta; | ||
import com.mongodb.annotations.Immutable; | ||
import com.mongodb.lang.Nullable; | ||
import org.bson.codecs.Codec; | ||
import org.bson.codecs.configuration.CodecProvider; | ||
import org.bson.codecs.configuration.CodecRegistry; | ||
|
||
/** | ||
* Provides Codec instances for MQL expressions. | ||
* | ||
* <p>Responsible for converting values and computations expressed using the | ||
* driver's implementation of the {@link Expression} API into the corresponding | ||
* values and computations expressed in MQL BSON. Booleans are converted to BSON | ||
* booleans, documents to BSON documents, and so on. The specific structure | ||
* representing numbers is preserved where possible (that is, number literals | ||
* specified as Java longs are converted into BSON int64, and so on). | ||
* | ||
* <p>This API is marked Beta because it may be replaced with a generalized | ||
* mechanism for converting expressions. This would only affect users who use | ||
* MqlExpressionCodecProvider directly in custom codec providers. This Beta | ||
* annotation does not imply that the Expressions API in general is Beta. | ||
*/ | ||
@Beta(Beta.Reason.CLIENT) | ||
@Immutable | ||
public final class ExpressionCodecProvider implements CodecProvider { | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
@Nullable | ||
public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) { | ||
if (MqlExpression.class.equals(clazz)) { | ||
return (Codec<T>) new MqlExpressionCodec(registry); | ||
} | ||
return null; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
driver-core/src/main/com/mongodb/client/model/expressions/Expressions.java
This file contains hidden or 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 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
import org.bson.BsonBoolean; | ||
import org.bson.BsonInt32; | ||
import org.bson.BsonString; | ||
|
||
/** | ||
* Convenience methods related to {@link Expression}. | ||
*/ | ||
public final class Expressions { | ||
|
||
private Expressions() {} | ||
|
||
/** | ||
* Returns an expression having the same boolean value as the provided | ||
* boolean primitive. | ||
* | ||
* @param of the boolean primitive | ||
* @return the boolean expression | ||
*/ | ||
public static BooleanExpression of(final boolean of) { | ||
// we intentionally disallow ofBoolean(null) | ||
return new MqlExpression<>((codecRegistry) -> new BsonBoolean(of)); | ||
} | ||
|
||
/** | ||
* Returns an expression having the same integer value as the provided | ||
* int primitive. | ||
* | ||
* @param of the int primitive | ||
* @return the integer expression | ||
*/ | ||
public static IntegerExpression of(final int of) { | ||
return new MqlExpression<>((codecRegistry) -> new BsonInt32(of)); | ||
} | ||
|
||
/** | ||
* Returns an expression having the same string value as the provided | ||
* string. | ||
* | ||
* @param of the string | ||
* @return the string expression | ||
*/ | ||
public static StringExpression of(final String of) { | ||
return new MqlExpression<>((codecRegistry) -> new BsonString(of)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
driver-core/src/main/com/mongodb/client/model/expressions/IntegerExpression.java
This file contains hidden or 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,24 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model.expressions; | ||
|
||
/** | ||
* Expresses an integer value. | ||
*/ | ||
public interface IntegerExpression extends NumberExpression { | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.