1f86de80d777717099c5e2c4b287ed09cace70b8
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
10 import java.util.Collection;
11 import com.google.common.base.Function;
12 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14
15 public final class StmtContextUtils {
16
17     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED = new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
18         @Override
19         public DeclaredStatement<?> apply(StmtContext<?, ?, ?> input) {
20             return input.buildDeclared();
21         }
22     };
23
24     private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE = new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
25         @Override
26         public EffectiveStatement<?, ?> apply(StmtContext<?, ?, ?> input) {
27             return input.buildEffective();
28         }
29     };
30
31     private StmtContextUtils() {
32         throw new UnsupportedOperationException("Utility class");
33     }
34
35     @SuppressWarnings("unchecked")
36     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
37         return Function.class.cast(BUILD_DECLARED);
38     }
39
40     @SuppressWarnings("unchecked")
41
42     public static final <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
43         return Function.class.cast(BUILD_EFFECTIVE);
44     }
45
46     @SuppressWarnings("unchecked")
47     public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
48             Iterable<? extends StmtContext<?, ?, ?>> contexts,
49             Class<DT> declaredType) {
50         for (StmtContext<?, ?, ?> ctx : contexts) {
51             if (producesDeclared(ctx, declaredType)) {
52                 return (AT) ctx.getStatementArgument();
53             }
54         }
55         return null;
56     }
57
58     @SuppressWarnings("unchecked")
59     public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
60             StmtContext<?, ?, ?> ctx, Class<DT> declaredType) {
61
62         if (producesDeclared(ctx, declaredType)) {
63             return (AT) ctx.getStatementArgument();
64         }
65
66         return null;
67     }
68
69     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatement(
70             StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType) {
71         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
72                 .declaredSubstatements();
73         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
74             if (producesDeclared(subStmtContext,declaredType)) {
75                 return subStmtContext;
76             }
77         }
78         return null;
79     }
80
81     public static final StmtContext<?, ?, ?> findFirstDeclaredSubstatement(
82             StmtContext<?, ?, ?> stmtContext, int startIndex, Class<? extends DeclaredStatement<?>>... types) {
83
84         if (startIndex >= types.length)
85             return null;
86
87         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
88                 .declaredSubstatements();
89         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
90             if (producesDeclared(subStmtContext,types[startIndex])) {
91                 if (startIndex + 1 == types.length) {
92                     return subStmtContext;
93                 } else {
94                     return findFirstDeclaredSubstatement(subStmtContext,
95                             ++startIndex, types);
96                 }
97             }
98         }
99         return null;
100     }
101
102     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
103             StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType,
104             int sublevel) {
105         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
106                 .declaredSubstatements();
107         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
108             if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) {
109                 return subStmtContext;
110             } else {
111                 if (sublevel > 1) {
112                     StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
113                             subStmtContext, declaredType, --sublevel);
114                     if (result != null) {
115                         return result;
116                     }
117                 }
118             }
119         }
120         return null;
121     }
122
123     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
124             StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType) {
125
126         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
127                 .declaredSubstatements();
128
129         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
130             if (producesDeclared(subStmtContext,declaredType)) {
131                 return subStmtContext;
132             } else {
133                 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(
134                         subStmtContext, declaredType);
135                 if (result != null) {
136                     return result;
137                 }
138
139             }
140         }
141         return null;
142     }
143
144     public static final boolean producesDeclared(StmtContext<?, ?, ?> ctx,
145             Class<? extends DeclaredStatement<?>> type) {
146         return type.isAssignableFrom(ctx.getPublicDefinition()
147                 .getDeclaredRepresentationClass());
148     }
149 }