2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
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;
19 import javax.annotation.Nonnull;
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;
29 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
30 private final List<? extends EffectiveStatement<?, ?>> substatements;
36 * context of statement.
38 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
39 final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
40 final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
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());
51 substatementsInit.add(declaredSubstatement);
54 substatementsInit.addAll(effectiveSubstatements);
56 this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
60 * Create a set of substatements. This method is split out so it can be overridden in
61 * {@link ExtensionEffectiveStatementImpl} to leak a not-fully-initialized instance.
63 * @param substatementsInit proposed substatements
64 * @return Filtered substatements
66 Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
67 final Collection<StatementContextBase<?, ?, ?>> substatementsInit) {
68 return Collections2.transform(Collections2.filter(substatementsInit,
69 StmtContext::isSupportedToBuildEffective), StatementContextBase::buildEffective);
73 public final <K, V, N extends IdentifierNamespace<K, V>> V get(@Nonnull final Class<N> namespace, @Nonnull final K identifier) {
74 throw new UnsupportedOperationException("Not implemented yet.");
78 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull final Class<N> namespace) {
79 throw new UnsupportedOperationException("Not implemented yet.");
84 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
88 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
89 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
90 Predicates.instanceOf(type));
91 return possible.isPresent() ? type.cast(possible.get()) : null;
94 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
95 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
96 Predicates.instanceOf(type));
97 return possible.isPresent() ? type.cast(possible.get()) : null;
100 @SuppressWarnings("unchecked")
101 protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
102 return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
105 protected final <T> T firstSubstatementOfType(final Class<T> type) {
106 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
107 Predicates.instanceOf(type));
108 return possible.isPresent() ? type.cast(possible.get()) : null;
111 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
112 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
113 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
114 return possible.isPresent() ? returnType.cast(possible.get()) : null;
117 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
118 return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();