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.model.spi.meta;
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.EffectiveStatementMixins.DocumentedNodeMixin;
27 * A declared {@link AbstractDeclaredEffectiveStatement} with {@link DocumentedNode.WithStatus}.
30 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
31 extends AbstractDeclaredEffectiveStatement<A, D>
32 implements DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
33 private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
34 private final @NonNull D declared;
35 private final A argument;
37 protected AbstractEffectiveDocumentedNodeWithStatus(final A argument, final @NonNull D declared,
38 final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
39 this.argument = argument;
40 this.declared = requireNonNull(declared);
41 this.substatements = requireNonNull(substatements);
45 public final A argument() {
50 public final @NonNull D getDeclared() {
55 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
59 @SuppressWarnings("unchecked")
60 public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
61 return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
65 public final Status getStatus() {
66 return findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
69 protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
70 return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
73 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
74 return effectiveSubstatements().stream()
75 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
76 .findFirst().map(returnType::cast).orElse(null);