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;
return map;
}
if (map.isEmpty()) {
- return Collections.emptyMap();
+ return ImmutableMap.of();
+ }
+ if (map.size() < WRAP_THRESHOLD) {
+ return ImmutableMap.copyOf(map);
}
return new UnmodifiableChildrenMap(map);
@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);
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);
+ }
}