62ac6e5f774ac1400a203a2188533aa3de2f037d
[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.concepts.Builder;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
18
19 abstract class ContextBuilder<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
20     implements Builder<StatementContextBase<A, D, E>> {
21
22     private final StatementDefinitionContext<A, D, E> definition;
23     private final StatementSourceReference stmtRef;
24
25     private StatementSourceReference argRef;
26     private String rawArg;
27
28     ContextBuilder(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference sourceRef) {
29         this.definition = Preconditions.checkNotNull(def);
30         this.stmtRef = Preconditions.checkNotNull(sourceRef);
31     }
32
33     void setArgument(@Nonnull final String argument, @Nonnull final StatementSourceReference argumentSource) {
34         SourceException.throwIf(!definition.hasArgument(), argumentSource, "Statement %s does not take argument",
35             definition.getStatementName());
36         this.rawArg = Preconditions.checkNotNull(argument);
37         this.argRef = Preconditions.checkNotNull(argumentSource);
38     }
39
40     String getRawArgument() {
41         return rawArg;
42     }
43
44     StatementSourceReference getStamementSource() {
45         return stmtRef;
46     }
47
48     StatementSourceReference getArgumentSource() {
49         return argRef;
50     }
51
52     StatementDefinitionContext<A, D, E> getDefinition() {
53         return definition;
54     }
55
56     StatementIdentifier createIdentifier() {
57         return new StatementIdentifier(definition.getStatementName(), rawArg);
58     }
59
60     /**
61      * {@inheritDoc}
62      *
63      * @throws SourceException when a source-level problem is found
64      */
65     @Override
66     public abstract StatementContextBase<A, D, E> build();
67 }