BUG-4456: add RecursiveExtensionResolver
[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 org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.RecursiveObjectLeaker;
28
29 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
30     private final List<? extends EffectiveStatement<?, ?>> substatements;
31
32     /**
33      * Constructor.
34      *
35      * @param ctx
36      *            context of statement.
37      */
38     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
39         final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
40         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
41
42         final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
43                 ctx.declaredSubstatements(), StmtContextUtils::areFeaturesSupported);
44         for (final StatementContextBase<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
45             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
46                 substatementsInit.add(declaredSubstatement);
47                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
48                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
49                         .getEffectOfStatement());
50             } else {
51                 substatementsInit.add(declaredSubstatement);
52             }
53         }
54         substatementsInit.addAll(effectiveSubstatements);
55
56         // WARNING: this leaks an incompletely-initialized pbject
57         RecursiveObjectLeaker.inConstructor(this);
58
59         this.substatements = ImmutableList.copyOf(Collections2.transform(Collections2.filter(substatementsInit,
60             StmtContext::isSupportedToBuildEffective), StatementContextBase::buildEffective));
61     }
62
63     @Override
64     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
65         throw new UnsupportedOperationException("Not implemented yet.");
66     }
67
68     @Override
69     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
70         throw new UnsupportedOperationException("Not implemented yet.");
71     }
72
73     @Override
74     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
75         return substatements;
76     }
77
78     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
79         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
80                 Predicates.instanceOf(type));
81         return possible.isPresent() ? type.cast(possible.get()) : null;
82     }
83
84     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
85         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
86                 Predicates.instanceOf(type));
87         return possible.isPresent() ? type.cast(possible.get()) : null;
88     }
89
90     @SuppressWarnings("unchecked")
91     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
92         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
93     }
94
95     protected final <T> T firstSubstatementOfType(final Class<T> type) {
96         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
97                 Predicates.instanceOf(type));
98         return possible.isPresent() ? type.cast(possible.get()) : null;
99     }
100
101     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
102         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
103                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
104         return possible.isPresent() ? returnType.cast(possible.get()) : null;
105     }
106
107     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
108         return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
109     }
110 }