Take advantage of MapNode.size() 85/89585/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 4 May 2020 11:17:34 +0000 (13:17 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 4 May 2020 13:02:02 +0000 (15:02 +0200)
Checking the size allows us to skip instantiating values, plus we
can pass it down to map construction, where a correct strategy
can be taken based on the size.

JIRA: MDSAL-539
Change-Id: Ib42e290571cf162150b6e94b24df4aca5a9762ff
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/KeyedListNodeCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ListNodeCodecContext.java

index dd44e6896ae2f33203b015c27f15ac576312964e..5d8445e27935a889c22ff9cf075a0ce6cd5ec683 100644 (file)
@@ -17,7 +17,6 @@ import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.lang.invoke.WrongMethodTypeException;
 import java.lang.reflect.Method;
-import java.util.Collection;
 import java.util.List;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.binding.DataObject;
@@ -56,10 +55,10 @@ abstract class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>>
         }
 
         @Override
-        Object fromMap(final MapNode map, final Collection<MapEntryNode> value) {
-            // FIXME: Could be this lazy transformed map?
-            final Builder<Object, D> builder = ImmutableMap.builderWithExpectedSize(value.size());
-            for (MapEntryNode node : value) {
+        Object fromMap(final MapNode map, final int size) {
+            // FIXME: MDSAL-539: Make this a lazily-populated map
+            final Builder<Object, D> builder = ImmutableMap.builderWithExpectedSize(size);
+            for (MapEntryNode node : map.getValue()) {
                 final D entry = fromMapEntry(node);
                 builder.put(getKey(entry), entry);
             }
index 1e98817947ab75aca2b1c53db8dd96240c7ec606..2577b615f15d7233cab31bb30178860b841ca9fa 100644 (file)
@@ -58,15 +58,15 @@ class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<
     }
 
     private Object fromMap(final MapNode map) {
-        final Collection<MapEntryNode> value = map.getValue();
+        final int size;
         // This should never happen, but we do need to ensure users never see an empty Map
-        return value.isEmpty() ? null : fromMap(map, value);
+        return (size = map.size()) == 0 ? null : fromMap(map, size);
     }
 
-    Object fromMap(final MapNode map, final Collection<MapEntryNode> value) {
-        // FIXME: Could be this lazy transformed list?
-        final Builder<D> builder = ImmutableList.builderWithExpectedSize(value.size());
-        for (MapEntryNode node : value) {
+    @NonNull Object fromMap(final MapNode map, final int size) {
+        // FIXME: Make this a lazily-populated list
+        final Builder<D> builder = ImmutableList.builderWithExpectedSize(size);
+        for (MapEntryNode node : map.getValue()) {
             builder.add(createBindingProxy(node));
         }
         return builder.build();