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