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 com.google.common.collect.ImmutableList;
11 import java.lang.invoke.MethodHandles;
12 import java.lang.invoke.VarHandle;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 // FIXME: 6.0.0: use DocumentedNodeMixin.WithStatus instead of keeping any state
26 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
27 extends AbstractEffectiveDocumentedNode<A, D> implements DocumentedNode.WithStatus {
28 private static final VarHandle UNKNOWN_NODES;
32 UNKNOWN_NODES = MethodHandles.lookup().findVarHandle(AbstractEffectiveDocumentedNodeWithStatus.class,
33 "unknownNodes", ImmutableList.class);
34 } catch (NoSuchFieldException | IllegalAccessException e) {
35 throw new ExceptionInInitializerError(e);
39 private final @NonNull Status status;
41 @SuppressWarnings("unused")
42 private volatile ImmutableList<UnknownSchemaNode> unknownNodes;
48 * context of statement.
50 protected AbstractEffectiveDocumentedNodeWithStatus(final StmtContext<A, D, ?> ctx) {
52 status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
56 public final Status getStatus() {
61 public final Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
62 final ImmutableList<UnknownSchemaNode> existing =
63 (ImmutableList<UnknownSchemaNode>) UNKNOWN_NODES.getAcquire(this);
64 return existing != null ? existing : loadUnknownSchemaNodes();
67 @SuppressWarnings("unchecked")
68 private @NonNull ImmutableList<UnknownSchemaNode> loadUnknownSchemaNodes() {
69 final List<UnknownSchemaNode> init = new ArrayList<>();
70 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
71 if (stmt instanceof UnknownSchemaNode) {
72 init.add((UnknownSchemaNode) stmt);
76 final ImmutableList<UnknownSchemaNode> computed = ImmutableList.copyOf(init);
77 final Object witness = UNKNOWN_NODES.compareAndExchangeRelease(this, null, computed);
78 return witness == null ? computed : (ImmutableList<UnknownSchemaNode>) witness;