/* * 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.spi.meta; import java.util.Collection; import com.google.common.base.Function; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; public final class StmtContextUtils { private static final Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { @Override public DeclaredStatement apply(StmtContext input) { return input.buildDeclared(); } }; private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { @Override public EffectiveStatement apply(StmtContext input) { return input.buildEffective(); } }; private StmtContextUtils() { throw new UnsupportedOperationException("Utility class"); } @SuppressWarnings("unchecked") public static > Function, D> buildDeclared() { return Function.class.cast(BUILD_DECLARED); } @SuppressWarnings("unchecked") public static final > Function, E> buildEffective() { return Function.class.cast(BUILD_EFFECTIVE); } @SuppressWarnings("unchecked") public static final > AT firstAttributeOf( Iterable> contexts, Class
declaredType) { for (StmtContext ctx : contexts) { if (producesDeclared(ctx, declaredType)) { return (AT) ctx.getStatementArgument(); } } return null; } @SuppressWarnings("unchecked") public static final > AT firstAttributeOf( StmtContext ctx, Class
declaredType) { if (producesDeclared(ctx, declaredType)) { return (AT) ctx.getStatementArgument(); } return null; } public static final
> StmtContext findFirstDeclaredSubstatement( StmtContext stmtContext, Class
declaredType) { Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } } return null; } public static final StmtContext findFirstDeclaredSubstatement( StmtContext stmtContext, int startIndex, Class>... types) { if (startIndex >= types.length) { return null; } Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (producesDeclared(subStmtContext,types[startIndex])) { if (startIndex + 1 == types.length) { return subStmtContext; } else { return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types); } } } return null; } public static final
> StmtContext findFirstDeclaredSubstatementOnSublevel( StmtContext stmtContext, Class
declaredType, int sublevel) { Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } else { if (sublevel > 1) { StmtContext result = findFirstDeclaredSubstatementOnSublevel( subStmtContext, declaredType, --sublevel); if (result != null) { return result; } } } } return null; } public static final
> StmtContext findDeepFirstDeclaredSubstatement( StmtContext stmtContext, Class
declaredType) { Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } else { StmtContext result = findDeepFirstDeclaredSubstatement( subStmtContext, declaredType); if (result != null) { return result; } } } return null; } public static final boolean producesDeclared(StmtContext ctx, Class> type) { return type.isAssignableFrom(ctx.getPublicDefinition() .getDeclaredRepresentationClass()); } }