Skip to content

Commit

Permalink
use LinkedHashMap for BasicDBObject since it maintains insertion order
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Dec 18, 2009
1 parent b736208 commit 729fc49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/main/com/mongodb/BasicDBObject.java
Expand Up @@ -30,7 +30,7 @@
* obj.put( "foo", "bar" );
* </pre></blockquote>
*/
public class BasicDBObject extends HashMap<String,Object> implements DBObject {
public class BasicDBObject extends LinkedHashMap<String,Object> implements DBObject {

/**
* Creates an empty object.
Expand All @@ -52,29 +52,22 @@ public BasicDBObject(String key, Object value){
* @param m map to convert
*/
public BasicDBObject(Map m) {
Iterator<Map.Entry> i = m.entrySet().iterator();
while (i.hasNext()) {
Map.Entry entry = i.next();
put(entry.getKey().toString(), entry.getValue());
}
super(m);
}

/**
* Converts a DBObject to a map.
* @return the DBObject
*/
public Map toMap() {
Map m = new HashMap();
m.putAll((HashMap)this);
return m;
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 ){
_keys.remove(key);
return remove( key );
}

Expand All @@ -86,7 +79,7 @@ public boolean isPartialObject(){
}

/** Checks if this object contains a given field
* @param field field name
* @param key field name
* @return if the field exists
*/
public boolean containsField( String field ){
Expand Down Expand Up @@ -157,7 +150,6 @@ public String getString( String key ){
* @return the <code>val</code> parameter
*/
public Object put( String key , Object val ){
_keys.add( key );
return super.put( key , val );
}

Expand All @@ -179,20 +171,11 @@ public void putAll( DBObject o ){
* @return the <code>val</code> parameter
*/
public BasicDBObject append( String key , Object val ){
_keys.add( key );
put( key , val );

return this;
}

/** Gets a set of this object's fieldnames
* @return the fieldnames
*/
public Set<String> keySet(){
assert( _keys.size() == size() );
return _keys;
}

/** Returns a JSON serialization of this object
* @return JSON serialization
*/
Expand All @@ -211,10 +194,10 @@ public boolean equals( Object o ){
return false;

DBObject other = (DBObject)o;
if ( ! _keys.equals( other.keySet() ) )
if ( ! keySet().equals( other.keySet() ) )
return false;

for ( String key : _keys ){
for ( String key : keySet() ){
Object a = get( key );
Object b = other.get( key );

Expand All @@ -231,6 +214,5 @@ public boolean equals( Object o ){
return true;
}

private final Set<String> _keys = new OrderedSet<String>();
private boolean _isPartialObject = false;
}
19 changes: 19 additions & 0 deletions src/test/com/mongodb/DBObjectTest.java
Expand Up @@ -19,6 +19,7 @@
import java.net.*;
import java.util.*;


import org.testng.annotations.Test;

import com.mongodb.util.*;
Expand Down Expand Up @@ -153,6 +154,24 @@ public void testInnerDot() {
assertTrue(thrown);
}

@Test(groups = {"basic"})
public void testEntrySetOrder() {
final List<String> expectedKeys = new ArrayList<String>();
final BasicDBObject o = new BasicDBObject();
for (int i = 1; i < 1000; i++) {
final String key = String.valueOf(i);
expectedKeys.add(key);
o.put(key, "Test" + key);
}
final List<String> keysFromKeySet = new ArrayList<String>(o.keySet());
final List<String> keysFromEntrySet = new ArrayList<String>();
for (final Map.Entry<String, Object> entry : o.entrySet()) {
keysFromEntrySet.add(entry.getKey());
}
assertEquals(keysFromKeySet, expectedKeys);
assertEquals(keysFromEntrySet, expectedKeys);
}

private DB _db;

public static void main( String args[] ) {
Expand Down

0 comments on commit 729fc49

Please sign in to comment.