Skip to content

Commit 786eb3b

Browse files
committed
Let's introduce AbstractClientDeleteOptions, AbstractClientUpdateOptions
This allows for code reuse. mongodb#1593
1 parent 88b7cd2 commit 786eb3b

11 files changed

+286
-370
lines changed

driver-core/src/main/com/mongodb/client/model/bulk/ClientDeleteOneOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @since 5.3
2828
*/
2929
@Sealed
30-
public interface ClientDeleteOneOptions extends BaseClientDeleteOptions{
30+
public interface ClientDeleteOneOptions extends BaseClientDeleteOptions {
3131
/**
3232
* Creates the default options.
3333
*

driver-core/src/main/com/mongodb/internal/client/model/bulk/AbstractClientDeleteModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
/**
2121
* This class is not part of the public API and may be removed or changed at any time.
2222
*/
23-
public abstract class AbstractClientDeleteModel<T> implements ClientWriteModel {
23+
public abstract class AbstractClientDeleteModel<O extends AbstractClientDeleteOptions> implements ClientWriteModel {
2424
private final Bson filter;
25-
private final T options;
25+
private final O options;
2626

27-
AbstractClientDeleteModel(final Bson filter, final T options) {
27+
AbstractClientDeleteModel(final Bson filter, final O options) {
2828
this.filter = filter;
2929
this.options = options;
3030
}
@@ -33,7 +33,7 @@ public final Bson getFilter() {
3333
return filter;
3434
}
3535

36-
public final T getOptions() {
36+
public final O getOptions() {
3737
return options;
3838
}
3939

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.internal.client.model.bulk;
17+
18+
import com.mongodb.client.model.Collation;
19+
import com.mongodb.lang.Nullable;
20+
import org.bson.conversions.Bson;
21+
22+
import java.util.Optional;
23+
24+
import static java.util.Optional.ofNullable;
25+
26+
/**
27+
* This class is not part of the public API and may be removed or changed at any time.
28+
*/
29+
public abstract class AbstractClientDeleteOptions {
30+
@Nullable
31+
private Collation collation;
32+
@Nullable
33+
private Bson hint;
34+
@Nullable
35+
private String hintString;
36+
37+
AbstractClientDeleteOptions() {
38+
}
39+
40+
public AbstractClientDeleteOptions collation(@Nullable final Collation collation) {
41+
this.collation = collation;
42+
return this;
43+
}
44+
45+
/**
46+
* @see #collation(Collation)
47+
*/
48+
public Optional<Collation> getCollation() {
49+
return ofNullable(collation);
50+
}
51+
52+
public AbstractClientDeleteOptions hint(@Nullable final Bson hint) {
53+
this.hint = hint;
54+
this.hintString = null;
55+
return this;
56+
}
57+
58+
/**
59+
* @see #hint(Bson)
60+
*/
61+
public Optional<Bson> getHint() {
62+
return ofNullable(hint);
63+
}
64+
65+
public AbstractClientDeleteOptions hintString(@Nullable final String hintString) {
66+
this.hintString = hintString;
67+
this.hint = null;
68+
return this;
69+
}
70+
71+
/**
72+
* @see #hintString(String)
73+
*/
74+
public Optional<String> getHintString() {
75+
return ofNullable(hintString);
76+
}
77+
78+
abstract String getToStringDescription();
79+
80+
@Override
81+
public String toString() {
82+
return getToStringDescription()
83+
+ "{collation=" + collation
84+
+ ", hint=" + hint
85+
+ ", hintString='" + hintString + '\''
86+
+ '}';
87+
}
88+
}

driver-core/src/main/com/mongodb/internal/client/model/bulk/AbstractClientUpdateModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@
2626
/**
2727
* This class is not part of the public API and may be removed or changed at any time.
2828
*/
29-
public abstract class AbstractClientUpdateModel<T> {
29+
public abstract class AbstractClientUpdateModel<O extends AbstractClientUpdateOptions> {
3030
private final Bson filter;
3131
@Nullable
3232
private final Bson update;
3333
@Nullable
3434
private final Iterable<? extends Bson> updatePipeline;
35-
private final T options;
35+
private final O options;
3636

3737
AbstractClientUpdateModel(
3838
final Bson filter,
3939
@Nullable
4040
final Bson update,
4141
@Nullable final Iterable<? extends Bson> updatePipeline,
42-
final T options) {
42+
final O options) {
4343
this.filter = filter;
4444
assertTrue(update == null ^ updatePipeline == null);
4545
this.update = update;
@@ -59,7 +59,7 @@ public final Optional<Iterable<? extends Bson>> getUpdatePipeline() {
5959
return ofNullable(updatePipeline);
6060
}
6161

62-
public final T getOptions() {
62+
public final O getOptions() {
6363
return options;
6464
}
6565

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.internal.client.model.bulk;
17+
18+
import com.mongodb.client.model.Collation;
19+
import com.mongodb.lang.Nullable;
20+
import org.bson.conversions.Bson;
21+
22+
import java.util.Optional;
23+
24+
import static java.util.Optional.ofNullable;
25+
26+
/**
27+
* This class is not part of the public API and may be removed or changed at any time.
28+
*/
29+
public abstract class AbstractClientUpdateOptions {
30+
@Nullable
31+
private Iterable<? extends Bson> arrayFilters;
32+
@Nullable
33+
private Collation collation;
34+
@Nullable
35+
private Bson hint;
36+
@Nullable
37+
private String hintString;
38+
@Nullable
39+
private Boolean upsert;
40+
41+
AbstractClientUpdateOptions() {
42+
}
43+
44+
public AbstractClientUpdateOptions arrayFilters(@Nullable final Iterable<? extends Bson> arrayFilters) {
45+
this.arrayFilters = arrayFilters;
46+
return this;
47+
}
48+
49+
/**
50+
* @see #arrayFilters(Iterable)
51+
*/
52+
public Optional<Iterable<? extends Bson>> getArrayFilters() {
53+
return ofNullable(arrayFilters);
54+
}
55+
56+
public AbstractClientUpdateOptions collation(@Nullable final Collation collation) {
57+
this.collation = collation;
58+
return this;
59+
}
60+
61+
/**
62+
* @see #collation(Collation)
63+
*/
64+
public Optional<Collation> getCollation() {
65+
return ofNullable(collation);
66+
}
67+
68+
public AbstractClientUpdateOptions hint(@Nullable final Bson hint) {
69+
this.hint = hint;
70+
this.hintString = null;
71+
return this;
72+
}
73+
74+
/**
75+
* @see #hint(Bson)
76+
*/
77+
public Optional<Bson> getHint() {
78+
return ofNullable(hint);
79+
}
80+
81+
public AbstractClientUpdateOptions hintString(@Nullable final String hintString) {
82+
this.hintString = hintString;
83+
this.hint = null;
84+
return this;
85+
}
86+
87+
/**
88+
* @see #hintString(String)
89+
*/
90+
public Optional<String> getHintString() {
91+
return ofNullable(hintString);
92+
}
93+
94+
public AbstractClientUpdateOptions upsert(@Nullable final Boolean upsert) {
95+
this.upsert = upsert;
96+
return this;
97+
}
98+
99+
/**
100+
* @see #isUpsert()
101+
*/
102+
public Optional<Boolean> isUpsert() {
103+
return ofNullable(upsert);
104+
}
105+
106+
abstract String getToStringDescription();
107+
108+
@Override
109+
public String toString() {
110+
return getToStringDescription()
111+
+ "{arrayFilters=" + arrayFilters
112+
+ ", collation=" + collation
113+
+ ", hint=" + hint
114+
+ ", hintString='" + hintString + '\''
115+
+ ", upsert=" + upsert
116+
+ '}';
117+
}
118+
}

driver-core/src/main/com/mongodb/internal/client/model/bulk/ConcreteClientDeleteManyOptions.java

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,73 +20,29 @@
2020
import com.mongodb.lang.Nullable;
2121
import org.bson.conversions.Bson;
2222

23-
import java.util.Optional;
24-
25-
import static java.util.Optional.ofNullable;
26-
2723
/**
2824
* This class is not part of the public API and may be removed or changed at any time.
2925
*/
30-
public final class ConcreteClientDeleteManyOptions implements ClientDeleteManyOptions {
26+
public final class ConcreteClientDeleteManyOptions extends AbstractClientDeleteOptions implements ClientDeleteManyOptions {
3127
static final ConcreteClientDeleteManyOptions MUTABLE_EMPTY = new ConcreteClientDeleteManyOptions();
3228

33-
@Nullable
34-
private Collation collation;
35-
@Nullable
36-
private Bson hint;
37-
@Nullable
38-
private String hintString;
39-
4029
public ConcreteClientDeleteManyOptions() {
4130
}
4231

43-
@Override
44-
public ClientDeleteManyOptions collation(@Nullable final Collation collation) {
45-
this.collation = collation;
46-
return this;
47-
}
48-
49-
/**
50-
* @see #collation(Collation)
51-
*/
52-
public Optional<Collation> getCollation() {
53-
return ofNullable(collation);
32+
public ConcreteClientDeleteManyOptions collation(@Nullable final Collation collation) {
33+
return (ConcreteClientDeleteManyOptions) super.collation(collation);
5434
}
5535

56-
@Override
57-
public ClientDeleteManyOptions hint(@Nullable final Bson hint) {
58-
this.hint = hint;
59-
this.hintString = null;
60-
return this;
61-
}
62-
63-
/**
64-
* @see #hint(Bson)
65-
*/
66-
public Optional<Bson> getHint() {
67-
return ofNullable(hint);
68-
}
69-
70-
@Override
71-
public ClientDeleteManyOptions hintString(@Nullable final String hintString) {
72-
this.hintString = hintString;
73-
this.hint = null;
74-
return this;
36+
public ConcreteClientDeleteManyOptions hint(@Nullable final Bson hint) {
37+
return (ConcreteClientDeleteManyOptions) super.hint(hint);
7538
}
7639

77-
/**
78-
* @see #hintString(String)
79-
*/
80-
public Optional<String> getHintString() {
81-
return ofNullable(hintString);
40+
public ConcreteClientDeleteManyOptions hintString(@Nullable final String hintString) {
41+
return (ConcreteClientDeleteManyOptions) super.hintString(hintString);
8242
}
8343

8444
@Override
85-
public String toString() {
86-
return "ClientDeleteManyOptions{"
87-
+ "collation=" + collation
88-
+ ", hint=" + hint
89-
+ ", hintString='" + hintString + '\''
90-
+ '}';
45+
String getToStringDescription() {
46+
return "ConcreteClientDeleteManyOptions";
9147
}
9248
}

0 commit comments

Comments
 (0)