-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement switch expression #1055
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
15 commits
Select commit
Hold shift + click to select a range
12caad8
Implement switch expression
katcharov 396ae10
Expand pass and switch; add tests; fix broken test
katcharov 5c094dc
Fixes
katcharov b8749c4
Signature fixes
katcharov 820abdd
Fixes
katcharov 4482251
Fixes, add isInteger
katcharov 0306b45
Parameterize Branches using initiating method
katcharov 3d79be3
Fix
katcharov f814af1
Use switch
katcharov bb6a3bf
Remove ? super
katcharov 08ad990
Add ?super to Branches/Intermediary.
katcharov 43fc637
Fixes
katcharov 40693a5
Fix
katcharov a5f8b34
Post-rebase fixes, method renamed
katcharov 647e307
Checkstyle
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
97 changes: 97 additions & 0 deletions
97
driver-core/src/main/com/mongodb/client/model/expressions/Branches.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,97 @@ | ||
/* | ||
* 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 java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public final class Branches<T extends Expression> { | ||
|
||
Branches() { | ||
} | ||
|
||
private static <T extends Expression, R extends Expression> 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) { | ||
return (MqlExpression<?>) value; | ||
} | ||
|
||
// is fn | ||
|
||
public <T extends Expression, R extends Expression> BranchesIntermediary<T, R> is(final Function<? super T, BooleanExpression> o, final Function<? super T, ? extends R> r) { | ||
return with(value -> new SwitchCase<>(o.apply(value), r.apply(value))); | ||
} | ||
|
||
// eq lt lte | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Function<? super T, ? extends R> r) { | ||
return is(value -> value.eq(v), r); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Function<? super T, ? extends R> r) { | ||
return is(value -> value.lt(v), r); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Function<? super T, ? extends R> r) { | ||
return is(value -> value.lte(v), r); | ||
} | ||
|
||
// is type | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isBoolean(final Function<? super BooleanExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isBoolean(), v -> r.apply((BooleanExpression) v)); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isNumber(final Function<? super NumberExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isNumber(), v -> r.apply((NumberExpression) v)); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isInteger(final Function<? super IntegerExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isInteger(), v -> r.apply((IntegerExpression) v)); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isString(final Function<? super StringExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isString(), v -> r.apply((StringExpression) v)); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<? super DateExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isDate(), v -> r.apply((DateExpression) v)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isArray(final Function<? super ArrayExpression<Q>, ? extends R> r) { | ||
return is(v -> mqlEx(v).isArray(), v -> r.apply((ArrayExpression<Q>) v)); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Function<? super DocumentExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isDocumentOrMap(), v -> r.apply((DocumentExpression) v)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isMap(final Function<? super MapExpression<Q>, ? extends R> r) { | ||
return is(v -> mqlEx(v).isDocumentOrMap(), v -> r.apply((MapExpression<Q>) v)); | ||
} | ||
|
||
public <R extends Expression> BranchesIntermediary<T, R> isNull(final Function<? super Expression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isNull(), v -> r.apply(v)); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
driver-core/src/main/com/mongodb/client/model/expressions/BranchesIntermediary.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,102 @@ | ||
/* | ||
* 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 java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public final class BranchesIntermediary<T extends Expression, R extends Expression> extends BranchesTerminal<T, R> { | ||
BranchesIntermediary(final List<Function<T, SwitchCase<R>>> branches) { | ||
super(branches, null); | ||
} | ||
|
||
private BranchesIntermediary<T, R> with(final Function<T, SwitchCase<R>> switchCase) { | ||
List<Function<T, SwitchCase<R>>> v = new ArrayList<>(this.getBranches()); | ||
v.add(switchCase); | ||
return new BranchesIntermediary<>(v); | ||
} | ||
|
||
private static <T extends Expression> MqlExpression<?> mqlEx(final T value) { | ||
return (MqlExpression<?>) value; | ||
} | ||
|
||
// is fn | ||
|
||
public BranchesIntermediary<T, R> is(final Function<? super T, BooleanExpression> o, final Function<? super T, ? extends R> r) { | ||
return this.with(value -> new SwitchCase<>(o.apply(value), r.apply(value))); | ||
} | ||
|
||
// eq lt lte | ||
|
||
public BranchesIntermediary<T, R> eq(final T v, final Function<? super T, ? extends R> r) { | ||
return is(value -> value.eq(v), r); | ||
} | ||
|
||
public BranchesIntermediary<T, R> lt(final T v, final Function<? super T, ? extends R> r) { | ||
return is(value -> value.lt(v), r); | ||
} | ||
|
||
public BranchesIntermediary<T, R> lte(final T v, final Function<? super T, ? extends R> r) { | ||
return is(value -> value.lte(v), r); | ||
} | ||
|
||
// is type | ||
|
||
public BranchesIntermediary<T, R> isBoolean(final Function<? super BooleanExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isBoolean(), v -> r.apply((BooleanExpression) v)); | ||
} | ||
|
||
public BranchesIntermediary<T, R> isNumber(final Function<? super NumberExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isNumber(), v -> r.apply((NumberExpression) v)); | ||
} | ||
|
||
public BranchesIntermediary<T, R> isInteger(final Function<? super IntegerExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isInteger(), v -> r.apply((IntegerExpression) v)); | ||
} | ||
|
||
public BranchesIntermediary<T, R> isString(final Function<? super StringExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isString(), v -> r.apply((StringExpression) v)); | ||
} | ||
|
||
public BranchesIntermediary<T, R> isDate(final Function<? super DateExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isDate(), v -> r.apply((DateExpression) v)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <Q extends Expression> BranchesIntermediary<T, R> isArray(final Function<? super ArrayExpression<Q>, ? extends R> r) { | ||
return is(v -> mqlEx(v).isArray(), v -> r.apply((ArrayExpression<Q>) v)); | ||
} | ||
|
||
public BranchesIntermediary<T, R> isDocument(final Function<? super DocumentExpression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isDocumentOrMap(), v -> r.apply((DocumentExpression) v)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <Q extends Expression> BranchesIntermediary<T, R> isMap(final Function<? super MapExpression<Q>, ? extends R> r) { | ||
return is(v -> mqlEx(v).isDocumentOrMap(), v -> r.apply((MapExpression<Q>) v)); | ||
} | ||
|
||
public BranchesIntermediary<T, R> isNull(final Function<? super Expression, ? extends R> r) { | ||
return is(v -> mqlEx(v).isNull(), v -> r.apply(v)); | ||
} | ||
|
||
public BranchesTerminal<T, R> defaults(final Function<? super T, ? extends R> r) { | ||
return this.withDefault(value -> r.apply(value)); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
driver-core/src/main/com/mongodb/client/model/expressions/BranchesTerminal.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.lang.Nullable; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class BranchesTerminal<T extends Expression, R extends Expression> { | ||
|
||
private final List<Function<T, SwitchCase<R>>> branches; | ||
|
||
private final Function<T, R> defaults; | ||
|
||
BranchesTerminal(final List<Function<T, SwitchCase<R>>> branches, @Nullable final Function<T, R> defaults) { | ||
this.branches = branches; | ||
this.defaults = defaults; | ||
} | ||
|
||
protected BranchesTerminal<T, R> withDefault(final Function<T, R> defaults) { | ||
return new BranchesTerminal<>(branches, defaults); | ||
} | ||
|
||
protected List<Function<T, SwitchCase<R>>> getBranches() { | ||
return branches; | ||
} | ||
|
||
@Nullable | ||
protected Function<T, R> getDefaults() { | ||
return defaults; | ||
} | ||
} |
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
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
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
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.