Convert parser-{reactor,spi} classes to JDT annotations
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractDeclaredStatement.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.spi.meta;
9
10 import com.google.common.collect.Collections2;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
17
18 /**
19  * Utility abstract base class for implementing declared statements.
20  *
21  * @param <A> Argument type.
22  */
23 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
24     private final @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements;
25     private final @NonNull StatementDefinition definition;
26     private final @NonNull StatementSource source;
27
28     private final A argument;
29     private final String rawArgument;
30
31     protected AbstractDeclaredStatement(final StmtContext<A, ?, ?> context) {
32         rawArgument = context.rawStatementArgument();
33         argument = context.getStatementArgument();
34         source = context.getStatementSource();
35         definition = context.getPublicDefinition();
36         /*
37          * Perform an explicit copy, because Collections2.transform() is lazily transformed and retains pointer to
38          * original collection, which may contains references to mutable context.
39          */
40         substatements = ImmutableList.copyOf(Collections2.transform(context.declaredSubstatements(),
41             StmtContext::buildDeclared));
42     }
43
44     /**
45      * Find first declared substatement of a particular type.
46      *
47      * @param type {@link DeclaredStatement} type
48      * @return First effective substatement, or null if no match is found.
49      * @deprecated Use {@link #findFirstDeclaredSubstatement(Class)} instead.
50      */
51     @Deprecated
52     protected final <S extends DeclaredStatement<?>> S firstDeclared(final Class<S> type) {
53         return findFirstDeclaredSubstatement(type).orElse(null);
54     }
55
56     @Override
57     public String rawArgument() {
58         return rawArgument;
59     }
60
61     @Override
62     public A argument() {
63         return argument;
64     }
65
66     @Override
67     public StatementDefinition statementDefinition() {
68         return definition;
69     }
70
71     @Override
72     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
73         return substatements;
74     }
75
76     @Override
77     public StatementSource getStatementSource() {
78         return source;
79     }
80
81     /**
82      * Returns collection of explicitly declared child statements, while preserving its original ordering from original
83      * source.
84      *
85      * @param type {@link DeclaredStatement} type
86      * @return Collection of statements, which were explicitly declared in source of model.
87      * @throws NullPointerException if {@code type} is null
88      * @deprecated Use {@link #declaredSubstatements(Class)} instead.
89      */
90     @Deprecated
91     protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
92         return declaredSubstatements(type);
93     }
94 }