Make ChildTrackingPolicy an abstract class 75/26875/4
authorRobert Varga <rovarga@cisco.com>
Fri, 11 Sep 2015 20:37:19 +0000 (22:37 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 14 Sep 2015 07:48:32 +0000 (07:48 +0000)
Rather than switching on an enum to instantiate the map for children,
make it an interface contract. While the functionality will remain the
same, this will allow allocation of the map based on information
available from the SchemaNode.

Change-Id: Ibeee8c6c991fb592f5e1af02c86ebbfe8ef85bf2
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ChildTrackingPolicy.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModifiedNode.java

index 52bffc73cfe87f0a7ece97d61bb0cd8bc5bb4316..a8ad75de7be3222afe9f040bbfa8588b42d006a4 100644 (file)
@@ -7,21 +7,51 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
 /**
  * Child ordering policy. It defines how a {@link ModifiedNode} tracks its children.
  */
-enum ChildTrackingPolicy {
+abstract class ChildTrackingPolicy {
+    private static final int DEFAULT_CHILD_COUNT = 8;
+
     /**
      * No child nodes are possible, ever.
      */
-    NONE,
+    static final ChildTrackingPolicy NONE = new ChildTrackingPolicy() {
+        @Override
+        Map<PathArgument, ModifiedNode> createMap() {
+            return Collections.emptyMap();
+        }
+    };
     /**
      * Child nodes are possible and we need to make sure that their iteration order
      * matches the order in which they are introduced.
      */
-    ORDERED,
+    static final ChildTrackingPolicy ORDERED = new ChildTrackingPolicy() {
+        @Override
+        Map<PathArgument, ModifiedNode> createMap() {
+            return new LinkedHashMap<>(DEFAULT_CHILD_COUNT);
+        }
+    };
     /**
      * Child nodes are possible, but their iteration order can be undefined.
      */
-    UNORDERED,
+    static final ChildTrackingPolicy UNORDERED = new ChildTrackingPolicy() {
+        @Override
+        Map<PathArgument, ModifiedNode> createMap() {
+            return new HashMap<>();
+        }
+    };
+
+    /**
+     * Instantiate a new map for all possible children.
+     *
+     * @return An empty map instance
+     */
+    abstract Map<PathArgument, ModifiedNode> createMap();
 }
index c42e5dd49c55b36ea9bed71fb0d1cf15a08c1daf..48a3c4841d87e80032951aa4cd46a0482615879d 100644 (file)
@@ -11,9 +11,6 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
 import java.util.Map;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.NotThreadSafe;
@@ -51,7 +48,6 @@ final class ModifiedNode extends NodeModification implements StoreTreeNode<Modif
             throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getOperation()));
         }
     };
-    private static final int DEFAULT_CHILD_COUNT = 8;
 
     private final Map<PathArgument, ModifiedNode> children;
     private final Optional<TreeNode> original;
@@ -64,20 +60,7 @@ final class ModifiedNode extends NodeModification implements StoreTreeNode<Modif
     private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original, final ChildTrackingPolicy childPolicy) {
         this.identifier = identifier;
         this.original = original;
-
-        switch (childPolicy) {
-        case NONE:
-            children = Collections.emptyMap();
-            break;
-        case ORDERED:
-            children = new LinkedHashMap<>(DEFAULT_CHILD_COUNT);
-            break;
-        case UNORDERED:
-            children = new HashMap<>(DEFAULT_CHILD_COUNT);
-            break;
-        default:
-            throw new IllegalArgumentException("Unsupported child tracking policy " + childPolicy);
-        }
+        this.children = childPolicy.createMap();
     }
 
     /**