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.base.Predicates;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.FluentIterable;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
21 * Utility abstract base class for implementing declared statements.
24 * @param <A> Argument type.
26 public abstract class AbstractDeclaredStatement<A> implements DeclaredStatement<A> {
29 private final A argument;
30 private final String rawArgument;
31 private final ImmutableList<? extends DeclaredStatement<?>> substatements;
32 private final StatementDefinition definition;
33 private final StatementSource source;
35 protected AbstractDeclaredStatement(StmtContext<A,?,?> context) {
36 rawArgument = context.rawStatementArgument();
37 argument = context.getStatementArgument();
38 source = context.getStatementSource();
39 definition = context.getPublicDefinition();
41 * Collections.transform could not be used here, since it is lazily
42 * transformed and retains pointer to original collection, which may
43 * contains references to mutable context.
45 * FluentIterable.tranform().toList() - actually performs transformation
46 * and creates immutable list from transformed results.
48 substatements = FluentIterable.from(context.declaredSubstatements()).transform(StmtContextUtils.buildDeclared()).toList();
51 protected final <S extends DeclaredStatement<?>> S firstDeclared(Class<S> type) {
52 return type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
56 public String rawArgument() {
66 public StatementDefinition statementDefinition() {
71 public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
76 public StatementSource getStatementSource() {
80 @SuppressWarnings("unchecked")
81 protected final <S extends DeclaredStatement<?>> Collection<? extends S> allDeclared(Class<S> type) {
82 return Collection.class.cast(Collections2.filter(substatements,Predicates.instanceOf(type)));