Use switch expressions for CopyType dispatch
[yangtools.git] / parser / yang-parser-reactor / 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 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;
14
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;
22
23 final class StatementContextWriter implements StatementWriter {
24     private final @NonNull ModelProcessingPhase phase;
25     private final SourceSpecificContext ctx;
26
27     private AbstractResumedStatement<?, ?, ?> current;
28
29     StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
30         this.ctx = requireNonNull(ctx);
31         this.phase = requireNonNull(phase);
32     }
33
34     @Override
35     public ModelProcessingPhase getPhase() {
36         return phase;
37     }
38
39     @Override
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);
45         }
46         return Optional.empty();
47     }
48
49     private void resumeStatement(final AbstractResumedStatement<?, ?, ?> child) {
50         if (child.isFullyDefined()) {
51             child.declarationFinished(phase);
52         } else {
53             current = child;
54         }
55     }
56
57     @Override
58     public void storeStatement(final int expectedChildren, final boolean fullyDefined) {
59         checkState(current != null);
60         checkArgument(expectedChildren >= 0);
61         current.resizeSubstatements(expectedChildren);
62
63         if (fullyDefined) {
64             current.setFullyDefined();
65         }
66     }
67
68     @Override
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));
74     }
75
76     @Override
77     public void endStatement(final StatementSourceReference ref) {
78         checkState(current != null);
79         current = current.exitStatement(phase);
80     }
81
82     private static @Nullable AbstractResumedStatement<?, ?, ?> lookupDeclaredChild(
83             final AbstractResumedStatement<?, ?, ?> current, final int childId) {
84         return current == null ? null : current.enterSubstatement(childId);
85     }
86 }