Skip to content

Commit 7e17904

Browse files
author
Ryan
committed
added getObjectId method.
1 parent 74476aa commit 7e17904

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/main/org/bson/BasicBSONObject.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818

1919
package org.bson;
2020

21-
import java.util.*;
21+
// BSON
22+
import org.bson.types.ObjectId;
23+
24+
// Java
25+
import java.util.Map;
26+
import java.util.Set;
27+
import java.util.LinkedHashMap;
2228
import java.util.regex.Pattern;
2329

2430
/**
25-
* A simple implementation of <code>DBObject</code>.
31+
* A simple implementation of <code>DBObject</code>.
2632
* A <code>DBObject</code> can be created as follows, using this class:
2733
* <blockquote><pre>
2834
* DBObject obj = new BasicBSONObject();
@@ -32,7 +38,7 @@
3238
public class BasicBSONObject extends LinkedHashMap<String,Object> implements BSONObject {
3339

3440
private static final long serialVersionUID = -4415279469780082174L;
35-
41+
3642
/**
3743
* Creates an empty object.
3844
*/
@@ -56,7 +62,7 @@ public BasicBSONObject(String key, Object value){
5662
* Creates a DBObject from a map.
5763
* @param m map to convert
5864
*/
59-
@SuppressWarnings("unchecked")
65+
@SuppressWarnings("unchecked")
6066
public BasicBSONObject(Map m) {
6167
super(m);
6268
}
@@ -69,7 +75,7 @@ public Map toMap() {
6975
return new LinkedHashMap<String,Object>(this);
7076
}
7177

72-
/** Deletes a field from this object.
78+
/** Deletes a field from this object.
7379
* @param key the field name to remove
7480
* @return the object removed
7581
*/
@@ -109,7 +115,7 @@ public int getInt( String key ){
109115
Object o = get(key);
110116
if ( o == null )
111117
throw new NullPointerException( "no value for: " + key );
112-
118+
113119
return BSON.toInt( o );
114120
}
115121

@@ -122,15 +128,15 @@ public int getInt( String key , int def ){
122128
Object foo = get( key );
123129
if ( foo == null )
124130
return def;
125-
131+
126132
return BSON.toInt( foo );
127133
}
128134

129135
/**
130136
* Returns the value of a field as a <code>long</code>.
131137
*
132138
* @param key the field to return
133-
* @return the field value
139+
* @return the field value
134140
*/
135141
public long getLong( String key){
136142
Object foo = get( key );
@@ -141,13 +147,13 @@ public long getLong( String key){
141147
* Returns the value of a field as a <code>double</code>.
142148
*
143149
* @param key the field to return
144-
* @return the field value
150+
* @return the field value
145151
*/
146152
public double getDouble( String key){
147153
Object foo = get( key );
148154
return ((Number)foo).doubleValue();
149155
}
150-
156+
151157
/** Returns the value of a field as a string
152158
* @param key the field to look up
153159
* @return the value of the field, converted to a string
@@ -183,6 +189,16 @@ public boolean getBoolean( String key , boolean def ){
183189
throw new IllegalArgumentException( "can't coerce to bool:" + foo.getClass() );
184190
}
185191

192+
/**
193+
* Returns the object id or null if not set.
194+
* @param field The field to return
195+
* @return The field object value or null if not found (or if null :-^).
196+
*/
197+
public ObjectId getObjectId( final String field ) {
198+
Object obj = get( field );
199+
return (obj != null) ? (ObjectId)obj : null;
200+
}
201+
186202
/** Add a key/value pair to this object
187203
* @param key the field name
188204
* @param val the field value
@@ -192,18 +208,18 @@ public Object put( String key , Object val ){
192208
return super.put( key , val );
193209
}
194210

195-
@SuppressWarnings("unchecked")
211+
@SuppressWarnings("unchecked")
196212
public void putAll( Map m ){
197213
for ( Map.Entry entry : (Set<Map.Entry>)m.entrySet() ){
198214
put( entry.getKey().toString() , entry.getValue() );
199215
}
200-
}
201-
216+
}
217+
202218
public void putAll( BSONObject o ){
203219
for ( String k : o.keySet() ){
204220
put( k , o.get( k ) );
205221
}
206-
}
222+
}
207223

208224
/** Add a key/value pair to this object
209225
* @param key the field name
@@ -218,23 +234,23 @@ public BasicBSONObject append( String key , Object val ){
218234

219235
/** Returns a JSON serialization of this object
220236
* @return JSON serialization
221-
*/
237+
*/
222238
public String toString(){
223239
return com.mongodb.util.JSON.serialize( this );
224240
}
225241

226242
public boolean equals( Object o ){
227243
if ( ! ( o instanceof BSONObject ) )
228244
return false;
229-
245+
230246
BSONObject other = (BSONObject)o;
231247
if ( ! keySet().equals( other.keySet() ) )
232248
return false;
233249

234250
for ( String key : keySet() ){
235251
Object a = get( key );
236252
Object b = other.get( key );
237-
253+
238254
if ( a == null ){
239255
if ( b != null )
240256
return false;
@@ -244,7 +260,7 @@ public boolean equals( Object o ){
244260
return false;
245261
}
246262
else if ( a instanceof Number && b instanceof Number ){
247-
if ( ((Number)a).doubleValue() !=
263+
if ( ((Number)a).doubleValue() !=
248264
((Number)b).doubleValue() )
249265
return false;
250266
}

0 commit comments

Comments
 (0)