forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicBSONObject.java
More file actions
401 lines (347 loc) · 11.4 KB
/
BasicBSONObject.java
File metadata and controls
401 lines (347 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// BasicBSONObject.java
package org.bson;
// BSON
import com.mongodb.util.JSONSerializers;
import org.bson.types.BasicBSONList;
import org.bson.types.ObjectId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
// Java
/**
* A simple implementation of <code>DBObject</code>.
* A <code>DBObject</code> can be created as follows, using this class:
* <blockquote><pre>
* DBObject obj = new BasicBSONObject();
* obj.put( "foo", "bar" );
* </pre></blockquote>
*/
public class BasicBSONObject extends LinkedHashMap<String,Object> implements BSONObject {
private static final long serialVersionUID = -4415279469780082174L;
/**
* Creates an empty object.
*/
public BasicBSONObject(){
}
public BasicBSONObject(int size){
super(size);
}
/**
* Convenience CTOR
* @param key key under which to store
* @param value value to stor
*/
public BasicBSONObject(String key, Object value){
put(key, value);
}
/**
* Creates a DBObject from a map.
* @param m map to convert
*/
@SuppressWarnings("unchecked")
public BasicBSONObject(Map m) {
super(m);
}
/**
* Converts a DBObject to a map.
* @return the DBObject
*/
public Map toMap() {
return new LinkedHashMap<String,Object>(this);
}
/** Deletes a field from this object.
* @param key the field name to remove
* @return the object removed
*/
public Object removeField( String key ){
return remove( key );
}
/** Checks if this object contains a given field
* @param field field name
* @return if the field exists
*/
public boolean containsField( String field ){
return super.containsKey(field);
}
/**
* @deprecated
*/
@Deprecated
public boolean containsKey( String key ){
return containsField(key);
}
/** Gets a value from this object
* @param key field name
* @return the value
*/
public Object get( String key ){
return super.get(key);
}
/** Returns the value of a field as an <code>int</code>.
* @param key the field to look for
* @return the field value (or default)
*/
public int getInt( String key ){
Object o = get(key);
if ( o == null )
throw new NullPointerException( "no value for: " + key );
return BSON.toInt(o);
}
/** Returns the value of a field as an <code>int</code>.
* @param key the field to look for
* @param def the default to return
* @return the field value (or default)
*/
public int getInt( String key , int def ){
Object foo = get( key );
if ( foo == null )
return def;
return BSON.toInt(foo);
}
/**
* Returns the value of a field as a <code>long</code>.
*
* @param key the field to return
* @return the field value
*/
public long getLong( String key){
Object foo = get( key );
return ((Number)foo).longValue();
}
/**
* Returns the value of a field as an <code>long</code>.
* @param key the field to look for
* @param def the default to return
* @return the field value (or default)
*/
public long getLong( String key , long def ) {
Object foo = get( key );
if ( foo == null )
return def;
return ((Number)foo).longValue();
}
/**
* Returns the value of a field as a <code>double</code>.
*
* @param key the field to return
* @return the field value
*/
public double getDouble( String key){
Object foo = get( key );
return ((Number)foo).doubleValue();
}
/**
* Returns the value of a field as an <code>double</code>.
* @param key the field to look for
* @param def the default to return
* @return the field value (or default)
*/
public double getDouble( String key , double def ) {
Object foo = get( key );
if ( foo == null )
return def;
return ((Number)foo).doubleValue();
}
/** Returns the value of a field as a string
* @param key the field to look up
* @return the value of the field, converted to a string
*/
public String getString( String key ){
Object foo = get( key );
if ( foo == null )
return null;
return foo.toString();
}
/**
* Returns the value of a field as a string
* @param key the field to look up
* @param def the default to return
* @return the value of the field, converted to a string
*/
public String getString( String key, final String def ) {
Object foo = get( key );
if ( foo == null )
return def;
return foo.toString();
}
/** Returns the value of a field as a boolean.
* @param key the field to look up
* @return the value of the field, or false if field does not exist
*/
public boolean getBoolean( String key ){
return getBoolean(key, false);
}
/** Returns the value of a field as a boolean
* @param key the field to look up
* @param def the default value in case the field is not found
* @return the value of the field, converted to a string
*/
public boolean getBoolean( String key , boolean def ){
Object foo = get( key );
if ( foo == null )
return def;
if ( foo instanceof Number )
return ((Number)foo).intValue() > 0;
if ( foo instanceof Boolean )
return ((Boolean)foo).booleanValue();
throw new IllegalArgumentException( "can't coerce to bool:" + foo.getClass() );
}
/**
* Returns the object id or null if not set.
* @param field The field to return
* @return The field object value or null if not found (or if null :-^).
*/
public ObjectId getObjectId( final String field ) {
return (ObjectId) get( field );
}
/**
* Returns the object id or def if not set.
* @param field The field to return
* @param def the default value in case the field is not found
* @return The field object value or def if not set.
*/
public ObjectId getObjectId( final String field, final ObjectId def ) {
final Object foo = get( field );
return (foo != null) ? (ObjectId)foo : def;
}
/**
* Returns the date or null if not set.
* @param field The field to return
* @return The field object value or null if not found.
*/
public Date getDate( final String field ) {
return (Date) get( field );
}
/**
* Returns the date or def if not set.
* @param field The field to return
* @param def the default value in case the field is not found
* @return The field object value or def if not set.
*/
public Date getDate( final String field, final Date def ) {
final Object foo = get( field );
return (foo != null) ? (Date)foo : def;
}
/** Add a key/value pair to this object
* @param key the field name
* @param val the field value
* @return the <code>val</code> parameter
*/
public Object put( String key , Object val ){
return super.put( key , val );
}
@SuppressWarnings("unchecked")
public void putAll( Map m ){
for ( Map.Entry entry : (Set<Map.Entry>)m.entrySet() ){
put( entry.getKey().toString() , entry.getValue() );
}
}
public void putAll( BSONObject o ){
for ( String k : o.keySet() ){
put( k , o.get( k ) );
}
}
/** Add a key/value pair to this object
* @param key the field name
* @param val the field value
* @return <code>this</code>
*/
public BasicBSONObject append( String key , Object val ){
put( key , val );
return this;
}
/** Returns a JSON serialization of this object
* @return JSON serialization
*/
public String toString(){
return JSONSerializers.getStrict().serialize(this);
}
/**
* Compares two documents according to their serialized form, ignoring the order of keys.
*
* @param o the document to compare to, which must be an instance of {@link org.bson.BSONObject}.
* @return true if the documents have the same serialized form, ignoring key order.
*/
@Override
public boolean equals( Object o ) {
if (o == this) {
return true;
}
if (! (o instanceof BSONObject)) {
return false;
}
BSONObject other = (BSONObject) o;
if (!keySet().equals(other.keySet())) {
return false;
}
return Arrays.equals(canonicalizeBSONObject(this).encode(), canonicalizeBSONObject(other).encode());
}
@Override
public int hashCode() {
return Arrays.hashCode(canonicalizeBSONObject(this).encode());
}
private byte[] encode() {
return new BasicBSONEncoder().encode(this);
}
private BSONObject decode(final byte[] encodedBytes) {
return new BasicBSONDecoder().readObject(encodedBytes);
}
// create a copy of "from", but with keys ordered alphabetically
@SuppressWarnings("unchecked")
private static Object canonicalize(final Object from) {
if (from instanceof BSONObject && !(from instanceof BasicBSONList)) {
return canonicalizeBSONObject((BSONObject) from);
} else if (from instanceof List) {
return canonicalizeList((List<Object>) from);
} else if (from instanceof Map) {
return canonicalizeMap((Map<String, Object>) from);
} else {
return from;
}
}
private static Map<String, Object> canonicalizeMap(final Map<String, Object> from) {
Map<String, Object> canonicalized = new LinkedHashMap<String, Object>(from.size());
TreeSet<String> keysInOrder = new TreeSet<String>(from.keySet());
for (String key : keysInOrder) {
Object val = from.get(key);
canonicalized.put(key, canonicalize(val));
}
return canonicalized;
}
private static BasicBSONObject canonicalizeBSONObject(final BSONObject from) {
BasicBSONObject canonicalized = new BasicBSONObject();
TreeSet<String> keysInOrder = new TreeSet<String>(from.keySet());
for (String key : keysInOrder) {
Object val = from.get(key);
canonicalized.put(key, canonicalize(val));
}
return canonicalized;
}
private static List canonicalizeList(final List<Object> list) {
List<Object> canonicalized = new ArrayList<Object>(list.size());
for (Object cur : list) {
canonicalized.add(canonicalize(cur));
}
return canonicalized;
}
}