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