BUG-7052: split out yang-parser-spi
[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.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
21 /**
22  * Utility abstract base class for implementing declared statements.
23  *
24  * @param <A> Argument type.
25  */
26 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
27     private final A argument;
28     private final String rawArgument;
29     private final ImmutableList<? extends DeclaredStatement<?>> substatements;
30     private final StatementDefinition definition;
31     private final StatementSource source;
32
33     protected AbstractDeclaredStatement(final StmtContext<A,?,?> context) {
34         rawArgument = context.rawStatementArgument();
35         argument = context.getStatementArgument();
36         source = context.getStatementSource();
37         definition = context.getPublicDefinition();
38         /*
39          * Perform an explicit copy, because Collections2.transform() is lazily transformed and retains pointer to
40          * original collection, which may contains references to mutable context.
41          */
42         substatements = ImmutableList.copyOf(Collections2.transform(context.declaredSubstatements(),
43             StmtContext::buildDeclared));
44     }
45
46     protected final <S extends DeclaredStatement<?>> S firstDeclared(final Class<S> type) {
47         return type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
48     }
49
50     @Override
51     public String rawArgument() {
52         return rawArgument;
53     }
54
55     @Override
56     public A argument() {
57         return argument;
58     }
59
60     @Nonnull
61     @Override
62     public StatementDefinition statementDefinition() {
63         return definition;
64     }
65
66     @Nonnull
67     @Override
68     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
69         return substatements;
70     }
71
72     @Nonnull
73     @Override
74     public StatementSource getStatementSource() {
75         return source;
76     }
77
78     @SuppressWarnings("unchecked")
79     protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
80         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
81     }
82 }