2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
10 import static com.google.common.base.Verify.verify;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 * A strategy for matching an {@link EffectiveStatement} to an {@link AbstractExplicitGenerator}.
23 abstract class MatchStrategy {
25 * Strategy matching on exact statement identity. We use this initially as it works for non-weird cases.
27 private static final class Identity extends MatchStrategy {
28 static final @NonNull Identity INSTANCE = new Identity();
31 AbstractExplicitGenerator<?> findGenerator(final EffectiveStatement<?, ?> needle,
32 final Iterable<? extends Generator> haystack) {
33 for (Generator gen : haystack) {
34 if (gen instanceof AbstractExplicitGenerator) {
35 final AbstractExplicitGenerator<?> ret = (AbstractExplicitGenerator<?>) gen;
36 if (needle == ret.statement()) {
46 * Strategy matching on exact QName argument. Used when we are switching along the 'augments' axis.
48 private static class OnQName extends MatchStrategy {
49 static final @NonNull OnQName INSTANCE = new OnQName();
52 final AbstractExplicitGenerator<?> findGenerator(final EffectiveStatement<?, ?> needle,
53 final Iterable<? extends Generator> haystack) {
54 final Object arg = needle.argument();
55 verify(arg instanceof QName, "Unexpected argument %s in %s", arg, needle);
56 return findGenerator((QName) arg, haystack);
59 AbstractExplicitGenerator<?> findGenerator(final QName needle, final Iterable<? extends Generator> haystack) {
60 for (Generator gen : haystack) {
61 if (gen instanceof AbstractExplicitGenerator) {
62 final AbstractExplicitGenerator<?> ret = (AbstractExplicitGenerator<?>) gen;
63 if (needle.equals(ret.statement().argument())) {
73 * Strategy matching on exact QName argument rehosted to a particular grouping. This is how we can locate a node
74 * inlined via a 'uses' of that grouping.
76 private static final class Grouping extends OnQName {
77 private final @NonNull QNameModule module;
79 Grouping(final GroupingGenerator grouping) {
80 module = grouping.statement().argument().getModule();
84 AbstractExplicitGenerator<?> findGenerator(final QName needle, final Iterable<? extends Generator> haystack) {
85 return super.findGenerator(needle.bindTo(module), haystack);
89 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
90 return super.addToStringAttributes(helper).add("module", module);
94 private MatchStrategy() {
98 static @NonNull MatchStrategy augment() {
99 return OnQName.INSTANCE;
102 static @NonNull MatchStrategy grouping(final GroupingGenerator grouping) {
103 return new Grouping(grouping);
106 static @NonNull MatchStrategy identity() {
107 return Identity.INSTANCE;
110 abstract @Nullable AbstractExplicitGenerator<?> findGenerator(EffectiveStatement<?, ?> needle,
111 Iterable<? extends Generator> haystack);
114 public final String toString() {
115 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
118 ToStringHelper addToStringAttributes(final ToStringHelper helper) {