Remove duplicate documentation
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AugmentResolver.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.mdsal.binding.generator.impl.reactor;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.ArrayDeque;
13 import java.util.Deque;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
18
19 /**
20  * Utility to resolve instantiated {@code augment} statements to their {@link AbstractAugmentGenerator} counterparts.
21  * This is essentially a stack of {@link AbstractCompositeGenerator}s which should be examined.
22  */
23 final class AugmentResolver implements Mutable {
24     private final Deque<AbstractCompositeGenerator<?, ?>> stack = new ArrayDeque<>();
25
26     void enter(final AbstractCompositeGenerator<?, ?> generator) {
27         stack.push(requireNonNull(generator));
28     }
29
30     void exit() {
31         stack.pop();
32     }
33
34     @NonNull AbstractAugmentGenerator getAugment(final AugmentEffectiveStatement statement) {
35         for (var generator : stack) {
36             final var found = findAugment(generator, statement);
37             if (found != null) {
38                 return found;
39             }
40         }
41         throw new IllegalStateException("Failed to resolve " + statement + " in " + stack);
42     }
43
44     private @Nullable AbstractAugmentGenerator findAugment(final AbstractCompositeGenerator<?, ?> generator,
45             final AugmentEffectiveStatement statement) {
46         for (var augment : generator.augments()) {
47             if (augment.matchesInstantiated(statement)) {
48                 return augment;
49             }
50         }
51         for (var grouping : generator.groupings()) {
52             final var found = findAugment(grouping, statement);
53             if (found != null) {
54                 return found;
55             }
56         }
57         return null;
58     }
59 }