Add CommonStmtCtx
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / CommonStmtCtx.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.spi.meta;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.VerifyException;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
19
20 /**
21  * Common interface for all statement contexts, exposing information which is always available. Note this includes only
22  * stateless information -- hence we have {@link #rawArgument()} but do not have an equivalent {@code argument()}.
23  */
24 @Beta
25 public interface CommonStmtCtx {
26     /**
27      * See {@link StatementSupport#getPublicView()}.
28      */
29     @NonNull StatementDefinition publicDefinition();
30
31     /**
32      * Returns a reference to statement source.
33      *
34      * @return reference of statement source
35      */
36     @NonNull StatementSourceReference sourceReference();
37
38     /**
39      * Returns the origin of the statement.
40      *
41      * @return origin of statement
42      */
43     default @NonNull StatementSource source() {
44         return sourceReference().getStatementSource();
45     }
46
47     /**
48      * Return the statement argument in literal format.
49      *
50      * @return raw statement argument string, or null if this statement does not have an argument.
51      */
52     @Nullable String rawArgument();
53
54     /**
55      * Return the statement argument in literal format.
56      *
57      * @return raw statement argument string
58      * @throws VerifyException if this statement does not have an argument
59      */
60     default @NonNull String getRawArgument() {
61         return verifyNotNull(rawArgument(), "Statement context %s does not have an argument", this);
62     }
63 }