Split off DeclaredEffectiveStatementBase
[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
28 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
29
30     private final List<? extends EffectiveStatement<?, ?>> substatements;
31
32     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
33         Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
34
35         Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
36
37         for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
38             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
39                 substatementsInit.add(declaredSubstatement);
40                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
41                 ((StatementContextBase<?, ?, ?>)ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
42                         .getEffectOfStatement());
43             } else {
44                 substatementsInit.add(declaredSubstatement);
45             }
46         }
47
48         substatementsInit.addAll(effectiveSubstatements);
49
50         this.substatements = ImmutableList.copyOf(Collections2.transform(
51             Collections2.filter(substatementsInit, StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE),
52                 StmtContextUtils.buildEffective()));
53     }
54
55     @Override
56     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
57         throw new UnsupportedOperationException("Not implemented yet.");
58     }
59
60     @Override
61     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
62         throw new UnsupportedOperationException("Not implemented yet.");
63     }
64
65     @Override
66     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
67         return substatements;
68     }
69
70     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
71         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
72                 Predicates.instanceOf(type));
73         return possible.isPresent() ? type.cast(possible.get()) : null;
74     }
75
76     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
77         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
78                 Predicates.instanceOf(type));
79         return possible.isPresent() ? type.cast(possible.get()) : null;
80     }
81
82     @SuppressWarnings("unchecked")
83     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
84         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
85     }
86
87     protected final <T> T firstSubstatementOfType(final Class<T> type) {
88         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 <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
94         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
95                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
96         return possible.isPresent() ? returnType.cast(possible.get()) : null;
97     }
98
99     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
100         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
101                 Predicates.instanceOf(type));
102         return possible.isPresent() ? possible.get() : null;
103     }
104 }