Skip to content

Commit cb9f0fb

Browse files
committed
push/pop in builder
1 parent bab66a1 commit cb9f0fb

4 files changed

Lines changed: 130 additions & 8 deletions

File tree

src/main/com/mongodb/BasicDBObject.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,31 @@ public void markAsPartialObject(){
206206
_isPartialObject = true;
207207
}
208208

209+
public boolean equals( Object o ){
210+
if ( ! ( o instanceof DBObject ) )
211+
return false;
212+
213+
DBObject other = (DBObject)o;
214+
if ( ! _keys.equals( other.keySet() ) )
215+
return false;
216+
217+
for ( String key : _keys ){
218+
Object a = get( key );
219+
Object b = other.get( key );
220+
221+
if ( a instanceof Number && b instanceof Number ){
222+
if ( ((Number)a).doubleValue() !=
223+
((Number)b).doubleValue() )
224+
return false;
225+
}
226+
else {
227+
if ( ! a.equals( b ) )
228+
return false;
229+
}
230+
}
231+
return true;
232+
}
233+
209234
private final Set<String> _keys = new OrderedSet<String>();
210235
private boolean _isPartialObject = false;
211236
}

src/main/com/mongodb/BasicDBObjectBuilder.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
package com.mongodb;
2020

21-
import java.util.Map;
22-
import java.util.Iterator;
23-
21+
import java.util.*;
2422

2523
/**
2624
* utility for building objects
@@ -29,6 +27,11 @@
2927
*/
3028
public class BasicDBObjectBuilder {
3129

30+
public BasicDBObjectBuilder(){
31+
_stack = new LinkedList<DBObject>();
32+
_stack.add( new BasicDBObject() );
33+
}
34+
3235
public static BasicDBObjectBuilder start(){
3336
return new BasicDBObjectBuilder();
3437
}
@@ -56,23 +59,41 @@ public static BasicDBObjectBuilder start(Map m){
5659
* @return returns itself so you can chain .append( "a" , 1 ).add( "b" , 1 )
5760
*/
5861
public BasicDBObjectBuilder append( String key , Object val ){
59-
_it.put( key , val );
62+
_cur().put( key , val );
6063
return this;
6164
}
62-
65+
6366

6467
/**
6568
* @return returns itself so you can chain .add( "a" , 1 ).add( "b" , 1 )
6669
*/
6770
public BasicDBObjectBuilder add( String key , Object val ){
68-
_it.put( key , val );
71+
_cur().put( key , val );
72+
return this;
73+
}
74+
75+
public BasicDBObjectBuilder push( String key ){
76+
BasicDBObject o = new BasicDBObject();
77+
_cur().put( key , o );
78+
_stack.addLast( o );
79+
return this;
80+
}
81+
82+
public BasicDBObjectBuilder pop(){
83+
if ( _stack.size() <= 1 )
84+
throw new IllegalArgumentException( "can't pop last element" );
85+
_stack.removeLast();
6986
return this;
7087
}
7188

7289
public DBObject get(){
73-
return _it;
90+
return _stack.getFirst();
91+
}
92+
93+
private DBObject _cur(){
94+
return _stack.getLast();
7495
}
7596

76-
DBObject _it = new BasicDBObject();
97+
private final LinkedList<DBObject> _stack;
7798

7899
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// BasicDBObjectTest.java
2+
3+
/**
4+
* Copyright (C) 2008 10gen Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.mongodb;
20+
21+
import java.util.*;
22+
import java.util.regex.*;
23+
import java.io.IOException;
24+
25+
import org.testng.annotations.Test;
26+
27+
import com.mongodb.util.*;
28+
29+
public class BasicDBObjectTest extends TestCase {
30+
31+
@Test(groups = {"basic"})
32+
public void testBasic(){
33+
BasicDBObject a = new BasicDBObject( "x" , 1 );
34+
BasicDBObject b = new BasicDBObject( "x" , 1 );
35+
assert( a.equals( b ) );
36+
37+
DBObject x = JSON.parse( "{ 'x' : 1 }" );
38+
assert( a.equals( x ) );
39+
}
40+
41+
42+
@Test(groups = {"basic"})
43+
public void testBasic2(){
44+
BasicDBObject a = new BasicDBObject( "x" , 1 );
45+
DBObject b = BasicDBObjectBuilder.start().append( "x" , 1 ).get();
46+
assert( a.equals( b ) );
47+
assert( a.equals( JSON.parse( "{ 'x' : 1 }" ) ) );
48+
assert( ! a.equals( JSON.parse( "{ 'x' : 2 }" ) ) );
49+
}
50+
51+
@Test(groups = {"basic"})
52+
public void testDown1(){
53+
BasicDBObjectBuilder b = BasicDBObjectBuilder.start();
54+
b.append( "x" , 1 );
55+
b.push("y");
56+
b.append( "a" , 2 );
57+
b.pop();
58+
b.push( "z" );
59+
b.append( "b" , 3 );
60+
61+
62+
DBObject x = b.get();
63+
DBObject y = JSON.parse( "{ 'x' : 1 , 'y' : { 'a' : 2 } , 'z' : { 'b' : 3 } }" );
64+
65+
assert( x.equals( y ) );
66+
}
67+
68+
69+
public static void main( String args[] )
70+
throws Exception {
71+
(new BasicDBObjectTest()).runConsole();
72+
73+
}
74+
75+
}

testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<class name="com.mongodb.io.ByteBufferStreamTest"/>
1010

1111
<class name="com.mongodb.ByteTest" />
12+
<class name="com.mongodb.BasicDBObjectTest" />
1213
<class name="com.mongodb.JavaClientTest" />
1314
<class name="com.mongodb.ReflectionTest" />
1415
<class name="com.mongodb.DBAddressTest" />

0 commit comments

Comments
 (0)