Skip to content

Commit c927288

Browse files
committed
DBRef : add a constructor that does't require to know about
a parent object, a field name and a database (!). Not sure if the resultant object is useful in general, but is certainly serializable. Also some reformatting of the code
1 parent 072ae41 commit c927288

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

src/main/com/mongodb/DBRef.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22

33
package com.mongodb;
44

5-
import java.util.*;
6-
75
public class DBRef {
86

97
static final boolean D = Boolean.getBoolean( "DEBUG.DBREF" );
108

9+
/**
10+
* CTOR used for testing BSON encoding. Otherwise
11+
* non-functional due to a DBRef needing a parent db object,
12+
* a fieldName and a db
13+
*
14+
* @param ns namespace to point to
15+
* @param id value of _id
16+
*/
17+
public DBRef(String ns, ObjectId id) {
18+
this (null, null, null, ns, id);
19+
}
20+
1121
DBRef( DBObject parent , String fieldName , DBBase db , String ns , ObjectId id ){
1222

1323
_parent = parent;
@@ -19,25 +29,25 @@ public class DBRef {
1929
_id = id;
2030
}
2131

22-
private DBObject fetch(){
23-
if ( _loadedPointedTo )
24-
return _pointedTo;
25-
26-
if ( _db == null )
27-
throw new RuntimeException( "no db" );
32+
private DBObject fetch() {
33+
if (_loadedPointedTo)
34+
return _pointedTo;
35+
36+
if (_db == null)
37+
throw new RuntimeException("no db");
2838

29-
if ( D ){
30-
System.out.println( "following dbref. parent.field:" + _fieldName + " ref to ns:" + _ns );
39+
if (D) {
40+
System.out.println("following dbref. parent.field:" + _fieldName + " ref to ns:" + _ns);
3141
Throwable t = new Throwable();
3242
t.fillInStackTrace();
3343
t.printStackTrace();
3444
}
35-
36-
final DBCollection coll = _db.getCollectionFromString( _ns );
37-
38-
_pointedTo = coll.find( _id );
39-
_loadedPointedTo = true;
40-
return _pointedTo;
45+
46+
final DBCollection coll = _db.getCollectionFromString(_ns);
47+
48+
_pointedTo = coll.find(_id);
49+
_loadedPointedTo = true;
50+
return _pointedTo;
4151
}
4252

4353
final DBObject _parent;
@@ -49,5 +59,4 @@ private DBObject fetch(){
4959

5060
private boolean _loadedPointedTo = false;
5161
private DBObject _pointedTo;
52-
5362
}

0 commit comments

Comments
 (0)