c841a54d2b48faba467763d145000f38b9d3e7a8
[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.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
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 = new Predicate<StmtContext<?, ?, ?>>() {
33         @Override
34         public boolean apply(final StmtContext<?, ?, ?> input) {
35             return input.isSupportedToBuildEffective();
36         }
37     };
38
39     private static final Predicate<StmtContext<?, ?, ?>> IS_UNKNOWN_STATEMENT_CONTEXT = new Predicate<StmtContext<?, ?, ?>>() {
40         @Override
41         public boolean apply(final StmtContext<?, ?, ?> input) {
42             return StmtContextUtils.isUnknownStatement(input);
43         }
44     };
45
46     private static final Predicate<StatementContextBase<?, ?, ?>> ARE_FEATURES_SUPPORTED = new Predicate<StatementContextBase<?, ?, ?>>() {
47
48         @Override
49         public boolean apply(final StatementContextBase<?, ?, ?> input) {
50             return StmtContextUtils.areFeaturesSupported(input);
51         }
52     };
53
54     private final List<? extends EffectiveStatement<?, ?>> substatements;
55     private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
56
57     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
58         this(ctx, true);
59     }
60
61     /**
62      * Constructor.
63      *
64      * @param ctx
65      *            context of statement.
66      * @param buildUnknownSubstatements
67      *            if it is false, the unknown substatements are omitted from
68      *            build of effective substatements till the call of either
69      *            effectiveSubstatements or getOmittedUnknownSubstatements
70      *            method. The main purpose of this is to allow the build of
71      *            recursive extension definitions.
72      */
73     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, final boolean buildUnknownSubstatements) {
74
75         final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
76         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
77
78         final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
79                 ctx.declaredSubstatements(), ARE_FEATURES_SUPPORTED);
80         for (final StatementContextBase<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
81             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
82                 substatementsInit.add(declaredSubstatement);
83                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
84                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
85                         .getEffectOfStatement());
86             } else {
87                 substatementsInit.add(declaredSubstatement);
88             }
89         }
90         substatementsInit.addAll(effectiveSubstatements);
91
92         Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
93                 IS_SUPPORTED_TO_BUILD_EFFECTIVE);
94         if (!buildUnknownSubstatements) {
95             this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
96                     IS_UNKNOWN_STATEMENT_CONTEXT));
97             substatementsToBuild = Collections2.filter(substatementsToBuild,
98                     Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
99         } else {
100             this.unknownSubstatementsToBuild = ImmutableList.of();
101         }
102
103         final Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils
104                 .buildEffective();
105         this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
106     }
107
108     Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
109         final Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils
110                 .buildEffective();
111         return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
112     }
113
114     @Override
115     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
116         throw new UnsupportedOperationException("Not implemented yet.");
117     }
118
119     @Override
120     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
121         throw new UnsupportedOperationException("Not implemented yet.");
122     }
123
124     @Override
125     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
126         if (unknownSubstatementsToBuild.isEmpty()) {
127             return substatements;
128         } else {
129             return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
130         }
131     }
132
133     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
134         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
135                 Predicates.instanceOf(type));
136         return possible.isPresent() ? type.cast(possible.get()) : null;
137     }
138
139     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
140         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
141                 Predicates.instanceOf(type));
142         return possible.isPresent() ? type.cast(possible.get()) : null;
143     }
144
145     @SuppressWarnings("unchecked")
146     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
147         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
148     }
149
150     protected final <T> T firstSubstatementOfType(final Class<T> type) {
151         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
152                 Predicates.instanceOf(type));
153         return possible.isPresent() ? type.cast(possible.get()) : null;
154     }
155
156     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
157         final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
158                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
159         return possible.isPresent() ? returnType.cast(possible.get()) : null;
160     }
161
162     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
163         return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
164     }
165 }