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 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
26 extends AbstractEffectiveDocumentedNode<A, D> implements DocumentedNode.WithStatus {
27 private static final VarHandle UNKNOWN_NODES;
31 UNKNOWN_NODES = MethodHandles.lookup().findVarHandle(AbstractEffectiveDocumentedNodeWithStatus.class,
32 "unknownNodes", ImmutableList.class);
33 } catch (NoSuchFieldException | IllegalAccessException e) {
34 throw new ExceptionInInitializerError(e);
38 private final @NonNull Status status;
40 @SuppressWarnings("unused")
41 private volatile ImmutableList<UnknownSchemaNode> unknownNodes;
47 * context of statement.
49 protected AbstractEffectiveDocumentedNodeWithStatus(final StmtContext<A, D, ?> ctx) {
51 status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
55 public final Status getStatus() {
60 public final Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
61 final ImmutableList<UnknownSchemaNode> existing =
62 (ImmutableList<UnknownSchemaNode>) UNKNOWN_NODES.getAcquire(this);
63 return existing != null ? existing : loadUnknownSchemaNodes();
66 @SuppressWarnings("unchecked")
67 private @NonNull ImmutableList<UnknownSchemaNode> loadUnknownSchemaNodes() {
68 final List<UnknownSchemaNode> init = new ArrayList<>();
69 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
70 if (stmt instanceof UnknownSchemaNode) {
71 init.add((UnknownSchemaNode) stmt);
75 final ImmutableList<UnknownSchemaNode> computed = ImmutableList.copyOf(init);
76 final Object witness = UNKNOWN_NODES.compareAndExchangeRelease(this, null, computed);
77 return witness == null ? computed : (ImmutableList<UnknownSchemaNode>) witness;