BUG-865: remove JSONCodec.needQuotes()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / UnmodifiableChildrenMap.java
index 49887cc329eece51ab471bd213936e1ec9dca70a..2551efb569fb909336040ef9cd307cc01f28130d 100644 (file)
@@ -12,20 +12,23 @@ import com.google.common.collect.ImmutableMap;
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Internal equivalent of {@link Collections}' unmodifiable Map. It does not retain
  * keySet/entrySet references, thus lowering the memory overhead.
  */
-final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
-    private static final Logger LOG = LoggerFactory.getLogger(UnmodifiableChildrenMap.class);
+final class UnmodifiableChildrenMap implements CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
     private static final long serialVersionUID = 1L;
+    /*
+     * Do not wrap maps which are smaller than this and instead copy them into
+     * an ImmutableMap.
+     */
+    private static final int WRAP_THRESHOLD = 9;
     private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
     private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
 
@@ -48,7 +51,10 @@ final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerCh
             return map;
         }
         if (map.isEmpty()) {
-            return Collections.emptyMap();
+            return ImmutableMap.of();
+        }
+        if (map.size() < WRAP_THRESHOLD) {
+            return ImmutableMap.copyOf(map);
         }
 
         return new UnmodifiableChildrenMap(map);
@@ -115,11 +121,16 @@ final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerCh
 
     @Override
     public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
-        LOG.warn("Invocation of inefficient entrySet()", new Throwable().fillInStackTrace());
+        /*
+         * Okay, this is not as efficient as it could be -- we could save ourselves the
+         * map instantiation. The cost of that would be re-implementation of a read-only
+         * Map.Entry to ensure our delegate is never modified.
+         *
+         * Let's skip that and use whatever the JRE gives us instead.
+         */
         return Collections.unmodifiableMap(delegate).entrySet();
     }
 
-
     @Override
     public boolean equals(final Object o) {
         return this == o || delegate.equals(o);
@@ -134,4 +145,14 @@ final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerCh
     public String toString() {
         return delegate.toString();
     }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> createMutableClone() {
+        if (delegate instanceof HashMap) {
+            return (Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>) ((HashMap<?, ?>) delegate).clone();
+        }
+
+        return new HashMap<>(delegate);
+    }
 }