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
9 package org.opendaylight.yangtools.yang.parser.spi.meta;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
20 * Utility abstract base class for implementing declared statements.
22 * @param <A> Argument type.
24 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
25 private final A argument;
26 private final String rawArgument;
27 private final ImmutableList<? extends DeclaredStatement<?>> substatements;
28 private final StatementDefinition definition;
29 private final StatementSource source;
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() {
68 public StatementDefinition statementDefinition() {
74 public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
80 public StatementSource getStatementSource() {
85 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
88 * @param type {@link DeclaredStatement} type
89 * @return Collection of statements, which were explicitly declared in source of model.
90 * @throws NullPointerException if {@code type} is null
91 * @deprecated Use {@link #declaredSubstatements(Class)} instead.
94 protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(final Class<S> type) {
95 return declaredSubstatements(type);