2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.java.api.generator;
10 import static java.util.Objects.requireNonNull;
12 import java.util.HashMap;
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;
25 * Base Java file template. Contains a non-null type and imports which the generated code refers to.
27 class JavaFileTemplate {
28 // Hidden to well-define operations
29 private final Map<String, String> importMap = new HashMap<>();
31 protected final GeneratedType type;
33 JavaFileTemplate(final GeneratedType type) {
34 this.type = requireNonNull(type);
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()) {
44 final GeneratedTransferObject parent = gto.getSuperType();
45 return parent != null ? findProperty(parent, name) : null;
48 static final Restrictions getRestrictions(final Type type) {
49 if (type instanceof ConcreteType) {
50 return ((ConcreteType)type).getRestrictions();
52 if (type instanceof GeneratedTransferObject) {
53 return ((GeneratedTransferObject)type).getRestrictions();
58 final String generateImportBlock() {
59 return importMap.entrySet().stream()
60 .filter(e -> isDefaultVisible(e.getValue()))
62 final int cmp = e1.getValue().compareTo(e2.getValue());
63 return cmp != 0 ? cmp : e1.getKey().compareTo(e2.getKey());
65 .map(e -> "import " + e.getValue() + "." + e.getKey() + ";\n")
66 .collect(Collectors.joining());
69 final String importedName(final Type intype) {
70 GeneratorUtil.putTypeIntoImports(type, intype, importMap);
71 return GeneratorUtil.getExplicitType(type, intype, importMap);
74 final String importedName(final Class<?> cls) {
75 return importedName(Types.typeForClass(cls));
78 final void addImport(final String className, final String packageName) {
79 importMap.put(className, packageName);
82 final void addImports(final JavaFileTemplate from) {
83 importMap.putAll(from.importMap);
86 // Exposed for BuilderTemplate
87 boolean isLocalInnerClass(final String importedTypePackageName) {
88 return type.getFullyQualifiedName().equals(importedTypePackageName);
91 private boolean isDefaultVisible(final String prefix) {
92 return !hasSamePackage(prefix) || !isLocalInnerClass(prefix);
96 * Checks if packages of generated type and imported type is the same
98 * @param importedTypePackageName the package name of imported type
99 * @return true if the packages are the same false otherwise
101 private boolean hasSamePackage(final String importedTypePackageName) {
102 return type.getPackageName().equals(importedTypePackageName);