2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
23 final class StatementContextWriter implements StatementWriter {
24 private final @NonNull ModelProcessingPhase phase;
25 private final SourceSpecificContext ctx;
27 private AbstractResumedStatement<?, ?, ?> current;
29 StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
30 this.ctx = requireNonNull(ctx);
31 this.phase = requireNonNull(phase);
35 public ModelProcessingPhase getPhase() {
40 public Optional<? extends ResumedStatement> resumeStatement(final int childId) {
41 final AbstractResumedStatement<?, ?, ?> existing = lookupDeclaredChild(current, childId);
42 if (existing != null) {
43 resumeStatement(existing);
44 return Optional.of(existing);
46 return Optional.empty();
49 private void resumeStatement(final AbstractResumedStatement<?, ?, ?> child) {
50 if (child.isFullyDefined()) {
51 child.declarationFinished(phase);
58 public void storeStatement(final int expectedChildren, final boolean fullyDefined) {
59 checkState(current != null);
60 checkArgument(expectedChildren >= 0);
61 current.resizeSubstatements(expectedChildren);
64 current.setFullyDefined();
69 public void startStatement(final int childId, final QName name, final String argument,
70 final StatementSourceReference ref) {
71 final AbstractResumedStatement<?, ?, ?> existing = lookupDeclaredChild(current, childId);
72 current = existing != null ? existing
73 : verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
77 public void endStatement(final StatementSourceReference ref) {
78 checkState(current != null);
79 current = current.exitStatement(phase);
82 private static @Nullable AbstractResumedStatement<?, ?, ?> lookupDeclaredChild(
83 final AbstractResumedStatement<?, ?, ?> current, final int childId) {
84 return current == null ? null : current.enterSubstatement(childId);