Remove unused abstract classes
[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 com.google.common.annotations.Beta;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.function.Predicate;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Status;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
30 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34
35 /**
36  * A declared {@link AbstractEffectiveStatement} with DocumentedNode.WithStatus.
37  */
38 //FIXME: 6.0.0: use DocumentedNodeMixin.WithStatus instead of keeping any state
39 @Beta
40 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
41         extends AbstractEffectiveStatement<A, D> implements DocumentedNode.WithStatus {
42     private static final VarHandle UNKNOWN_NODES;
43
44     static {
45         try {
46             UNKNOWN_NODES = MethodHandles.lookup().findVarHandle(AbstractEffectiveDocumentedNodeWithStatus.class,
47                 "unknownNodes", ImmutableList.class);
48         } catch (NoSuchFieldException | IllegalAccessException e) {
49             throw new ExceptionInInitializerError(e);
50         }
51     }
52
53     private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
54     private final @NonNull StatementSource statementSource;
55     private final A argument;
56     private final @NonNull D declaredInstance;
57     private final @Nullable String description;
58     private final @Nullable String reference;
59     private final @NonNull Status status;
60
61     @SuppressWarnings("unused")
62     private volatile ImmutableList<UnknownSchemaNode> unknownNodes;
63
64     /**
65      * Constructor.
66      *
67      * @param ctx context of statement.
68      */
69     protected AbstractEffectiveDocumentedNodeWithStatus(final StmtContext<A, D, ?> ctx) {
70         argument = ctx.getStatementArgument();
71         statementSource = ctx.getStatementSource();
72         declaredInstance = ctx.buildDeclared();
73         substatements = ImmutableList.copyOf(
74             Collections2.transform(Collections2.filter(BaseStatementSupport.declaredSubstatements(ctx),
75                 StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective));
76
77         description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
78         reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
79         status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
80     }
81
82     @Override
83     public final StatementDefinition statementDefinition() {
84         return declaredInstance.statementDefinition();
85     }
86
87     @Override
88     public A argument() {
89         return argument;
90     }
91
92     @Override
93     public final StatementSource getStatementSource() {
94         return statementSource;
95     }
96
97     @Override
98     public final D getDeclared() {
99         return declaredInstance;
100     }
101
102     @Override
103     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
104         return substatements;
105     }
106
107     @Override
108     public final Optional<String> getDescription() {
109         return Optional.ofNullable(description);
110     }
111
112     @Override
113     public final Optional<String> getReference() {
114         return Optional.ofNullable(reference);
115     }
116
117     @SuppressWarnings("unchecked")
118     public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
119         return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
120     }
121
122     @Override
123     public final Status getStatus() {
124         return status;
125     }
126
127     @Override
128     public final Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
129         final ImmutableList<UnknownSchemaNode> existing =
130                 (ImmutableList<UnknownSchemaNode>) UNKNOWN_NODES.getAcquire(this);
131         return existing != null ? existing : loadUnknownSchemaNodes();
132     }
133
134     protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
135         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
136     }
137
138     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
139         return effectiveSubstatements().stream()
140                 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
141                 .findFirst().map(returnType::cast).orElse(null);
142     }
143
144     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
145         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().orElse(null);
146     }
147
148     // FIXME: rename to 'getFirstEffectiveStatement()'
149     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
150         return findFirstEffectiveSubstatement(type).orElse(null);
151     }
152
153     @SuppressWarnings("unchecked")
154     private @NonNull ImmutableList<UnknownSchemaNode> loadUnknownSchemaNodes() {
155         final List<UnknownSchemaNode> init = new ArrayList<>();
156         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
157             if (stmt instanceof UnknownSchemaNode) {
158                 init.add((UnknownSchemaNode) stmt);
159             }
160         }
161
162         final ImmutableList<UnknownSchemaNode> computed = ImmutableList.copyOf(init);
163         final Object witness = UNKNOWN_NODES.compareAndExchangeRelease(this, null, computed);
164         return witness == null ? computed : (ImmutableList<UnknownSchemaNode>) witness;
165     }
166 }