Integrate JavaTypeName as Identifier
[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.JavaTypeName;
21 import org.opendaylight.mdsal.binding.model.api.Restrictions;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.mdsal.binding.model.util.Types;
24
25 /**
26  * Base Java file template. Contains a non-null type and imports which the generated code refers to.
27  */
28 class JavaFileTemplate {
29     // Hidden to well-define operations
30     private final Map<String, JavaTypeName> importMap = new HashMap<>();
31
32     protected final GeneratedType type;
33
34     JavaFileTemplate(final GeneratedType type) {
35         this.type = requireNonNull(type);
36     }
37
38     final GeneratedProperty findProperty(final GeneratedTransferObject gto, final String name) {
39         final Optional<GeneratedProperty> optProp = gto.getProperties().stream()
40                 .filter(prop -> prop.getName().equals(name)).findFirst();
41         if (optProp.isPresent()) {
42             return optProp.get();
43         }
44
45         final GeneratedTransferObject parent = gto.getSuperType();
46         return parent != null ? findProperty(parent, name) : null;
47     }
48
49     static final Restrictions getRestrictions(final Type type) {
50         if (type instanceof ConcreteType) {
51             return ((ConcreteType)type).getRestrictions();
52         }
53         if (type instanceof GeneratedTransferObject) {
54             return ((GeneratedTransferObject)type).getRestrictions();
55         }
56         return null;
57     }
58
59     final String generateImportBlock() {
60         return importMap.entrySet().stream()
61                 .filter(e -> isDefaultVisible(e.getValue()))
62                 .sorted((e1, e2) -> {
63                     return e1.getValue().toString().compareTo(e2.getValue().toString());
64                 })
65                 .map(e -> "import " + e.getValue() + ";\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 Class<?> cls) {
79         final JavaTypeName name = JavaTypeName.create(cls);
80         importMap.put(name.simpleName(), name);
81     }
82
83     final void addImports(final JavaFileTemplate from) {
84         importMap.putAll(from.importMap);
85     }
86
87     // Exposed for BuilderTemplate
88     boolean isLocalInnerClass(final JavaTypeName name) {
89         final Optional<JavaTypeName> optEnc = name.immediatelyEnclosingClass();
90         return optEnc.isPresent() && type.getIdentifier().equals(optEnc.get());
91     }
92
93     private boolean isDefaultVisible(final JavaTypeName name) {
94         return !hasSamePackage(name) || !isLocalInnerClass(name);
95     }
96
97     /**
98      * Checks if packages of generated type and imported type is the same
99      *
100      * @param importedTypePackageName the package name of imported type
101      * @return true if the packages are the same false otherwise
102      */
103     private boolean hasSamePackage(final JavaTypeName name) {
104         return type.getPackageName().equals(name.packageName());
105     }
106 }