Sonar issues clean-up
[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
88         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
89                 .declaredSubstatements();
90         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
91             if (producesDeclared(subStmtContext,types[startIndex])) {
92                 if (startIndex + 1 == types.length) {
93                     return subStmtContext;
94                 } else {
95                     return findFirstDeclaredSubstatement(subStmtContext,
96                             ++startIndex, types);
97                 }
98             }
99         }
100         return null;
101     }
102
103     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
104             StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType,
105             int sublevel) {
106         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
107                 .declaredSubstatements();
108         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
109             if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) {
110                 return subStmtContext;
111             } else {
112                 if (sublevel > 1) {
113                     StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
114                             subStmtContext, declaredType, --sublevel);
115                     if (result != null) {
116                         return result;
117                     }
118                 }
119             }
120         }
121         return null;
122     }
123
124     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
125             StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType) {
126
127         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
128                 .declaredSubstatements();
129
130         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
131             if (producesDeclared(subStmtContext,declaredType)) {
132                 return subStmtContext;
133             } else {
134                 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(
135                         subStmtContext, declaredType);
136                 if (result != null) {
137                     return result;
138                 }
139
140             }
141         }
142         return null;
143     }
144
145     public static final boolean producesDeclared(StmtContext<?, ?, ?> ctx,
146             Class<? extends DeclaredStatement<?>> type) {
147         return type.isAssignableFrom(ctx.getPublicDefinition()
148                 .getDeclaredRepresentationClass());
149     }
150 }