BUG-7052: reduce StatementContextBase proliferation
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveStatementBase.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.stmt.rfc6020.effective;
9
10 import com.google.common.base.Optional;
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.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Map;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
27
28 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
29     private final List<? extends EffectiveStatement<?, ?>> substatements;
30
31     /**
32      * Constructor.
33      *
34      * @param ctx
35      *            context of statement.
36      */
37     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
38         final Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
39         final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
40
41         final Collection<? extends StmtContext<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
42                 ctx.declaredSubstatements(), StmtContext::isSupportedByFeatures);
43         for (final StmtContext<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
44             if (declaredSubstatement.getPublicDefinition().equals(YangStmtMapping.USES)) {
45                 substatementsInit.add(declaredSubstatement);
46                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
47                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
48                         .getEffectOfStatement());
49             } else {
50                 substatementsInit.add(declaredSubstatement);
51             }
52         }
53         substatementsInit.addAll(effectiveSubstatements);
54
55         this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
56     }
57
58     /**
59      * Create a set of substatements. This method is split out so it can be overridden in
60      * {@link ExtensionEffectiveStatementImpl} to leak a not-fully-initialized instance.
61      *
62      * @param substatementsInit proposed substatements
63      * @return Filtered substatements
64      */
65     Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
66             final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
67         return Collections2.transform(Collections2.filter(substatementsInit,
68             StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
69     }
70
71     @Override
72     public final <K, V, N extends IdentifierNamespace<K, V>> V get(@Nonnull final Class<N> namespace, @Nonnull final K identifier) {
73         throw new UnsupportedOperationException("Not implemented yet.");
74     }
75
76     @Override
77     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull final Class<N> namespace) {
78         throw new UnsupportedOperationException("Not implemented yet.");
79     }
80
81     @Nonnull
82     @Override
83     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
84         return substatements;
85     }
86
87     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
88         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
89                 Predicates.instanceOf(type));
90         return possible.isPresent() ? type.cast(possible.get()) : null;
91     }
92
93     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
94         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
95                 Predicates.instanceOf(type));
96         return possible.isPresent() ? type.cast(possible.get()) : null;
97     }
98
99     @SuppressWarnings("unchecked")
100     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
101         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
102     }
103
104     protected final <T> T firstSubstatementOfType(final Class<T> type) {
105         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
106                 Predicates.instanceOf(type));
107         return possible.isPresent() ? type.cast(possible.get()) : null;
108     }
109
110     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
111         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
112                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
113         return possible.isPresent() ? returnType.cast(possible.get()) : null;
114     }
115
116     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
117         return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
118     }
119 }