Skip to content

Commit dd71640

Browse files
committed
JAVA-2636: Add getFirstKey method to BsonDocument
1 parent 6d80616 commit dd71640

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

bson/src/main/org/bson/BsonDocument.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,17 @@ public BsonDocument append(final String key, final BsonValue value) {
771771
return this;
772772
}
773773

774+
/**
775+
* Gets the first key in the document.
776+
*
777+
* @return the first key in the document
778+
* @throws java.util.NoSuchElementException if the document is empty
779+
* @since 3.6
780+
*/
781+
public String getFirstKey() {
782+
return keySet().iterator().next();
783+
}
784+
774785
@Override
775786
public boolean equals(final Object o) {
776787
if (this == o) {

bson/src/main/org/bson/RawBsonDocument.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.nio.ByteOrder;
3939
import java.util.Collection;
4040
import java.util.Map;
41+
import java.util.NoSuchElementException;
4142
import java.util.Set;
4243

4344
import static org.bson.assertions.Assertions.isTrueArgument;
@@ -243,6 +244,21 @@ public Set<String> keySet() {
243244
return toBsonDocument().keySet();
244245
}
245246

247+
@Override
248+
public String getFirstKey() {
249+
BsonBinaryReader bsonReader = createReader();
250+
try {
251+
bsonReader.readStartDocument();
252+
try {
253+
return bsonReader.readName();
254+
} catch (BsonInvalidOperationException e) {
255+
throw new NoSuchElementException();
256+
}
257+
} finally {
258+
bsonReader.close();
259+
}
260+
}
261+
246262
@Override
247263
public boolean containsKey(final Object key) {
248264
if (key == null) {

bson/src/test/unit/org/bson/BsonDocumentSpecification.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,25 @@ class BsonDocumentSpecification extends Specification {
324324
thrown(BsonInvalidOperationException)
325325
}
326326

327+
def 'should get first key'() {
328+
given:
329+
def document = new BsonDocument('i', new BsonInt32(2))
330+
331+
expect:
332+
document.getFirstKey() == 'i'
333+
}
334+
335+
def 'getFirstKey should throw NoSuchElementException if the document is empty'() {
336+
given:
337+
def document = new BsonDocument()
338+
339+
when:
340+
document.getFirstKey()
341+
342+
then:
343+
thrown(NoSuchElementException)
344+
}
345+
327346
def 'should serialize and deserialize'() {
328347
given:
329348
def document = new BsonDocument('d', new BsonDocument().append('i2', new BsonInt32(1)))

bson/src/test/unit/org/bson/RawBsonDocumentSpecification.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ class RawBsonDocumentSpecification extends Specification {
250250
rawDocument << createRawDocumentVariants()
251251
}
252252

253+
def 'should get first key'() {
254+
expect:
255+
document.getFirstKey() == 'a'
256+
257+
where:
258+
rawDocument << createRawDocumentVariants()
259+
}
260+
261+
def 'getFirstKey should throw NoSuchElementException if the document is empty'() {
262+
when:
263+
emptyRawDocument.getFirstKey()
264+
265+
then:
266+
thrown(NoSuchElementException)
267+
}
268+
253269
def 'toJson should return equivalent JSON'() {
254270
expect:
255271
new RawBsonDocumentCodec().decode(new JsonReader(rawDocument.toJson()), DecoderContext.builder().build()) == document

0 commit comments

Comments
 (0)