Cleanup use of Guava library
[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     protected final <S extends DeclaredStatement<?>> S firstDeclared(final Class<S> type) {
45         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
46     }
47
48     @Override
49     public String rawArgument() {
50         return rawArgument;
51     }
52
53     @Override
54     public A argument() {
55         return argument;
56     }
57
58     @Nonnull
59     @Override
60     public StatementDefinition statementDefinition() {
61         return definition;
62     }
63
64     @Nonnull
65     @Override
66     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
67         return substatements;
68     }
69
70     @Nonnull
71     @Override
72     public StatementSource getStatementSource() {
73         return source;
74     }
75
76     protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
77         return Collections2.transform(Collections2.filter(substatements, type::isInstance), type::cast);
78     }
79 }