18
18
19
19
package org .bson ;
20
20
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 ;
22
28
import java .util .regex .Pattern ;
23
29
24
30
/**
25
- * A simple implementation of <code>DBObject</code>.
31
+ * A simple implementation of <code>DBObject</code>.
26
32
* A <code>DBObject</code> can be created as follows, using this class:
27
33
* <blockquote><pre>
28
34
* DBObject obj = new BasicBSONObject();
32
38
public class BasicBSONObject extends LinkedHashMap <String ,Object > implements BSONObject {
33
39
34
40
private static final long serialVersionUID = -4415279469780082174L ;
35
-
41
+
36
42
/**
37
43
* Creates an empty object.
38
44
*/
@@ -56,7 +62,7 @@ public BasicBSONObject(String key, Object value){
56
62
* Creates a DBObject from a map.
57
63
* @param m map to convert
58
64
*/
59
- @ SuppressWarnings ("unchecked" )
65
+ @ SuppressWarnings ("unchecked" )
60
66
public BasicBSONObject (Map m ) {
61
67
super (m );
62
68
}
@@ -69,7 +75,7 @@ public Map toMap() {
69
75
return new LinkedHashMap <String ,Object >(this );
70
76
}
71
77
72
- /** Deletes a field from this object.
78
+ /** Deletes a field from this object.
73
79
* @param key the field name to remove
74
80
* @return the object removed
75
81
*/
@@ -109,7 +115,7 @@ public int getInt( String key ){
109
115
Object o = get (key );
110
116
if ( o == null )
111
117
throw new NullPointerException ( "no value for: " + key );
112
-
118
+
113
119
return BSON .toInt ( o );
114
120
}
115
121
@@ -122,15 +128,15 @@ public int getInt( String key , int def ){
122
128
Object foo = get ( key );
123
129
if ( foo == null )
124
130
return def ;
125
-
131
+
126
132
return BSON .toInt ( foo );
127
133
}
128
134
129
135
/**
130
136
* Returns the value of a field as a <code>long</code>.
131
137
*
132
138
* @param key the field to return
133
- * @return the field value
139
+ * @return the field value
134
140
*/
135
141
public long getLong ( String key ){
136
142
Object foo = get ( key );
@@ -141,13 +147,13 @@ public long getLong( String key){
141
147
* Returns the value of a field as a <code>double</code>.
142
148
*
143
149
* @param key the field to return
144
- * @return the field value
150
+ * @return the field value
145
151
*/
146
152
public double getDouble ( String key ){
147
153
Object foo = get ( key );
148
154
return ((Number )foo ).doubleValue ();
149
155
}
150
-
156
+
151
157
/** Returns the value of a field as a string
152
158
* @param key the field to look up
153
159
* @return the value of the field, converted to a string
@@ -183,6 +189,16 @@ public boolean getBoolean( String key , boolean def ){
183
189
throw new IllegalArgumentException ( "can't coerce to bool:" + foo .getClass () );
184
190
}
185
191
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
+
186
202
/** Add a key/value pair to this object
187
203
* @param key the field name
188
204
* @param val the field value
@@ -192,18 +208,18 @@ public Object put( String key , Object val ){
192
208
return super .put ( key , val );
193
209
}
194
210
195
- @ SuppressWarnings ("unchecked" )
211
+ @ SuppressWarnings ("unchecked" )
196
212
public void putAll ( Map m ){
197
213
for ( Map .Entry entry : (Set <Map .Entry >)m .entrySet () ){
198
214
put ( entry .getKey ().toString () , entry .getValue () );
199
215
}
200
- }
201
-
216
+ }
217
+
202
218
public void putAll ( BSONObject o ){
203
219
for ( String k : o .keySet () ){
204
220
put ( k , o .get ( k ) );
205
221
}
206
- }
222
+ }
207
223
208
224
/** Add a key/value pair to this object
209
225
* @param key the field name
@@ -218,23 +234,23 @@ public BasicBSONObject append( String key , Object val ){
218
234
219
235
/** Returns a JSON serialization of this object
220
236
* @return JSON serialization
221
- */
237
+ */
222
238
public String toString (){
223
239
return com .mongodb .util .JSON .serialize ( this );
224
240
}
225
241
226
242
public boolean equals ( Object o ){
227
243
if ( ! ( o instanceof BSONObject ) )
228
244
return false ;
229
-
245
+
230
246
BSONObject other = (BSONObject )o ;
231
247
if ( ! keySet ().equals ( other .keySet () ) )
232
248
return false ;
233
249
234
250
for ( String key : keySet () ){
235
251
Object a = get ( key );
236
252
Object b = other .get ( key );
237
-
253
+
238
254
if ( a == null ){
239
255
if ( b != null )
240
256
return false ;
@@ -244,7 +260,7 @@ public boolean equals( Object o ){
244
260
return false ;
245
261
}
246
262
else if ( a instanceof Number && b instanceof Number ){
247
- if ( ((Number )a ).doubleValue () !=
263
+ if ( ((Number )a ).doubleValue () !=
248
264
((Number )b ).doubleValue () )
249
265
return false ;
250
266
}
0 commit comments