Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / rt / AbstractCompositeRuntimeType.java
1 /*
2  * Copyright (c) 2021 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.rt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Functions;
13 import com.google.common.collect.ImmutableCollection;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
18 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
19 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
20 import org.opendaylight.mdsal.binding.runtime.api.GeneratedRuntimeType;
21 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
25
26 abstract class AbstractCompositeRuntimeType<S extends EffectiveStatement<?, ?>>
27         extends AbstractRuntimeType<S, GeneratedType> implements CompositeRuntimeType {
28     private final ImmutableMap<JavaTypeName, GeneratedRuntimeType> byClass;
29     private final ImmutableMap<QName, RuntimeType> bySchemaTree;
30
31     AbstractCompositeRuntimeType(final GeneratedType bindingType, final S statement, final List<RuntimeType> children) {
32         super(bindingType, statement);
33
34         byClass = children.stream()
35             .filter(GeneratedRuntimeType.class::isInstance)
36             .map(GeneratedRuntimeType.class::cast)
37             .collect(ImmutableMap.toImmutableMap(GeneratedRuntimeType::getIdentifier, Functions.identity()));
38
39         // Note: this may be over-sized, but we typically deal with schema tree statements, hence it is kind of accurate
40         final var builder = ImmutableMap.<QName, RuntimeType>builderWithExpectedSize(children.size());
41         for (var child : children) {
42             final var stmt = child.statement();
43             if (stmt instanceof SchemaTreeEffectiveStatement) {
44                 builder.put(((SchemaTreeEffectiveStatement<?>)stmt).argument(), child);
45             }
46         }
47         bySchemaTree = builder.build();
48     }
49
50     @Override
51     public final RuntimeType schemaTreeChild(final QName qname) {
52         return bySchemaTree.get(requireNonNull(qname));
53     }
54
55     @Override
56     public final GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
57         return byClass.get(requireNonNull(typeName));
58     }
59
60     final @NonNull ImmutableCollection<RuntimeType> schemaTreeChildren() {
61         return bySchemaTree.values();
62     }
63 }