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