Merge "Introduced skeletons of Contributor and User guide."
[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 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
10 import com.google.common.base.Predicates;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.FluentIterable;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
19
20 /**
21  * Utility abstract base class for implementing declared statements.
22  *
23  *
24  * @param <A> Argument type.
25  */
26 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
27
28
29     private final A argument;
30     private final String rawArgument;
31     private final ImmutableList<? extends DeclaredStatement<?>> substatements;
32     private final StatementDefinition definition;
33     private final StatementSource source;
34
35     protected AbstractDeclaredStatement(StmtContext<A,?,?> context) {
36         rawArgument = context.rawStatementArgument();
37         argument = context.getStatementArgument();
38         source = context.getStatementSource();
39         definition = context.getPublicDefinition();
40         /*
41          *  Collections.transform could not be used here, since it is lazily
42          *  transformed and retains pointer to original collection, which may
43          *  contains references to mutable context.
44          *
45          *  FluentIterable.tranform().toList() - actually performs transformation
46          *  and creates immutable list from transformed results.
47          */
48         substatements = FluentIterable.from(context.declaredSubstatements()).transform(StmtContextUtils.buildDeclared()).toList();
49     }
50
51     protected final <S extends DeclaredStatement<?>> S firstDeclared(Class<S> type) {
52         return type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
53     }
54
55     @Override
56     public String rawArgument() {
57         return rawArgument;
58     }
59
60     @Override
61     public A argument() {
62         return argument;
63     }
64
65     @Override
66     public StatementDefinition statementDefinition() {
67         return definition;
68     }
69
70     @Override
71     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
72         return substatements;
73     }
74
75     @Override
76     public StatementSource getStatementSource() {
77         return source;
78     }
79
80     @SuppressWarnings("unchecked")
81     protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(Class<S> type) {
82         return Collection.class.cast(Collections2.filter(substatements,Predicates.instanceOf(type)));
83     }
84 }