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 java.util.Collection;
16 import java.util.function.Predicate;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
20 import org.opendaylight.yangtools.yang.model.api.Status;
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.stmt.StatusEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractDeclaredEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.DocumentedNodeMixin;
28 * A declared {@link AbstractDeclaredEffectiveStatement} with DocumentedNode.WithStatus.
31 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
32 extends AbstractDeclaredEffectiveStatement<A, D>
33 implements DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
34 private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
35 private final @NonNull D declared;
36 private final A argument;
38 protected AbstractEffectiveDocumentedNodeWithStatus(final A argument, final @NonNull D declared,
39 final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
40 this.argument = argument;
41 this.declared = requireNonNull(declared);
42 this.substatements = requireNonNull(substatements);
46 public final A argument() {
51 public final @NonNull D getDeclared() {
56 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
60 @SuppressWarnings("unchecked")
61 public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
62 return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
66 public final Status getStatus() {
67 return findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
70 protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
71 return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
74 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
75 return effectiveSubstatements().stream()
76 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
77 .findFirst().map(returnType::cast).orElse(null);