Add SchemaInferenceStack utility factory methods 08/95108/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Feb 2021 10:07:39 +0000 (11:07 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Feb 2021 10:10:27 +0000 (11:10 +0100)
A number of users of yang.model.api use some form of addressing,
this is related either to SchemaNodeIdentifier or SchemaPath. These
users can benefit from SchemaInferenceStack -- but would have to
duplicate code to correctly initialize it.

Add two factory methods to create a new SchemaInferenceStack.

JIRA: YANGTOOLS-1086
Change-Id: I3006e5d74d239eb6bc96e148521148c579fb5811
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java

index 561b9d463f79d25e1beca29b46e58a9253b7f525..e4019f92cae181d51cc309c7b5844786b857d0fa 100644 (file)
@@ -68,6 +68,38 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
         this.effectiveModel = requireNonNull(effectiveModel);
     }
 
+    /**
+     * Create a new stack backed by an effective model, pointing to specified schema node identified by
+     * {@link Absolute}.
+     *
+     * @param effectiveModel EffectiveModelContext to which this stack is attached
+     * @throws NullPointerException {@code effectiveModel} is null
+     * @throws IllegalArgumentException if {@code path} cannot be resolved in the effective model
+     */
+    public static @NonNull SchemaInferenceStack of(final EffectiveModelContext effectiveModel, final Absolute path) {
+        final SchemaInferenceStack ret = new SchemaInferenceStack(effectiveModel);
+        path.getNodeIdentifiers().forEach(ret::enterSchemaTree);
+        return ret;
+    }
+
+    /**
+     * Create a new stack backed by an effective model, pointing to specified schema node identified by an absolute
+     * {@link SchemaPath} and its {@link SchemaPath#getPathFromRoot()}.
+     *
+     * @param effectiveModel EffectiveModelContext to which this stack is attached
+     * @throws NullPointerException {@code effectiveModel} is null
+     * @throws IllegalArgumentException if {@code path} cannot be resolved in the effective model or if it is not an
+     *                                  absolute path.
+     */
+    // FIXME: 7.0.0: consider deprecating this method
+    public static @NonNull SchemaInferenceStack ofInstantiatedPath(final EffectiveModelContext effectiveModel,
+            final SchemaPath path) {
+        checkArgument(path.isAbsolute(), "Cannot operate on relative path %s", path);
+        final SchemaInferenceStack ret = new SchemaInferenceStack(effectiveModel);
+        path.getPathFromRoot().forEach(ret::enterSchemaTree);
+        return ret;
+    }
+
     @Override
     public EffectiveModelContext getEffectiveModelContext() {
         return effectiveModel;