BUG-5222: Reuse substatements across phases
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextWriter.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 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
15 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
16 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
17
18 final class StatementContextWriter implements StatementWriter {
19     private final ModelProcessingPhase phase;
20     private final SourceSpecificContext ctx;
21
22     private StatementContextBase<?, ?, ?> current;
23
24     StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
25         this.ctx = Preconditions.checkNotNull(ctx);
26         this.phase = Preconditions.checkNotNull(phase);
27     }
28
29     @Override
30     public void startStatement(final int childId, @Nonnull final QName name, final String argument,
31             @Nonnull final StatementSourceReference ref) {
32         current = Verify.verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
33     }
34
35     @Override
36     public void endStatement(@Nonnull final StatementSourceReference ref) {
37         Preconditions.checkState(current != null);
38         current.endDeclared(ref, phase);
39         current = current.getParentContext();
40     }
41
42     @Nonnull
43     @Override
44     public ModelProcessingPhase getPhase() {
45         return phase;
46     }
47 }