Skip to content

Commit 80fe168

Browse files
author
kristina
committed
toMap()
1 parent 8e8966d commit 80fe168

6 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/main/com/mongodb/BasicDBList.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public Set<String> keySet(){
8080
return s;
8181
}
8282

83+
public Map toMap() {
84+
Map m = new HashMap();
85+
Iterator i = this.keySet().iterator();
86+
while (i.hasNext()) {
87+
Object s = i.next();
88+
m.put(s, this.get(s+""));
89+
}
90+
return m;
91+
}
92+
8393
int _getInt( String s ){
8494
try {
8595
return Integer.parseInt( s );

src/main/com/mongodb/BasicDBObject.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ public BasicDBObject(Map m) {
6161

6262
/**
6363
* Converts a DBObject to a map.
64-
* @param obj object to convert
6564
* @return the DBObject
6665
*/
67-
public static Map toMap(DBObject obj) {
66+
public Map toMap() {
6867
Map m = new HashMap();
69-
m.putAll((HashMap)obj);
68+
m.putAll((HashMap)this);
7069
return m;
7170
}
7271

src/main/com/mongodb/DBObject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public interface DBObject {
3636
*/
3737
public Object get( String key );
3838

39+
/**
40+
* Returns a map representing this DBObject.
41+
* @return the map
42+
*/
43+
public Map toMap();
44+
3945
/** Remove a field with a given name from this object.
4046
* @param key The name of the field to remove
4147
* @return The value removed from this object

src/main/com/mongodb/RawDBObject.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public Object get( String key ){
5252
return e.getObject();
5353
}
5454

55+
public Map toMap() {
56+
Map m = new HashMap();
57+
Iterator i = this.keySet().iterator();
58+
while (i.hasNext()) {
59+
Object s = i.next();
60+
m.put(s, this.get(s+""));
61+
}
62+
return m;
63+
}
5564

5665
public Object put( String key , Object v ){
5766
throw new RuntimeException( "read only" );

src/main/com/mongodb/ReflectionDBObject.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public boolean isPartialObject(){
5353
return false;
5454
}
5555

56+
public Map toMap() {
57+
Map m = new HashMap();
58+
Iterator i = this.keySet().iterator();
59+
while (i.hasNext()) {
60+
Object s = i.next();
61+
m.put(s, this.get(s+""));
62+
}
63+
return m;
64+
}
65+
5666
public void markAsPartialObject(){
5767
throw new RuntimeException( "ReflectionDBObjects can't be partial" );
5868
}

src/test/com/mongodb/DBObjectTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public void testDBObjectBuilder() {
5252

5353
}
5454

55+
@Test(groups = {"basic"})
56+
public void testToMap() {
57+
Map m = BasicDBObjectBuilder.start().add("y", "z").add("z","a").get().toMap();
58+
assertEquals(m.get("y"), "z");
59+
assertEquals(m.get("z"), "a");
60+
}
61+
5562
public static void main( String args[] ) {
5663
(new DBObjectTest()).runConsole();
5764
}

0 commit comments

Comments
 (0)