Split up tryToCompletePhase()
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index 12e812e24ffb256f2bf265d5c8eb59e7a024d086..3e39fd345fd02f1595c7fc75e0f15ee0df7ac32d 100644 (file)
@@ -522,37 +522,23 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     }
 
     /**
-     * tries to execute current {@link ModelProcessingPhase} of source parsing.
+     * Try to execute current {@link ModelProcessingPhase} of source parsing.
      *
-     * @param phase
-     *            to be executed (completed)
+     * @param phase to be executed (completed)
      * @return if phase was successfully completed
-     * @throws SourceException
-     *             when an error occurred in source parsing
+     * @throws SourceException when an error occurred in source parsing
      */
-    boolean tryToCompletePhase(final ModelProcessingPhase phase) {
-
-        boolean finished = true;
-        final Collection<ContextMutation> openMutations = phaseMutation.get(phase);
-        if (!openMutations.isEmpty()) {
-            final Iterator<ContextMutation> it = openMutations.iterator();
-            while (it.hasNext()) {
-                final ContextMutation current = it.next();
-                if (current.isFinished()) {
-                    it.remove();
-                } else {
-                    finished = false;
-                }
-            }
-
-            if (openMutations.isEmpty()) {
-                phaseMutation.removeAll(phase);
-                if (phaseMutation.isEmpty()) {
-                    phaseMutation = ImmutableMultimap.of();
-                }
-            }
+    final boolean tryToCompletePhase(final ModelProcessingPhase phase) {
+        final boolean finished = phaseMutation.isEmpty() ? true : runMutations(phase);
+        if (completeChildren(phase) && finished) {
+            onPhaseCompleted(phase);
+            return true;
         }
+        return false;
+    }
 
+    private boolean completeChildren(final ModelProcessingPhase phase) {
+        boolean finished = true;
         for (final StatementContextBase<?, ?, ?> child : mutableDeclaredSubstatements()) {
             finished &= child.tryToCompletePhase(phase);
         }
@@ -561,12 +547,33 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
                 finished &= ((StatementContextBase<?, ?, ?>) child).tryToCompletePhase(phase);
             }
         }
+        return finished;
+    }
 
-        if (finished) {
-            onPhaseCompleted(phase);
-            return true;
+    private boolean runMutations(final ModelProcessingPhase phase) {
+        final Collection<ContextMutation> openMutations = phaseMutation.get(phase);
+        return openMutations.isEmpty() ? true : runMutations(phase, openMutations);
+    }
+
+    private boolean runMutations(final ModelProcessingPhase phase, final Collection<ContextMutation> openMutations) {
+        boolean finished = true;
+        final Iterator<ContextMutation> it = openMutations.iterator();
+        while (it.hasNext()) {
+            final ContextMutation current = it.next();
+            if (current.isFinished()) {
+                it.remove();
+            } else {
+                finished = false;
+            }
         }
-        return false;
+
+        if (openMutations.isEmpty()) {
+            phaseMutation.removeAll(phase);
+            if (phaseMutation.isEmpty()) {
+                phaseMutation = ImmutableMultimap.of();
+            }
+        }
+        return finished;
     }
 
     /**