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 java.util.Objects.requireNonNull;
12 import java.util.List;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.binding.model.api.Type;
16 import org.opendaylight.mdsal.binding.model.ri.Types;
18 abstract sealed class TypeReference {
19 private static final class Identityref extends TypeReference {
20 private final List<IdentityGenerator> referencedGenerators;
22 private Type returnType;
24 Identityref(final List<IdentityGenerator> referencedGenerators) {
25 this.referencedGenerators = requireNonNull(referencedGenerators);
29 Type methodReturnType(final TypeBuilderFactory builderFactory) {
30 if (returnType == null) {
31 // FIXME: This deals only with RFC6020 semantics. In order to deal with full RFC7950 semantics, we need
32 // to analyze all the types and come up with the lowest-common denominator and use that as the
33 // return type. We also need to encode restrictions, so that builder generator ends up checking
34 // identities being passed -- because the identities may be completely unrelated, in which case
35 // we cannot generate type-safe code.
36 returnType = referencedGenerators.stream()
37 .map(gen -> gen.getGeneratedType(builderFactory))
45 // Note: this is exposed only for legacy naming handling
46 abstract static sealed class Leafref extends TypeReference {
52 static final class ResolvedLeafref extends Leafref {
53 private final AbstractTypeObjectGenerator<?, ?> referencedGenerator;
55 private ResolvedLeafref(final AbstractTypeObjectGenerator<?, ?> referencedGenerator) {
56 this.referencedGenerator = requireNonNull(referencedGenerator);
60 Type methodReturnType(final TypeBuilderFactory builderFactory) {
61 return referencedGenerator.methodReturnElementType(builderFactory);
65 private static final class UnresolvedLeafref extends Leafref {
66 static final @NonNull UnresolvedLeafref INSTANCE = new UnresolvedLeafref();
68 private UnresolvedLeafref() {
73 Type methodReturnType(final TypeBuilderFactory builderFactory) {
74 return Types.objectType();
78 private static final class FailedLeafref extends Leafref {
79 private final IllegalArgumentException cause;
81 FailedLeafref(final IllegalArgumentException cause) {
82 this.cause = requireNonNull(cause);
86 Type methodReturnType(final TypeBuilderFactory builderFactory) {
87 throw new UnsupportedOperationException("Cannot ascertain type", cause);
91 static @NonNull TypeReference leafRef(final @Nullable AbstractTypeObjectGenerator<?, ?> referencedGenerator) {
92 return referencedGenerator == null ? UnresolvedLeafref.INSTANCE : new ResolvedLeafref(referencedGenerator);
95 static @NonNull TypeReference leafRef(final @NonNull IllegalArgumentException cause) {
96 return new FailedLeafref(cause);
99 static @NonNull TypeReference identityRef(final List<IdentityGenerator> referencedGenerators) {
100 return new Identityref(referencedGenerators);
103 abstract @NonNull Type methodReturnType(@NonNull TypeBuilderFactory builderFactory);