Populate xpath/ hierarchy
[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.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
21
22 /**
23  * Common interface for all statement contexts, exposing information which is always available. Note this includes only
24  * stateless information -- hence we have {@link #rawArgument()} but do not have an equivalent {@code argument()}.
25  */
26 @Beta
27 public interface CommonStmtCtx {
28     /**
29      * See {@link StatementSupport#getPublicView()}.
30      */
31     @NonNull StatementDefinition publicDefinition();
32
33     /**
34      * Return true if this context produces specified {@link DeclaredStatement} representation.
35      *
36      * @param <D> Declared Statement representation
37      * @param type DeclaredStatement representation
38      * @return True if this context results in specified {@link DeclaredStatement} representation
39      */
40     default <D extends DeclaredStatement<?>> boolean producesDeclared(final Class<? super D> type) {
41         return type.isAssignableFrom(publicDefinition().getDeclaredRepresentationClass());
42     }
43
44     /**
45      * Return true if this context produces specified {@link EffectiveStatement} representation.
46      *
47      * @param <E> Effective Statement representation
48      * @param type EffectiveStatement representation
49      * @return True if this context results in specified {@link EffectiveStatement} representation
50      */
51     default <E extends EffectiveStatement<?, ?>> boolean producesEffective(final Class<? super E> type) {
52         return type.isAssignableFrom(publicDefinition().getEffectiveRepresentationClass());
53     }
54
55     /**
56      * Returns a reference to statement source.
57      *
58      * @return reference of statement source
59      */
60     @NonNull StatementSourceReference sourceReference();
61
62     /**
63      * Returns the origin of the statement.
64      *
65      * @return origin of statement
66      */
67     default @NonNull StatementOrigin origin() {
68         return sourceReference().statementOrigin();
69     }
70
71     /**
72      * Return the statement argument in literal format.
73      *
74      * @return raw statement argument string, or null if this statement does not have an argument.
75      */
76     @Nullable String rawArgument();
77
78     /**
79      * Return the statement argument in literal format.
80      *
81      * @return raw statement argument string
82      * @throws VerifyException if this statement does not have an argument
83      */
84     default @NonNull String getRawArgument() {
85         return verifyNotNull(rawArgument(), "Statement context %s does not have an argument", this);
86     }
87 }