Skip to content

Commit 6237c6f

Browse files
author
Ryan
committed
added support for more getTYPE with default methods.
1 parent 28bbce1 commit 6237c6f

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/main/org/bson/BasicBSONObject.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ public long getLong( String key){
144144
return ((Number)foo).longValue();
145145
}
146146

147+
/**
148+
* Returns the value of a field as an <code>long</code>.
149+
* @param key the field to look for
150+
* @param def the default to return
151+
* @return the field value (or default)
152+
*/
153+
public long getLong( String key , long def ) {
154+
Object foo = get( key );
155+
if ( foo == null )
156+
return def;
157+
158+
return ((Number)foo).longValue();
159+
}
160+
147161
/**
148162
* Returns the value of a field as a <code>double</code>.
149163
*
@@ -155,6 +169,20 @@ public double getDouble( String key){
155169
return ((Number)foo).doubleValue();
156170
}
157171

172+
/**
173+
* Returns the value of a field as an <code>double</code>.
174+
* @param key the field to look for
175+
* @param def the default to return
176+
* @return the field value (or default)
177+
*/
178+
public double getDouble( String key , double def ) {
179+
Object foo = get( key );
180+
if ( foo == null )
181+
return def;
182+
183+
return ((Number)foo).doubleValue();
184+
}
185+
158186
/** Returns the value of a field as a string
159187
* @param key the field to look up
160188
* @return the value of the field, converted to a string
@@ -166,6 +194,20 @@ public String getString( String key ){
166194
return foo.toString();
167195
}
168196

197+
/**
198+
* Returns the value of a field as a string
199+
* @param key the field to look up
200+
* @param def the default to return
201+
* @return the value of the field, converted to a string
202+
*/
203+
public String getString( String key, final String def ) {
204+
Object foo = get( key );
205+
if ( foo == null )
206+
return def;
207+
208+
return foo.toString();
209+
}
210+
169211
/** Returns the value of a field as a boolean.
170212
* @param key the field to look up
171213
* @return the value of the field, or false if field does not exist

0 commit comments

Comments
 (0)