Refactor AbstractEffectiveStatementInference 64/95164/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 14 Feb 2021 12:59:10 +0000 (13:59 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 14 Feb 2021 13:04:29 +0000 (14:04 +0100)
SchemaInferenceStack.Inference is incuring reordered copying just
because it is forced by AbstractEffectiveStatementInference. Add
a level of indirection so that Inference can do its own thing for
substatement list.

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

index 8dd25e29e01e5f545e97e8c152276b16cc503a09..53be93a8a0681acee56c986a0f0ff27ff1003275 100644 (file)
@@ -19,35 +19,49 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 
 /**
- * A simple capture of an {@link EffectiveModelContext} and a list of {@link EffectiveStatement}s. No further guarantees
- * are made.
+ * A simple capture of an {@link AbstractEffectiveModelContextProvider} and {@link EffectiveStatementInference}s.
  *
  * @param T constituent {@link EffectiveStatement} type
  */
 @Beta
 public abstract class AbstractEffectiveStatementInference<T extends EffectiveStatement<?, ?>>
         extends AbstractEffectiveModelContextProvider implements EffectiveStatementInference {
-    private final @NonNull List<T> path;
-
-    protected AbstractEffectiveStatementInference(final @NonNull EffectiveModelContext modelContext,
-            final @NonNull ImmutableList<T> path) {
+    protected AbstractEffectiveStatementInference(final @NonNull EffectiveModelContext modelContext) {
         super(modelContext);
-        this.path = requireNonNull(path);
-    }
-
-    protected AbstractEffectiveStatementInference(final @NonNull EffectiveModelContext modelContext,
-            final @NonNull List<? extends T> path) {
-        super(modelContext);
-        this.path = ImmutableList.copyOf(path);
     }
 
     @Override
-    public final List<T> statementPath() {
-        return path;
-    }
+    public abstract List<T> statementPath();
 
-    @Override
-    protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
-        return super.addToStringAttributes(helper).add("statements", path);
+    /**
+     * A simple capture of an {@link AbstractEffectiveStatementInference} and a list of {@link EffectiveStatement}s. No
+     * further guarantees are made.
+     *
+     * @param T constituent {@link EffectiveStatement} type
+     */
+    @Beta
+    public abstract static class WithPath<T extends EffectiveStatement<?, ?>>
+            extends AbstractEffectiveStatementInference<T> {
+        private final @NonNull List<T> path;
+
+        protected WithPath(final @NonNull EffectiveModelContext modelContext, final @NonNull ImmutableList<T> path) {
+            super(modelContext);
+            this.path = requireNonNull(path);
+        }
+
+        protected WithPath(final @NonNull EffectiveModelContext modelContext, final @NonNull List<? extends T> path) {
+            super(modelContext);
+            this.path = ImmutableList.copyOf(path);
+        }
+
+        @Override
+        public final List<T> statementPath() {
+            return path;
+        }
+
+        @Override
+        protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+            return super.addToStringAttributes(helper).add("path", path);
+        }
     }
 }
index 9917c3f0d19fbea26725efce4b94d7f99b8c19de..94003e15033603c7b686322e4c89f0aa92972d52 100644 (file)
@@ -21,6 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 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.WithPath;
 
 /**
  * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its
@@ -28,8 +29,8 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStateme
  */
 @Beta
 @NonNullByDefault
-public final class DefaultSchemaTreeInference
-        extends AbstractEffectiveStatementInference<SchemaTreeEffectiveStatement<?>> implements SchemaTreeInference {
+public final class DefaultSchemaTreeInference extends WithPath<SchemaTreeEffectiveStatement<?>>
+        implements SchemaTreeInference {
     private DefaultSchemaTreeInference(final EffectiveModelContext modelContext,
             final ImmutableList<SchemaTreeEffectiveStatement<?>> path) {
         super(modelContext, path);
index 708efcc914caf5b9e3c7578135e1fdc7145e2b8d..7bae59a880fa947f8a64b33d9ca9e6412bc4393e 100644 (file)
@@ -64,29 +64,31 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
      */
     @Beta
     public static final class Inference extends AbstractEffectiveStatementInference<EffectiveStatement<?, ?>> {
+        private final ArrayDeque<EffectiveStatement<?, ?>> deque;
         private final ModuleEffectiveStatement currentModule;
         private final int groupingDepth;
         private final boolean clean;
 
-        Inference(final @NonNull EffectiveModelContext modelContext,
-                final Iterator<? extends EffectiveStatement<?, ?>> path,
+        Inference(final @NonNull EffectiveModelContext modelContext, final ArrayDeque<EffectiveStatement<?, ?>> deque,
                 final ModuleEffectiveStatement currentModule, final int groupingDepth, final boolean clean) {
-            super(modelContext, ImmutableList.copyOf(path));
+            super(modelContext);
+            this.deque = requireNonNull(deque);
             this.currentModule = currentModule;
             this.groupingDepth = groupingDepth;
             this.clean = clean;
         }
 
+        @Override
+        public List<EffectiveStatement<?, ?>> statementPath() {
+            return ImmutableList.copyOf(deque.descendingIterator());
+        }
+
         /**
          * Convert this inference into a {@link SchemaInferenceStack}.
          *
          * @return A new stack
          */
         public @NonNull SchemaInferenceStack toSchemaInferenceStack() {
-            final List<EffectiveStatement<?, ?>> path = statementPath();
-            final ArrayDeque<EffectiveStatement<?, ?>> deque = new ArrayDeque<>(path.size());
-            path.forEach(deque::push);
-
             return new SchemaInferenceStack(getEffectiveModelContext(), deque, currentModule, groupingDepth, clean);
         }
     }
@@ -119,7 +121,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
             final ArrayDeque<EffectiveStatement<?, ?>> deque, final ModuleEffectiveStatement currentModule,
             final int groupingDepth, final boolean clean) {
         this.effectiveModel = requireNonNull(effectiveModel);
-        this.deque = requireNonNull(deque);
+        this.deque = deque.clone();
         this.currentModule = currentModule;
         this.groupingDepth = groupingDepth;
         this.clean = clean;
@@ -393,7 +395,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
      * @return An {@link Inference}
      */
     public @NonNull Inference toInference() {
-        return new Inference(effectiveModel, deque.descendingIterator(), currentModule, groupingDepth, clean);
+        return new Inference(effectiveModel, deque.clone(), currentModule, groupingDepth, clean);
     }
 
     /**