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