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.rfc7950.stmt;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.ArrayList;
17 import java.util.Collection;
19 import java.util.Optional;
20 import java.util.function.Predicate;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
32 private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
37 * @param ctx context of statement.
39 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
40 final Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
41 final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
43 final Collection<? extends StmtContext<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
44 ctx.declaredSubstatements(), StmtContext::isSupportedByFeatures);
45 for (final StmtContext<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
46 if (YangStmtMapping.USES == declaredSubstatement.getPublicDefinition()) {
47 substatementsInit.add(declaredSubstatement);
48 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
49 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
50 .getEffectOfStatement());
52 substatementsInit.add(declaredSubstatement);
55 substatementsInit.addAll(effectiveSubstatements);
57 this.substatements = ImmutableList.copyOf(initSubstatements(ctx, substatementsInit));
61 protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(final StmtContext<A, D, ?> ctx,
62 final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
63 return initSubstatements(substatementsInit);
67 * Create a set of substatements. This method is split out so it can be overridden in
68 * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
70 * @param substatementsInit proposed substatements
71 * @return Filtered substatements
73 protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
74 final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
75 return Collections2.transform(Collections2.filter(substatementsInit,
76 StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
80 public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
82 return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
86 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
87 final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
88 return ret.isPresent() ? ret.get() : ImmutableMap.of();
92 * Return the statement-specific contents of specified namespace, if available.
94 * @param namespace Requested namespace
95 * @return Namespace contents, if available.
98 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
99 final @NonNull Class<N> namespace) {
100 return Optional.empty();
104 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
105 return substatements;
108 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
109 return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
112 @SuppressWarnings("unchecked")
113 public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
114 return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
117 protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
118 return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
121 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
122 return substatements.stream()
123 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
124 .findFirst().map(returnType::cast).orElse(null);
127 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
128 return substatements.stream().filter(type::isInstance).findFirst().orElse(null);