<commit_before>/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;

// FIXME: 5.0.0: rename to AbstractEffectiveDocumentedNodeWithStatus
public abstract class AbstractEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
        extends AbstractEffectiveDocumentedNodeWithoutStatus<A, D> implements DocumentedNode.WithStatus {
    private final @NonNull ImmutableList<UnknownSchemaNode> unknownNodes;
    private final @NonNull Status status;

    /**
     * Constructor.
     *
     * @param ctx
     *            context of statement.
     */
    protected AbstractEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
        super(ctx);
        status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);

        final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
        for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
            if (stmt instanceof UnknownSchemaNode) {
                unknownNodesInit.add((UnknownSchemaNode) stmt);
            }
        }
        unknownNodes = ImmutableList.copyOf(unknownNodesInit);
    }

    @Override
    public final Status getStatus() {
        return status;
    }

    @Override
    public final ImmutableList<UnknownSchemaNode> getUnknownSchemaNodes() {
        return unknownNodes;
    }
}<commit_msg>Compute unknown nodes lazily<commit_after>/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;

import com.google.common.collect.ImmutableList;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;

// FIXME: 5.0.0: rename to AbstractEffectiveDocumentedNodeWithStatus
public abstract class AbstractEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
        extends AbstractEffectiveDocumentedNodeWithoutStatus<A, D> implements DocumentedNode.WithStatus {
    private static final VarHandle UNKNOWN_NODES;

    static {
        try {
            UNKNOWN_NODES = MethodHandles.lookup().findVarHandle(AbstractEffectiveDocumentedNode.class,
                "unknownNodes", ImmutableList.class);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    private final @NonNull Status status;

    @SuppressWarnings("unused")
    private volatile ImmutableList<UnknownSchemaNode> unknownNodes;

    /**
     * Constructor.
     *
     * @param ctx
     *            context of statement.
     */
    protected AbstractEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
        super(ctx);
        status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
    }

    @Override
    public final Status getStatus() {
        return status;
    }

    @Override
    public final ImmutableList<UnknownSchemaNode> getUnknownSchemaNodes() {
        final ImmutableList<UnknownSchemaNode> existing =
                (ImmutableList<UnknownSchemaNode>) UNKNOWN_NODES.getAcquire(this);
        return existing != null ? existing : loadUnknownSchemaNodes();
    }

    @SuppressWarnings("unchecked")
    private @NonNull ImmutableList<UnknownSchemaNode> loadUnknownSchemaNodes() {
        final List<UnknownSchemaNode> init = new ArrayList<>();
        for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
            if (stmt instanceof UnknownSchemaNode) {
                init.add((UnknownSchemaNode) stmt);
            }
        }

        final ImmutableList<UnknownSchemaNode> computed = ImmutableList.copyOf(init);
        final Object witness = UNKNOWN_NODES.compareAndExchangeRelease(this, null, computed);
        return witness == null ? computed : (ImmutableList<UnknownSchemaNode>) witness;
    }
}
{'fraction_non_alphanumeric': 0.09100719424460432, 'fraction_numerical': 0.00539568345323741, 'mean_word_length': 3.861013986013986, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2411445', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1525, 'n_words': 323}