Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ContextBuilder.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
16 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
17
18 abstract class ContextBuilder<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
19
20     private final StatementDefinitionContext<A, D, E> definition;
21     private final StatementSourceReference stmtRef;
22     private String rawArg;
23     private StatementSourceReference argRef;
24
25     public ContextBuilder(StatementDefinitionContext<A, D, E> def, StatementSourceReference sourceRef) {
26         this.definition = def;
27         this.stmtRef = sourceRef;
28     }
29
30     public void setArgument(@Nonnull String argument, @Nonnull StatementSourceReference argumentSource) {
31         Preconditions.checkArgument(definition.hasArgument(), "Statement does not take argument.");
32         this.rawArg = Preconditions.checkNotNull(argument);
33         this.argRef = Preconditions.checkNotNull(argumentSource);
34     }
35
36     public String getRawArgument() {
37         return rawArg;
38     }
39
40     public StatementSourceReference getStamementSource() {
41         return stmtRef;
42     }
43
44     public StatementSourceReference getArgumentSource() {
45         return argRef;
46     }
47
48     public StatementDefinitionContext<A, D, E> getDefinition() {
49         return definition;
50     }
51
52     public StatementIdentifier createIdentifier() {
53         return new StatementIdentifier(definition.getStatementName(), rawArg);
54     }
55
56     public abstract StatementContextBase<A, D, E> build() throws SourceException;
57
58 }