7270c8ff4bf4c7671a72d8c773aef8ec4a6e0df2
[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
9 package org.opendaylight.yangtools.yang.parser.spi.meta;
10
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
18
19 /**
20  * Utility abstract base class for implementing declared statements.
21  *
22  * @param <A> Argument type.
23  */
24 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
25     private final A argument;
26     private final String rawArgument;
27     private final ImmutableList<? extends DeclaredStatement<?>> substatements;
28     private final StatementDefinition definition;
29     private final StatementSource source;
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     @Nonnull
67     @Override
68     public StatementDefinition statementDefinition() {
69         return definition;
70     }
71
72     @Nonnull
73     @Override
74     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
75         return substatements;
76     }
77
78     @Nonnull
79     @Override
80     public StatementSource getStatementSource() {
81         return source;
82     }
83
84     protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
85         return Collections2.transform(Collections2.filter(substatements, type::isInstance), type::cast);
86     }
87 }