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