/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.yangtools.yang.parser.stmt.reactor;

import com.google.common.base.Preconditions;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;

abstract class ContextBuilder<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {

    private final StatementDefinitionContext<A, D, E> definition;
    private final StatementSourceReference stmtRef;
    private String rawArg;
    private StatementSourceReference argRef;

    public ContextBuilder(StatementDefinitionContext<A, D, E> def, StatementSourceReference sourceRef) {
        this.definition = def;
        this.stmtRef = sourceRef;
    }

    public void setArgument(@Nonnull String argument, @Nonnull StatementSourceReference argumentSource) {
        Preconditions.checkArgument(definition.hasArgument(), "Statement does not take argument.");
        this.rawArg = Preconditions.checkNotNull(argument);
        this.argRef = Preconditions.checkNotNull(argumentSource);
    }

    public String getRawArgument() {
        return rawArg;
    }

    public StatementSourceReference getStamementSource() {
        return stmtRef;
    }

    public StatementSourceReference getArgumentSource() {
        return argRef;
    }

    public StatementDefinitionContext<A, D, E> getDefinition() {
        return definition;
    }

    public StatementIdentifier createIdentifier() {
        return new StatementIdentifier(definition.getStatementName(), rawArg);
    }

    public abstract StatementContextBase<A, D, E> build() throws SourceException;

}

@@ -442,16 +442,68 @@
 onnull;%0A
+import org.opendaylight.yangtools.concepts.Builder;%0A
 import o
@@ -899,16 +899,70 @@
 t%3CA, D%3E%3E
+%0A    implements Builder%3CStatementContextBase%3CA, D, E%3E%3E
  %7B%0A%0A    
@@ -1079,34 +1079,8 @@
 ef;%0A
-    private String rawArg;
 %0A   
@@ -1121,27 +1121,47 @@
 argRef;%0A
-%0A
     p
-ublic
+rivate String rawArg;%0A%0A   
  Context
@@ -1168,16 +1168,22 @@
 Builder(
+final 
 Statemen
@@ -1214,16 +1214,22 @@
  E%3E def,
+ final
  Stateme
@@ -1285,19 +1285,47 @@
 ition = 
+Preconditions.checkNotNull(
 def
+)
 ;%0A      
@@ -1341,16 +1341,43 @@
 mtRef = 
+Preconditions.checkNotNull(
 sourceRe
@@ -1377,16 +1377,17 @@
 ourceRef
+)
 ;%0A    %7D%0A
@@ -1391,23 +1391,16 @@
  %7D%0A%0A    
-public 
 void set
@@ -1417,16 +1417,22 @@
 Nonnull 
+final 
 String a
@@ -1449,16 +1449,22 @@
 Nonnull 
+final 
 Statemen
@@ -1510,36 +1510,33 @@
     
-Preconditions.checkArgument(
+SourceException.throwIf(!
 defi
@@ -1556,16 +1556,32 @@
 ument(),
+ argumentSource,
  %22Statem
@@ -1584,16 +1584,19 @@
 atement 
+%25s 
 does not
@@ -1613,10 +1613,52 @@
 ment
-.
 %22
+,%0A            definition.getStatementName()
 );%0A 
@@ -1793,23 +1793,16 @@
  %7D%0A%0A    
-public 
 String g
@@ -1845,39 +1845,32 @@
 Arg;%0A    %7D%0A%0A    
-public 
 StatementSourceR
@@ -1928,39 +1928,32 @@
 Ref;%0A    %7D%0A%0A    
-public 
 StatementSourceR
@@ -2009,39 +2009,32 @@
 Ref;%0A    %7D%0A%0A    
-public 
 StatementDefinit
@@ -2109,23 +2109,16 @@
  %7D%0A%0A    
-public 
 Statemen
@@ -2239,90 +2239,6 @@
   %7D%0A
-%0A    public abstract StatementContextBase%3CA, D, E%3E build() throws SourceException;%0A%0A
 %7D%0A

Improve ContextBuilder error reporting