Populate data/ hierarchy
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / meta / AbstractEffectiveDocumentedNodeWithStatus.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.model.spi.meta;
9
10 import static java.util.Objects.requireNonNull;
11
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;
25
26 /**
27  * A declared {@link AbstractDeclaredEffectiveStatement} with {@link DocumentedNode.WithStatus}.
28  */
29 @Beta
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;
36
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);
42     }
43
44     @Override
45     public final A argument() {
46         return argument;
47     }
48
49     @Override
50     public final @NonNull D getDeclared() {
51         return declared;
52     }
53
54     @Override
55     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
56         return substatements;
57     }
58
59     @SuppressWarnings("unchecked")
60     public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
61         return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
62     }
63
64     @Override
65     public final Status getStatus() {
66         return findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
67     }
68
69     protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
70         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
71     }
72
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);
77     }
78 }