Remove superfluouse @NonNull
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / TopLevelJavaGeneratedType.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 com.google.common.collect.BiMap;
11 import com.google.common.collect.HashBiMap;
12 import java.util.Comparator;
13 import java.util.Iterator;
14 import java.util.Map.Entry;
15 import java.util.stream.Stream;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
18 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
19
20 /**
21  * This class tracks types generated into a single Java compilation unit (file) and manages imports of that compilation
22  * unit. Since we are generating only public classes, this is synonymous to a top-level Java type.
23  */
24 @NonNullByDefault
25 final class TopLevelJavaGeneratedType extends AbstractJavaGeneratedType {
26     private final BiMap<JavaTypeName, String> importedTypes = HashBiMap.create();
27
28     TopLevelJavaGeneratedType(final GeneratedType genType) {
29         super(genType);
30     }
31
32     @Override
33     String localTypeName(final JavaTypeName type) {
34         // Locally-anchored type, this is simple: just strip the first local name component and concat the others
35         final Iterator<String> it = type.localNameComponents().iterator();
36         it.next();
37
38         final StringBuilder sb = new StringBuilder().append(it.next());
39         while (it.hasNext()) {
40             sb.append('.').append(it.next());
41         }
42         return sb.toString();
43     }
44
45     @Override
46     boolean importCheckedType(final JavaTypeName type) {
47         if (importedTypes.containsKey(type)) {
48             return true;
49         }
50         final String simpleName = type.simpleName();
51         if (importedTypes.containsValue(simpleName)) {
52             return false;
53         }
54         importedTypes.put(type, simpleName);
55         return true;
56     }
57
58     Stream<JavaTypeName> imports() {
59         return importedTypes.entrySet().stream().filter(this::needsExplicitImport).map(Entry::getKey)
60                 .sorted(Comparator.comparing(JavaTypeName::toString));
61     }
62
63     private boolean needsExplicitImport(final Entry<JavaTypeName, String> entry) {
64         final JavaTypeName name = entry.getKey();
65
66         if (!getName().packageName().equals(name.packageName())) {
67             // Different package: need to import it
68             return true;
69         }
70
71         if (!name.immediatelyEnclosingClass().isPresent()) {
72             // This a top-level class import, we can skip it
73             return false;
74         }
75
76         // This is a nested class, we need to spell it out if the import entry points to the simple name
77         return entry.getValue().equals(name.simpleName());
78     }
79 }