Make JavaFileTemplate.importedName() identify all name collisions.
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorJavaFile.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 com.google.common.base.MoreObjects;
13 import com.google.common.collect.HashBasedTable;
14 import com.google.common.collect.Table;
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.function.Supplier;
20 import org.opendaylight.mdsal.binding.model.api.CodeGenerator;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Generates files with JAVA source codes for every specified type.
28  */
29 public final class GeneratorJavaFile {
30     public enum FileKind {
31         /**
32          * Transient file. It should be generated in target/generated-sources/ directory or similar.
33          */
34         TRANSIENT,
35         /**
36          * Persistent file. It should be generated in src/main/java/ directory or similar.
37          */
38         PERSISTENT,
39     }
40
41     private static final class GeneratorStringSupplier implements Supplier<String> {
42         private final CodeGenerator generator;
43         private final Type type;
44
45         GeneratorStringSupplier(final CodeGenerator generator, final Type type) {
46             this.generator = requireNonNull(generator);
47             this.type = requireNonNull(type);
48         }
49
50         @Override
51         public String get() {
52             return generator.generate(type);
53         }
54
55         @Override
56         public String toString() {
57             return MoreObjects.toStringHelper(this).add("generator", generator).add("type", type).toString();
58         }
59     }
60
61     private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
62
63     /**
64      * List of <code>CodeGenerator</code> instances.
65      */
66     private final List<CodeGenerator> generators = new ArrayList<>();
67
68     /**
69      * Set of <code>Type</code> instances for which the JAVA code is generated.
70      */
71     private final Collection<? extends Type> types;
72
73     /**
74      * Creates instance of this class with the set of <code>types</code> for which the JAVA code is generated. Generator
75      * instantiated this way uses the default build context, e.g. it will re-generate any and all files. The instances
76      * of concrete JAVA code generator are created.
77      *
78      * @param types set of types for which JAVA code should be generated
79      */
80     public GeneratorJavaFile(final Collection<? extends Type> types) {
81         this.types = requireNonNull(types);
82         generators.add(new InterfaceGenerator());
83         generators.add(new TOGenerator());
84         generators.add(new EnumGenerator());
85         generators.add(new BuilderGenerator());
86     }
87
88     public Table<FileKind, String, Supplier<String>> generateFileContent(final boolean ignoreDuplicates) {
89         final Table<FileKind, String, Supplier<String>> result = HashBasedTable.create();
90         for (Type type : types) {
91             for (CodeGenerator generator : generators) {
92                 if (!generator.isAcceptable(type)) {
93                     continue;
94                 }
95
96                 final FileKind kind = type instanceof GeneratedTransferObject
97                         && ((GeneratedTransferObject) type).isUnionTypeBuilder()
98                         ? FileKind.PERSISTENT : FileKind.TRANSIENT;
99                 final String file = type.getPackageName().replace('.', File.separatorChar)
100                         +  File.separator + generator.getUnitName(type) + ".java";
101
102                 if (result.contains(kind, file)) {
103                     if (ignoreDuplicates) {
104                         LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be "
105                                 + "generated.", type.getFullyQualifiedName());
106                         continue;
107                     }
108                     throw new IllegalStateException("Duplicate " + kind + " file '" + file + "' for "
109                             + type.getFullyQualifiedName());
110                 }
111
112                 result.put(kind, file, new GeneratorStringSupplier(generator, type));
113             }
114         }
115
116         return result;
117     }
118
119     /**
120      * Creates the package directory path as concatenation of <code>parentDirectory</code> and parsed
121      * <code>packageName</code>. The parsing of <code>packageName</code> is realized as replacement of the package name
122      * dots with the file system separator.
123      *
124      * @param parentDirectory <code>File</code> object with reference to parent directory
125      * @param packageName string with the name of the package
126      * @return <code>File</code> object which refers to the new directory for package <code>packageName</code>
127      */
128     public static File packageToDirectory(final File parentDirectory, final String packageName) {
129         if (packageName == null) {
130             throw new IllegalArgumentException("Package Name cannot be NULL!");
131         }
132
133         return new File(parentDirectory, packageName.replace('.', File.separatorChar));
134     }
135 }