Skip to content

Commit 21bb22e

Browse files
committed
Make ClientInsertOneResult.getInsertedId return Optional
JAVA-5527
1 parent dbf9a26 commit 21bb22e

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

driver-core/src/main/com/mongodb/client/result/bulk/ClientInsertOneResult.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import com.mongodb.annotations.Evolving;
1919
import com.mongodb.bulk.WriteConcernError;
2020
import com.mongodb.client.model.bulk.ClientWriteModel;
21-
import com.mongodb.lang.Nullable;
2221
import org.bson.BsonValue;
2322
import org.bson.RawBsonDocument;
2423

24+
import java.util.Optional;
25+
2526
/**
2627
* The result of a successful {@linkplain ClientWriteModel individual insert one operation}.
2728
* Note that {@link WriteConcernError}s are not considered as making individuals operations unsuccessful.
@@ -33,10 +34,10 @@ public interface ClientInsertOneResult {
3334
/**
3435
* The {@code "_id"} of the inserted document.
3536
*
36-
* @return The {@code "_id"} of the inserted document, or {@code null} if one is not available,
37-
* which happens when a {@link RawBsonDocument} without {@code "_id"} is inserted,
38-
* because the driver does not generate missing {@code "_id"} fields for {@link RawBsonDocument}s.
37+
* @return The {@code "_id"} of the inserted document.
38+
* {@linkplain Optional#isPresent() Present} unless a {@link RawBsonDocument} is inserted,
39+
* because the driver neither generates the missing {@code "_id"} field for a {@link RawBsonDocument},
40+
* nor does it read the {@code "_id"} field from a {@link RawBsonDocument} when inserting it.
3941
*/
40-
@Nullable
41-
BsonValue getInsertedId();
42+
Optional<BsonValue> getInsertedId();
4243
}

driver-core/src/main/com/mongodb/internal/client/result/bulk/ConcreteClientInsertOneResult.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.bson.BsonValue;
2121

2222
import java.util.Objects;
23+
import java.util.Optional;
24+
25+
import static java.util.Optional.ofNullable;
2326

2427
/**
2528
* This class is not part of the public API and may be removed or changed at any time.
@@ -33,9 +36,8 @@ public ConcreteClientInsertOneResult(@Nullable final BsonValue insertedId) {
3336
}
3437

3538
@Override
36-
@Nullable
37-
public BsonValue getInsertedId() {
38-
return insertedId;
39+
public Optional<BsonValue> getInsertedId() {
40+
return ofNullable(insertedId);
3941
}
4042

4143
@Override

0 commit comments

Comments
 (0)