Add @NonNull in DataObjects' default method signatures
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / AbstractJavaGeneratedType.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.java.api.generator;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Iterables;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.mdsal.binding.model.api.Enumeration;
24 import org.opendaylight.mdsal.binding.model.api.Enumeration.Pair;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
26 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
27 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
28 import org.opendaylight.mdsal.binding.model.api.Type;
29 import org.opendaylight.mdsal.binding.model.api.WildcardType;
30
31 /**
32  * Abstract class representing a generated type, either top-level or nested. It takes care of tracking references
33  * to other Java types and resolving them as best as possible. This class is NOT thread-safe.
34  *
35  * @author Robert Varga
36  */
37 @NonNullByDefault
38 abstract class AbstractJavaGeneratedType {
39     private final Map<JavaTypeName, @Nullable String> nameCache = new HashMap<>();
40     private final ImmutableMap<String, NestedJavaGeneratedType> enclosedTypes;
41     private final ImmutableSet<String> conflictingNames;
42
43     private final JavaTypeName name;
44
45     AbstractJavaGeneratedType(final GeneratedType genType) {
46         name = genType.getIdentifier();
47         final Builder<String, NestedJavaGeneratedType> b = ImmutableMap.builder();
48         for (GeneratedType type : Iterables.concat(genType.getEnclosedTypes(), genType.getEnumerations())) {
49             b.put(type.getIdentifier().simpleName(), new NestedJavaGeneratedType(this, type));
50         }
51         enclosedTypes = b.build();
52
53         final Set<String> cb = new HashSet<>();
54         if (genType instanceof Enumeration) {
55             ((Enumeration) genType).getValues().stream().map(Pair::getMappedName).forEach(cb::add);
56         }
57         // TODO: perhaps we can do something smarter to actually access the types
58         collectAccessibleTypes(cb, genType);
59
60         conflictingNames = ImmutableSet.copyOf(cb);
61     }
62
63     private void collectAccessibleTypes(final Set<String> set, final GeneratedType type) {
64         for (Type impl : type.getImplements()) {
65             if (impl instanceof GeneratedType) {
66                 final GeneratedType genType = (GeneratedType) impl;
67                 for (GeneratedType inner : Iterables.concat(genType.getEnclosedTypes(), genType.getEnumerations())) {
68                     set.add(inner.getIdentifier().simpleName());
69                 }
70                 collectAccessibleTypes(set, genType);
71             }
72         }
73     }
74
75     final JavaTypeName getName() {
76         return name;
77     }
78
79     final String getSimpleName() {
80         return name.simpleName();
81     }
82
83     private String annotateReference(final String ref, final Type type, final String annotation) {
84         return type instanceof ParameterizedType ? getReferenceString(annotate(ref, annotation), type,
85                 ((ParameterizedType) type).getActualTypeArguments())
86                 : annotate(ref, annotation).toString();
87     }
88
89     final String getFullyQualifiedReference(final Type type, final String annotation) {
90         return annotateReference(type.getFullyQualifiedName(), type ,annotation);
91     }
92
93     final String getReferenceString(final Type type) {
94         final String ref = getReferenceString(type.getIdentifier());
95         return type instanceof ParameterizedType ? getReferenceString(new StringBuilder(ref), type,
96             ((ParameterizedType) type).getActualTypeArguments())
97                 : ref;
98     }
99
100     final String getReferenceString(final Type type, final String annotation) {
101         // Package-private method, all callers who would be passing an empty array are bound to the more special
102         // case above, hence we know annotations.length >= 1
103         final String ref = getReferenceString(type.getIdentifier());
104         return annotateReference(ref, type, annotation);
105     }
106
107     private String getReferenceString(final StringBuilder sb, final Type type, final @NonNull Type[] arguments) {
108         if (arguments.length == 0) {
109             return sb.append("<?>").toString();
110         }
111
112         sb.append('<');
113         for (int i = 0; i < arguments.length; i++) {
114             final Type arg = arguments[i];
115             if (arg instanceof WildcardType) {
116                 sb.append("? extends ");
117             }
118             sb.append(getReferenceString(arg));
119             if (i != arguments.length - 1) {
120                 sb.append(", ");
121             }
122         }
123         return sb.append('>').toString();
124     }
125
126     final String getReferenceString(final JavaTypeName type) {
127         if (type.packageName().isEmpty()) {
128             // This is a packageless primitive type, refer to it directly
129             return type.simpleName();
130         }
131
132         // Self-reference, return simple name
133         if (name.equals(type)) {
134             return name.simpleName();
135         }
136
137         // Fast path: we have already resolved how to refer to this type
138         final String existing = nameCache.get(type);
139         if (existing != null) {
140             return existing;
141         }
142
143         // Fork based on whether the class is in this compilation unit, package or neither
144         final String result;
145         if (name.topLevelClass().equals(type.topLevelClass())) {
146             result = localTypeName(type);
147         } else if (name.packageName().equals(type.packageName())) {
148             result = packageTypeName(type);
149         } else {
150             result = foreignTypeName(type);
151         }
152
153         nameCache.put(type, result);
154         return result;
155     }
156
157     final NestedJavaGeneratedType getEnclosedType(final JavaTypeName type) {
158         return requireNonNull(enclosedTypes.get(type.simpleName()));
159     }
160
161     final boolean checkAndImportType(final JavaTypeName type) {
162         // We can import the type only if it does not conflict with us or our immediately-enclosed types
163         final String simpleName = type.simpleName();
164         return !simpleName.equals(getSimpleName()) && !enclosedTypes.containsKey(simpleName)
165                 && !conflictingNames.contains(simpleName) && importCheckedType(type);
166     }
167
168     abstract boolean importCheckedType(JavaTypeName type);
169
170     abstract String localTypeName(JavaTypeName type);
171
172     private String foreignTypeName(final JavaTypeName type) {
173         return checkAndImportType(type) ? type.simpleName() : type.toString();
174     }
175
176     private String packageTypeName(final JavaTypeName type) {
177         // Try to anchor the top-level type and use a local reference
178         return checkAndImportType(type.topLevelClass()) ? type.localName() : type.toString();
179     }
180
181     private static StringBuilder annotate(final String ref, final String annotation) {
182         final StringBuilder sb = new StringBuilder();
183         final int dot = ref.lastIndexOf('.');
184         if (dot != -1) {
185             sb.append(ref, 0, dot + 1);
186         }
187         return sb.append('@').append(annotation).append(' ').append(ref, dot + 1, ref.length());
188     }
189 }