From 729fc494e5305d362eea95b01c381f2a7daf2652 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Fri, 18 Dec 2009 15:51:51 -0500 Subject: [PATCH] use LinkedHashMap for BasicDBObject since it maintains insertion order JAVA-65 --- src/main/com/mongodb/BasicDBObject.java | 30 +++++-------------------- src/test/com/mongodb/DBObjectTest.java | 19 ++++++++++++++++ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/com/mongodb/BasicDBObject.java b/src/main/com/mongodb/BasicDBObject.java index 342515c036f..fc41c7189bf 100644 --- a/src/main/com/mongodb/BasicDBObject.java +++ b/src/main/com/mongodb/BasicDBObject.java @@ -30,7 +30,7 @@ * obj.put( "foo", "bar" ); * */ -public class BasicDBObject extends HashMap implements DBObject { +public class BasicDBObject extends LinkedHashMap implements DBObject { /** * Creates an empty object. @@ -52,11 +52,7 @@ public BasicDBObject(String key, Object value){ * @param m map to convert */ public BasicDBObject(Map m) { - Iterator i = m.entrySet().iterator(); - while (i.hasNext()) { - Map.Entry entry = i.next(); - put(entry.getKey().toString(), entry.getValue()); - } + super(m); } /** @@ -64,9 +60,7 @@ public BasicDBObject(Map m) { * @return the DBObject */ public Map toMap() { - Map m = new HashMap(); - m.putAll((HashMap)this); - return m; + return new LinkedHashMap(this); } /** Deletes a field from this object. @@ -74,7 +68,6 @@ public Map toMap() { * @return the object removed */ public Object removeField( String key ){ - _keys.remove(key); return remove( key ); } @@ -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 ){ @@ -157,7 +150,6 @@ public String getString( String key ){ * @return the val parameter */ public Object put( String key , Object val ){ - _keys.add( key ); return super.put( key , val ); } @@ -179,20 +171,11 @@ public void putAll( DBObject o ){ * @return the val 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 keySet(){ - assert( _keys.size() == size() ); - return _keys; - } - /** Returns a JSON serialization of this object * @return JSON serialization */ @@ -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 ); @@ -231,6 +214,5 @@ public boolean equals( Object o ){ return true; } - private final Set _keys = new OrderedSet(); private boolean _isPartialObject = false; } diff --git a/src/test/com/mongodb/DBObjectTest.java b/src/test/com/mongodb/DBObjectTest.java index 5b4a47ceb5a..56a2c6f67ce 100644 --- a/src/test/com/mongodb/DBObjectTest.java +++ b/src/test/com/mongodb/DBObjectTest.java @@ -19,6 +19,7 @@ import java.net.*; import java.util.*; + import org.testng.annotations.Test; import com.mongodb.util.*; @@ -153,6 +154,24 @@ public void testInnerDot() { assertTrue(thrown); } + @Test(groups = {"basic"}) + public void testEntrySetOrder() { + final List expectedKeys = new ArrayList(); + 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 keysFromKeySet = new ArrayList(o.keySet()); + final List keysFromEntrySet = new ArrayList(); + for (final Map.Entry entry : o.entrySet()) { + keysFromEntrySet.add(entry.getKey()); + } + assertEquals(keysFromKeySet, expectedKeys); + assertEquals(keysFromEntrySet, expectedKeys); + } + private DB _db; public static void main( String args[] ) {