BUG 1131: Introduced sealing of builder, initial clean up of ModuleBuilder.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / util / AbstractBuilder.java
index 35b869be6bf9f9f13827a8db6b8e067f1a798a97..5063b7f49a1a05888df56c19a00a71d7941ddde8 100644 (file)
@@ -27,6 +27,7 @@ public abstract class AbstractBuilder implements Builder {
 
     protected final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
     protected final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
+    private boolean sealed;
 
     protected AbstractBuilder(final String moduleName, final int line) {
         this.moduleName = Preconditions.checkNotNull(moduleName,"moduleName must not be null");
@@ -56,6 +57,7 @@ public abstract class AbstractBuilder implements Builder {
 
     @Override
     public void setParent(final Builder parentBuilder) {
+        checkNotSealed();
         this.parentBuilder = parentBuilder;
     }
 
@@ -69,4 +71,17 @@ public abstract class AbstractBuilder implements Builder {
         addedUnknownNodes.add(unknownNode);
     }
 
+    protected void seal() {
+        checkNotSealed();
+        sealed  = true;
+    }
+
+    protected final void checkNotSealed() {
+        Preconditions.checkState(!sealed, "Builder is sealed. No further modifications allowed");
+    }
+
+    protected boolean isSealed() {
+        return sealed;
+    }
+
 }