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