*/
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();
}
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;
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;
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();
}
/**