Introduce MapAdaptor.initialSnapshot()
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / MapAdaptor.java
index bea7630330c3a0ae89af54d141931ebd9868b8f6..48241cd5a638322dec8013aa54e32bca9be3de6b 100644 (file)
@@ -8,6 +8,8 @@
 package org.opendaylight.yangtools.util;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 import com.romix.scala.collection.concurrent.TrieMap;
 import java.util.Collections;
 import java.util.HashMap;
@@ -85,6 +87,22 @@ public final class MapAdaptor {
         return new MapAdaptor(useSingleton, copyMaxItems, persistMinItems);
     }
 
+    /**
+     * Creates an initial snapshot. The backing map is selected according to
+     * the expected size.
+     *
+     * @param expectedSize Expected map size
+     * @return An empty mutable map.
+     */
+    public <K, V> Map<K, V> initialSnapshot(final int expectedSize) {
+        Preconditions.checkArgument(expectedSize >= 0);
+        if (expectedSize > persistMinItems) {
+            return new ReadWriteTrieMap<>();
+        }
+
+        return Maps.newHashMapWithExpectedSize(expectedSize);
+    }
+
     /**
      * Input is treated is supposed to be left unmodified, result must be mutable.
      *
@@ -124,7 +142,7 @@ public final class MapAdaptor {
          */
         if (size == 0) {
             LOG.trace("Reducing input {} to an empty map", input);
-            return Collections.emptyMap();
+            return ImmutableMap.of();
         }
 
         /*