Add SchemaTreeInference
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaInferenceStack.java
index 1ca82186d905ab3a3ab7c5220a30c64c409d47a4..bbf05d54fe5e1d27ba406be15ba9124a25729fe6 100644 (file)
@@ -31,6 +31,7 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
@@ -42,6 +43,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absol
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveStatementInference;
+import org.opendaylight.yangtools.yang.model.spi.DefaultSchemaTreeInference;
 
 /**
  * A state tracking utility for walking {@link EffectiveModelContext}'s contents along schema/grouping namespaces. This
@@ -61,7 +63,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
      * {@link #statementPath()} is implementation-specific.
      */
     @Beta
-    public static final class Inference extends AbstractEffectiveStatementInference {
+    public static final class Inference extends AbstractEffectiveStatementInference<EffectiveStatement<?, ?>> {
         private final ModuleEffectiveStatement currentModule;
         private final int groupingDepth;
         private final boolean clean;
@@ -161,9 +163,25 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
     public static @NonNull SchemaInferenceStack ofInference(final EffectiveStatementInference inference) {
         if (inference.statementPath().isEmpty()) {
             return new SchemaInferenceStack(inference.getEffectiveModelContext());
+        } else if (inference instanceof SchemaTreeInference) {
+            return ofInference((SchemaTreeInference) inference);
+        } else if (inference instanceof Inference) {
+            return ((Inference) inference).toSchemaInferenceStack();
+        } else {
+            throw new IllegalArgumentException("Unsupported Inference " + inference);
         }
-        checkArgument(inference instanceof Inference, "Inference %s not supported", inference);
-        return ((Inference) inference).toSchemaInferenceStack();
+    }
+
+    /**
+     * Create a new stack from an {@link SchemaTreeInference}.
+     *
+     * @param inference SchemaTreeInference to use for initialization
+     * @return A new stack
+     * @throws NullPointerException if {@code inference} is null
+     * @throws IllegalArgumentException if {@code inference} cannot be resolved to a valid stack
+     */
+    public static @NonNull SchemaInferenceStack ofInference(final SchemaTreeInference inference) {
+        return of(inference.getEffectiveModelContext(), inference.toSchemaNodeIdentifier());
     }
 
     /**
@@ -356,6 +374,16 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
         return new Inference(effectiveModel, deque.descendingIterator(), currentModule, groupingDepth, clean);
     }
 
+    /**
+     * Return an {@link SchemaTreeInference} equivalent of current state.
+     *
+     * @return An {@link SchemaTreeInference}
+     * @throws IllegalStateException if current state cannot be converted to a {@link SchemaTreeInference}
+     */
+    public @NonNull SchemaTreeInference toSchemaTreeInference() {
+        return DefaultSchemaTreeInference.of(getEffectiveModelContext(), toSchemaNodeIdentifier());
+    }
+
     /**
      * Convert current state into an absolute schema node identifier.
      *