Skip to content

Commit f41d11d

Browse files
committed
Added Document#get("key", defaultValue) method
JAVA-2504
1 parent 9c3d9fa commit f41d11d

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

bson/src/main/org/bson/Document.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ public <T> T get(final Object key, final Class<T> clazz) {
140140
return clazz.cast(documentAsMap.get(key));
141141
}
142142

143+
/**
144+
* Gets the value of the given key, casting it to {@code Class<T>} or returning the default value if null.
145+
* This is useful to avoid having casts in client code, though the effect is the same.
146+
*
147+
* @param key the key
148+
* @param defaultValue what to return if the value is null
149+
* @param <T> the type of the class
150+
* @return the value of the given key, or null if the instance does not contain this key.
151+
* @throws ClassCastException if the value of the given key is not of type T
152+
* @since 3.5
153+
*/
154+
@SuppressWarnings("unchecked")
155+
public <T> T get(final Object key, final T defaultValue) {
156+
notNull("defaultValue", defaultValue);
157+
Class<T> clazz = notNull("clazz", (Class<T>) defaultValue.getClass());
158+
Object value = documentAsMap.get(key);
159+
return value == null ? defaultValue : clazz.cast(value);
160+
}
161+
143162
/**
144163
* Gets the value of the given key as an Integer.
145164
*
@@ -160,8 +179,7 @@ public Integer getInteger(final Object key) {
160179
* @throws java.lang.ClassCastException if the value is not an integer
161180
*/
162181
public int getInteger(final Object key, final int defaultValue) {
163-
Object value = get(key);
164-
return value == null ? defaultValue : (Integer) value;
182+
return get(key, defaultValue);
165183
}
166184

167185
/**
@@ -217,8 +235,7 @@ public Boolean getBoolean(final Object key) {
217235
* @throws java.lang.ClassCastException if the value is not a boolean
218236
*/
219237
public boolean getBoolean(final Object key, final boolean defaultValue) {
220-
Object value = get(key);
221-
return value == null ? defaultValue : (Boolean) value;
238+
return get(key, defaultValue);
222239
}
223240

224241
/**

bson/src/test/unit/org/bson/types/DocumentSpecification.groovy

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,32 @@ class DocumentSpecification extends Specification {
3737
.append('objectId', objectId).append('date', date);
3838

3939
then:
40-
doc.getInteger('int') == 1;
41-
doc.getLong('long') == 2L;
42-
doc.getDouble('double') == 3.0d;
43-
doc.getString('string') == 'hi';
44-
doc.getBoolean('boolean');
45-
doc.getObjectId('objectId') == objectId;
46-
doc.getDate('date') == date;
47-
doc.get('objectId', ObjectId) == objectId;
40+
doc.getInteger('int') == 1
41+
doc.getInteger('intNoVal', 42) == 42
42+
doc.getLong('long') == 2L
43+
doc.getDouble('double') == 3.0d
44+
doc.getString('string') == 'hi'
45+
doc.getBoolean('boolean')
46+
doc.getBoolean('booleanNoVal', true)
47+
doc.getObjectId('objectId') == objectId
48+
doc.getDate('date') == date
49+
50+
51+
doc.get('objectId', ObjectId) == objectId
52+
doc.get('int', Integer) == 1
53+
doc.get('long', Long) == 2L
54+
doc.get('double', Double) == 3.0d
55+
doc.get('string', String) == 'hi'
56+
doc.get('boolean', Boolean)
57+
doc.get('date', Date) == date
58+
59+
doc.get('noVal', 42L) == 42L
60+
doc.get('noVal', 3.1d) == 3.1d
61+
doc.get('noVal', 'defVal') == 'defVal'
62+
doc.get('noVal', true)
63+
doc.get('noVal', objectId) == objectId
64+
doc.get('noVal', date) == date
65+
doc.get('noVal', objectId) == objectId
4866
}
4967

5068
def 'should parse a valid JSON string to a Document'() {

0 commit comments

Comments
 (0)