/*
 * Copyright (c) 2008-2014 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb;

import org.bson.BSONObject;
import org.bson.BasicBSONCallback;
import org.bson.types.ObjectId;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * An implementation of DBCallback that decodes into a DBObject.
 */
public class DefaultDBCallback extends BasicBSONCallback implements DBCallback {

    private final DBObjectFactory objectFactory;

    /**
     * Creates a new DefaultDBCallback. If the Collection is null, it uses {@link DBCollectionObjectFactory} to create documents, otherwise
     * it uses the collection's object factory.
     *
     * @param collection an optionally null Collection that the documents created by this callback belong to.
     */
    public DefaultDBCallback(final DBCollection collection) {
        if (collection != null) {
            this.objectFactory = collection.getObjectFactory();
        } else {
            this.objectFactory = new DBCollectionObjectFactory();
        }
    }

    @Override
    public BSONObject create() {
        return objectFactory.getInstance();
    }

    @Override
    public BSONObject create(final boolean array, final List<String> path) {
        return array ? new BasicDBList() : objectFactory.getInstance(path != null ? path : Collections.<String>emptyList());
    }

    @Override
    public void gotDBRef(final String name, final String namespace, final ObjectId id) {
        _put(name, new DBRef(namespace, id));
    }

    @Override
    public Object objectDone() {
        String name = curName();
        BSONObject document = (BSONObject) super.objectDone();
        Iterator<String> iterator = document.keySet().iterator();
        if (iterator.hasNext() && iterator.next().equals("$ref") && iterator.hasNext() && iterator.next().equals("$id")) {
            _put(name, new DBRef((String) document.get("$ref"), document.get("$id")));
        }

        return document;
    }

    public static final DBCallbackFactory FACTORY = new DBCallbackFactory() {
        @Override
        public DBCallback create(final DBCollection collection) {
            return new DefaultDBCallback(collection);
        }
    };
}

@@ -683,16 +683,53 @@
 llback;%0A
+import org.bson.types.BasicBSONList;%0A
 import o
@@ -2236,24 +2236,80 @@
 jectDone();%0A
+        if (!(document instanceof BasicBSONList)) %7B%0A    
         Iter
@@ -2366,24 +2366,28 @@
 ();%0A        
+  
+  
 if (iterator
@@ -2493,32 +2493,36 @@
 ) %7B%0A            
+    
 _put(name, new D
@@ -2584,26 +2584,39 @@
 ));%0A        
+    %7D%0A        
 %7D
-%0A
 %0A        ret

Optimize DefaultDBCallback.objectDone by only looking to see if the BSONObject should be replaced by a DBRef if it's not an instance of BasicBSONList