Fixup collections return implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveDocumentedNodeWithStatus.java
index 493c37970ffb465a5944b76d1dcc649bd2e0fa82..fdc94a358f0345aed2caaf964c80de7800fed491 100644 (file)
@@ -7,73 +7,72 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.VarHandle;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
+import java.util.function.Predicate;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 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;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.DocumentedNodeMixin;
 
+/**
+ * A declared {@link AbstractEffectiveStatement} with DocumentedNode.WithStatus.
+ */
+@Beta
 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
-        extends AbstractEffectiveDocumentedNode<A, D> implements DocumentedNode.WithStatus {
-    private static final VarHandle UNKNOWN_NODES;
+        extends AbstractDeclaredEffectiveStatement<A, D>
+        implements DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
+    private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
+    private final @NonNull D declared;
+    private final A argument;
 
-    static {
-        try {
-            UNKNOWN_NODES = MethodHandles.lookup().findVarHandle(AbstractEffectiveDocumentedNodeWithStatus.class,
-                "unknownNodes", ImmutableList.class);
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            throw new ExceptionInInitializerError(e);
-        }
+    protected AbstractEffectiveDocumentedNodeWithStatus(final A argument, final @NonNull D declared,
+            final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        this.argument = argument;
+        this.declared = requireNonNull(declared);
+        this.substatements = requireNonNull(substatements);
     }
 
-    private final @NonNull Status status;
-
-    @SuppressWarnings("unused")
-    private volatile ImmutableList<UnknownSchemaNode> unknownNodes;
-
-    /**
-     * Constructor.
-     *
-     * @param ctx
-     *            context of statement.
-     */
-    protected AbstractEffectiveDocumentedNodeWithStatus(final StmtContext<A, D, ?> ctx) {
-        super(ctx);
-        status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
+    @Override
+    public final A argument() {
+        return argument;
     }
 
     @Override
-    public final Status getStatus() {
-        return status;
+    public final @NonNull D getDeclared() {
+        return declared;
     }
 
     @Override
-    public final Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
-        final ImmutableList<UnknownSchemaNode> existing =
-                (ImmutableList<UnknownSchemaNode>) UNKNOWN_NODES.getAcquire(this);
-        return existing != null ? existing : loadUnknownSchemaNodes();
+    public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
+        return substatements;
     }
 
     @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);
-            }
-        }
+    public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
+        return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
+    }
+
+    @Override
+    public final Status getStatus() {
+        return findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
+    }
+
+    protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
+        return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
+    }
 
-        final ImmutableList<UnknownSchemaNode> computed = ImmutableList.copyOf(init);
-        final Object witness = UNKNOWN_NODES.compareAndExchangeRelease(this, null, computed);
-        return witness == null ? computed : (ImmutableList<UnknownSchemaNode>) witness;
+    protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
+        return effectiveSubstatements().stream()
+                .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
+                .findFirst().map(returnType::cast).orElse(null);
     }
 }