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.spi.meta;
10 import com.google.common.collect.Collections2;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
19 * Utility abstract base class for implementing declared statements.
21 * @param <A> Argument type.
23 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
24 private final @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements;
25 private final @NonNull StatementDefinition definition;
26 private final @NonNull StatementSource source;
28 private final A argument;
29 private final String rawArgument;
31 protected AbstractDeclaredStatement(final StmtContext<A, ?, ?> context) {
32 rawArgument = context.rawStatementArgument();
33 argument = context.getStatementArgument();
34 source = context.getStatementSource();
35 definition = context.getPublicDefinition();
37 * Perform an explicit copy, because Collections2.transform() is lazily transformed and retains pointer to
38 * original collection, which may contains references to mutable context.
40 substatements = ImmutableList.copyOf(Collections2.transform(context.declaredSubstatements(),
41 StmtContext::buildDeclared));
45 * Find first declared substatement of a particular type.
47 * @param type {@link DeclaredStatement} type
48 * @return First effective substatement, or null if no match is found.
49 * @deprecated Use {@link #findFirstDeclaredSubstatement(Class)} instead.
52 protected final <S extends DeclaredStatement<?>> S firstDeclared(final Class<S> type) {
53 return findFirstDeclaredSubstatement(type).orElse(null);
57 public String rawArgument() {
67 public StatementDefinition statementDefinition() {
72 public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
77 public StatementSource getStatementSource() {
82 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
85 * @param type {@link DeclaredStatement} type
86 * @return Collection of statements, which were explicitly declared in source of model.
87 * @throws NullPointerException if {@code type} is null
88 * @deprecated Use {@link #declaredSubstatements(Class)} instead.
91 protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
92 return declaredSubstatements(type);