Fixed Eclipse specific compilation error related to generics
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveStatementBase.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.stmt.rfc6020.effective;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29
30 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
31
32     private static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
33             new Predicate<StmtContext<?,?,?>>() {
34         @Override
35         public boolean apply(final StmtContext<?, ?, ?> input) {
36             return input.isSupportedToBuildEffective();
37         }
38     };
39
40     private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
41             new Predicate<StmtContext<?,?,?>>() {
42         @Override
43         public boolean apply(final StmtContext<?, ?, ?> input) {
44             return StmtContextUtils.isUnknownStatement(input);
45         }
46     };
47
48     private final List<? extends EffectiveStatement<?, ?>> substatements;
49     private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
50
51     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
52         this(ctx, true);
53     }
54
55     /**
56      * Constructor.
57      *
58      * @param ctx
59      *            context of statement.
60      * @param buildUnknownSubstatements
61      *            if it is false, the unknown substatements are omitted from
62      *            build of effective substatements till the call of either
63      *            effectiveSubstatements or getOmittedUnknownSubstatements
64      *            method. The main purpose of this is to allow the build of
65      *            recursive extension definitions.
66      */
67     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
68
69         final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
70         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
71
72         for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
73             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
74                 substatementsInit.add(declaredSubstatement);
75                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
76                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
77                         .getEffectOfStatement());
78             } else {
79                 substatementsInit.add(declaredSubstatement);
80             }
81         }
82         substatementsInit.addAll(effectiveSubstatements);
83
84         Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
85                 IS_SUPPORTED_TO_BUILD_EFFECTIVE);
86         if (!buildUnknownSubstatements) {
87             this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
88                     IS_UNKNOWN_STATEMENT_CONTEXT));
89             substatementsToBuild = Collections2.filter(substatementsToBuild,
90                     Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
91         } else {
92             this.unknownSubstatementsToBuild = ImmutableList.of();
93         }
94
95         Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
96         this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
97     }
98
99     Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
100         Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
101         return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
102     }
103
104     @Override
105     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
106         throw new UnsupportedOperationException("Not implemented yet.");
107     }
108
109     @Override
110     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
111         throw new UnsupportedOperationException("Not implemented yet.");
112     }
113
114     @Override
115     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
116         if (unknownSubstatementsToBuild.isEmpty()) {
117             return substatements;
118         } else {
119             return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
120         }
121     }
122
123     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
124         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
125                 Predicates.instanceOf(type));
126         return possible.isPresent() ? type.cast(possible.get()) : null;
127     }
128
129     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
130         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
131                 Predicates.instanceOf(type));
132         return possible.isPresent() ? type.cast(possible.get()) : null;
133     }
134
135     @SuppressWarnings("unchecked")
136     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
137         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
138     }
139
140     protected final <T> T firstSubstatementOfType(final Class<T> type) {
141         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
142                 Predicates.instanceOf(type));
143         return possible.isPresent() ? type.cast(possible.get()) : null;
144     }
145
146     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
147         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
148                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
149         return possible.isPresent() ? returnType.cast(possible.get()) : null;
150     }
151
152     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
153         return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
154     }
155 }