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.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;
26 * Base Java file template. Contains a non-null type and imports which the generated code refers to.
28 class JavaFileTemplate {
29 // Hidden to well-define operations
30 private final Map<String, JavaTypeName> importMap = new HashMap<>();
32 protected final GeneratedType type;
34 JavaFileTemplate(final GeneratedType type) {
35 this.type = requireNonNull(type);
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()) {
45 final GeneratedTransferObject parent = gto.getSuperType();
46 return parent != null ? findProperty(parent, name) : null;
49 static final Restrictions getRestrictions(final Type type) {
50 if (type instanceof ConcreteType) {
51 return ((ConcreteType)type).getRestrictions();
53 if (type instanceof GeneratedTransferObject) {
54 return ((GeneratedTransferObject)type).getRestrictions();
59 final String generateImportBlock() {
60 return importMap.entrySet().stream()
61 .filter(e -> isDefaultVisible(e.getValue()))
63 return e1.getValue().toString().compareTo(e2.getValue().toString());
65 .map(e -> "import " + e.getValue() + ";\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 Class<?> cls) {
79 final JavaTypeName name = JavaTypeName.create(cls);
80 importMap.put(name.simpleName(), name);
83 final void addImports(final JavaFileTemplate from) {
84 importMap.putAll(from.importMap);
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());
93 private boolean isDefaultVisible(final JavaTypeName name) {
94 return !hasSamePackage(name) || !isLocalInnerClass(name);
98 * Checks if packages of generated type and imported type is the same
100 * @param importedTypePackageName the package name of imported type
101 * @return true if the packages are the same false otherwise
103 private boolean hasSamePackage(final JavaTypeName name) {
104 return type.getPackageName().equals(name.packageName());