Split out JavaFileTemplate
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / JavaFileTemplate.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 java.util.HashMap;
13 import java.util.Map;
14 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.Restrictions;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.mdsal.binding.model.util.Types;
23
24 /**
25  * Base Java file template. Contains a non-null type and imports which the generated code refers to.
26  */
27 class JavaFileTemplate {
28     // Hidden to well-define operations
29     private final Map<String, String> importMap = new HashMap<>();
30
31     protected final GeneratedType type;
32
33     JavaFileTemplate(final GeneratedType type) {
34         this.type = requireNonNull(type);
35     }
36
37     final GeneratedProperty findProperty(final GeneratedTransferObject gto, final String name) {
38         final Optional<GeneratedProperty> optProp = gto.getProperties().stream()
39                 .filter(prop -> prop.getName().equals(name)).findFirst();
40         if (optProp.isPresent()) {
41             return optProp.get();
42         }
43
44         final GeneratedTransferObject parent = gto.getSuperType();
45         return parent != null ? findProperty(parent, name) : null;
46     }
47
48     static final Restrictions getRestrictions(final Type type) {
49         if (type instanceof ConcreteType) {
50             return ((ConcreteType)type).getRestrictions();
51         }
52         if (type instanceof GeneratedTransferObject) {
53             return ((GeneratedTransferObject)type).getRestrictions();
54         }
55         return null;
56     }
57
58     final String generateImportBlock() {
59         return importMap.entrySet().stream()
60                 .filter(e -> isDefaultVisible(e.getValue()))
61                 .sorted((e1, e2) -> {
62                     final int cmp = e1.getValue().compareTo(e2.getValue());
63                     return cmp != 0 ? cmp : e1.getKey().compareTo(e2.getKey());
64                 })
65                 .map(e -> "import " + e.getValue() + "." + e.getKey() + ";\n")
66                 .collect(Collectors.joining());
67     }
68
69     final String importedName(final Type intype) {
70         GeneratorUtil.putTypeIntoImports(type, intype, importMap);
71         return GeneratorUtil.getExplicitType(type, intype, importMap);
72     }
73
74     final String importedName(final Class<?> cls) {
75         return importedName(Types.typeForClass(cls));
76     }
77
78     final void addImport(final String className, final String packageName) {
79         importMap.put(className, packageName);
80     }
81
82     final void addImports(final JavaFileTemplate from) {
83         importMap.putAll(from.importMap);
84     }
85
86     // Exposed for BuilderTemplate
87     boolean isLocalInnerClass(final String importedTypePackageName) {
88         return type.getFullyQualifiedName().equals(importedTypePackageName);
89     }
90
91     private boolean isDefaultVisible(final String prefix) {
92         return !hasSamePackage(prefix) || !isLocalInnerClass(prefix);
93     }
94
95     /**
96      * Checks if packages of generated type and imported type is the same
97      *
98      * @param importedTypePackageName the package name of imported type
99      * @return true if the packages are the same false otherwise
100      */
101     private boolean hasSamePackage(final String importedTypePackageName) {
102         return type.getPackageName().equals(importedTypePackageName);
103     }
104 }