3403aa3de00410e528c59edb1c4b1e2e7af53e2f
[yangtools.git] / yang / yang-parser-impl / 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.base.Predicates;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import java.util.Collection;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
21
22 /**
23  * Utility abstract base class for implementing declared statements.
24  *
25  *
26  * @param <A> Argument type.
27  */
28 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
29
30
31     private final A argument;
32     private final String rawArgument;
33     private final ImmutableList<? extends DeclaredStatement<?>> substatements;
34     private final StatementDefinition definition;
35     private final StatementSource source;
36
37     protected AbstractDeclaredStatement(final StmtContext<A,?,?> context) {
38         rawArgument = context.rawStatementArgument();
39         argument = context.getStatementArgument();
40         source = context.getStatementSource();
41         definition = context.getPublicDefinition();
42         /*
43          * Perform an explicit copy, because Collections2.transform() is lazily transformed and retains pointer to
44          * original collection, which may contains references to mutable context.
45          */
46         substatements = ImmutableList.copyOf(Collections2.transform(context.declaredSubstatements(),
47             StatementContextBase::buildDeclared));
48     }
49
50     protected final <S extends DeclaredStatement<?>> S firstDeclared(final Class<S> type) {
51         return type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
52     }
53
54     @Override
55     public String rawArgument() {
56         return rawArgument;
57     }
58
59     @Override
60     public A argument() {
61         return argument;
62     }
63
64     @Nonnull
65     @Override
66     public StatementDefinition statementDefinition() {
67         return definition;
68     }
69
70     @Nonnull
71     @Override
72     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
73         return substatements;
74     }
75
76     @Nonnull
77     @Override
78     public StatementSource getStatementSource() {
79         return source;
80     }
81
82     @SuppressWarnings("unchecked")
83     protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
84         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
85     }
86 }