/**
* 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.stmt.rfc6020.effective;
import java.util.Collections;
import java.util.NoSuchElementException;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import com.google.common.collect.Collections2;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import java.util.LinkedList;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
import com.google.common.collect.FluentIterable;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
import com.google.common.collect.ImmutableList;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import java.util.Collection;
import java.util.Map;
import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
abstract public class EffectiveStatementBase>
implements EffectiveStatement {
private final StmtContext stmtCtx;
private final ImmutableList extends EffectiveStatement, ?>> substatements;
private final StatementSource statementSource;
private final StatementDefinition statementDefinition;
private D declaredInstance;
private final A argument;
public EffectiveStatementBase(StmtContext ctx) {
this.stmtCtx = ctx;
this.statementDefinition = ctx.getPublicDefinition();
this.argument = ctx.getStatementArgument();
this.statementSource = ctx.getStatementSource();
Collection> declaredSubstatements = ctx
.declaredSubstatements();
Collection> effectiveSubstatements = ctx
.effectiveSubstatements();
Collection> substatements = new LinkedList>();
substatements.addAll(declaredSubstatements);
substatements.addAll(effectiveSubstatements);
this.substatements = FluentIterable.from(substatements)
.transform(StmtContextUtils.buildEffective()).toList();
}
@Override
public StatementDefinition statementDefinition() {
return statementDefinition;
}
@Override
public A argument() {
return argument;
}
@Override
public StatementSource getStatementSource() {
return statementSource;
}
@Override
public D getDeclared() {
if (declaredInstance == null) {
declaredInstance = stmtCtx.buildDeclared();
}
return declaredInstance;
}
// public > V
// get(
@Override
public > V get(
Class namespace, K identifier) {
return stmtCtx.getFromNamespace(namespace, identifier);
}
@Override
public > Map getAll(
Class namespace) {
return (Map) stmtCtx.getAllFromNamespace(namespace);
}
@Override
public Collection extends EffectiveStatement, ?>> effectiveSubstatements() {
return substatements;
}
public StmtContext getStatementContext() {
return stmtCtx;
}
protected final > S firstEffective(
Class type) {
S result = null;
try {
result = type.cast(Iterables.find(substatements,
Predicates.instanceOf(type)));
} catch (NoSuchElementException e) {
result = null;
}
return result;
}
@SuppressWarnings("unchecked")
protected final > Collection extends S> allEffective(
Class type) {
Collection extends S> result = null;
try {
result = Collection.class.cast(Collections2.filter(substatements,
Predicates.instanceOf(type)));
} catch (NoSuchElementException e) {
result = Collections.EMPTY_LIST;
}
return result;
}
protected final S firstSchemaNode(Class type) {
S result = null;
try {
result = type.cast(Iterables.find(substatements,
Predicates.instanceOf(type)));
} catch (NoSuchElementException e) {
result = null;
}
return result;
}
@SuppressWarnings("unchecked")
protected final Collection extends S> allSchemaNodes(
Class type) {
Collection extends S> result = null;
try {
result = Collection.class.cast(Collections2.filter(substatements,
Predicates.instanceOf(type)));
} catch (NoSuchElementException e) {
result = Collections.EMPTY_LIST;
}
return result;
}
}