Correct YangModuleInfo.getInstance() nullness warning
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorJavaFile.java
index 7ba0e846055070fd39df9ab33dd0e886d254de98..c7b1c3728446c514cd79f71e1717f6ac76b0c404 100644 (file)
@@ -7,30 +7,56 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator;
 
-import com.google.common.base.Preconditions;
-import java.io.BufferedWriter;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.Table;
 import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.function.Supplier;
 import org.opendaylight.mdsal.binding.model.api.CodeGenerator;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.sonatype.plexus.build.incremental.BuildContext;
-import org.sonatype.plexus.build.incremental.DefaultBuildContext;
 
 /**
  * Generates files with JAVA source codes for every specified type.
- *
  */
 public final class GeneratorJavaFile {
+    public enum FileKind {
+        /**
+         * Transient file. It should be generated in target/generated-sources/ directory or similar.
+         */
+        TRANSIENT,
+        /**
+         * Persistent file. It should be generated in src/main/java/ directory or similar.
+         */
+        PERSISTENT,
+    }
+
+    private static final class GeneratorStringSupplier implements Supplier<String> {
+        private final CodeGenerator generator;
+        private final Type type;
+
+        GeneratorStringSupplier(final CodeGenerator generator, final Type type) {
+            this.generator = requireNonNull(generator);
+            this.type = requireNonNull(type);
+        }
+
+        @Override
+        public String get() {
+            return generator.generate(type);
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this).add("generator", generator).add("type", type).toString();
+        }
+    }
 
     private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
 
@@ -45,168 +71,59 @@ public final class GeneratorJavaFile {
     private final Collection<? extends Type> types;
 
     /**
-     * BuildContext used for instantiating files
-     */
-    private final BuildContext buildContext;
-
-    /**
-     * Creates instance of this class with the set of <code>types</code> for
-     * which the JAVA code is generated.
-     *
-     * The instances of concrete JAVA code generator are created.
+     * Creates instance of this class with the set of <code>types</code> for which the JAVA code is generated. Generator
+     * instantiated this way uses the default build context, e.g. it will re-generate any and all files. The instances
+     * of concrete JAVA code generator are created.
      *
-     * @param buildContext
-     *            build context to use for accessing files
-     * @param types
-     *            set of types for which JAVA code should be generated
+     * @param types set of types for which JAVA code should be generated
      */
-    public GeneratorJavaFile(final BuildContext buildContext, final Collection<? extends Type> types) {
-        this.buildContext = Preconditions.checkNotNull(buildContext);
-        this.types = Preconditions.checkNotNull(types);
+    public GeneratorJavaFile(final Collection<? extends Type> types) {
+        this.types = requireNonNull(types);
         generators.add(new InterfaceGenerator());
         generators.add(new TOGenerator());
         generators.add(new EnumGenerator());
         generators.add(new BuilderGenerator());
     }
 
-    /**
-     * Creates instance of this class with the set of <code>types</code> for
-     * which the JAVA code is generated. Generator instantiated this way uses
-     * the default build context, e.g. it will re-generate any and all files.
-     *
-     * The instances of concrete JAVA code generator are created.
-     *
-     * @param types
-     *            set of types for which JAVA code should be generated
-     */
-    public GeneratorJavaFile(final Collection<? extends Type> types) {
-        this(new DefaultBuildContext(), types);
-    }
-
-    /**
-     * Generates list of files with JAVA source code. Only the suitable code
-     * generator is used to generate the source code for the concrete type.
-     *
-     * @param generatedSourcesDirectory
-     *            directory to which the output source codes should be generated
-     * @return list of output files
-     * @throws IOException
-     *             if the error during writing to the file occurs
-     */
-    public List<File> generateToFile(final File generatedSourcesDirectory) throws IOException {
-        return generateToFile(generatedSourcesDirectory, generatedSourcesDirectory);
-    }
-
-    public List<File> generateToFile(final File generatedSourcesDirectory, final File persistenSourcesDirectory)
-            throws IOException {
-        final List<File> result = new ArrayList<>();
+    public Table<FileKind, String, Supplier<String>> generateFileContent(final boolean ignoreDuplicates) {
+        final Table<FileKind, String, Supplier<String>> result = HashBasedTable.create();
         for (Type type : types) {
-            if (type != null) {
-                for (CodeGenerator generator : generators) {
-                    File generatedJavaFile = null;
-                    if (type instanceof GeneratedTransferObject
-                            && ((GeneratedTransferObject) type).isUnionTypeBuilder()) {
-                        File packageDir = packageToDirectory(persistenSourcesDirectory, type.getPackageName());
-                        File file = new File(packageDir, generator.getUnitName(type) + ".java");
-                        if (!file.exists()) {
-                            generatedJavaFile = generateTypeToJavaFile(persistenSourcesDirectory, type, generator);
-                        }
-                    } else {
-                        generatedJavaFile = generateTypeToJavaFile(generatedSourcesDirectory, type, generator);
-                    }
-                    if (generatedJavaFile != null) {
-                        result.add(generatedJavaFile);
-                    }
+            for (CodeGenerator generator : generators) {
+                if (!generator.isAcceptable(type)) {
+                    continue;
                 }
-            }
-        }
-        return result;
-    }
 
-    /**
-     * Generates <code>File</code> for <code>type</code>. All files are stored
-     * to subfolders of base directory <code>parentDir</code>. Subdirectories
-     * are generated according to packages to which the type belongs (e. g. if
-     * type belongs to the package <i>org.pcg</i> then in <code>parentDir</code>
-     * is created directory <i>org</i> which contains <i>pcg</i>).
-     *
-     * @param parentDir
-     *            directory where should be the new file generated
-     * @param type
-     *            JAVA <code>Type</code> for which should be JAVA source code
-     *            generated
-     * @param generator
-     *            code generator which is used for generating of the source code
-     * @return file which contains JAVA source code
-     * @throws IOException
-     *             if the error during writing to the file occurs
-     * @throws IllegalArgumentException
-     *             if <code>type</code> equals <code>null</code>
-     * @throws IllegalStateException
-     *             if string with generated code is empty
-     */
-    private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
-            throws IOException {
-        if (parentDir == null) {
-            LOG.warn("Parent Directory not specified, files will be generated "
-                    + "accordingly to generated Type package path.");
-        }
-        if (type == null) {
-            LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
-            throw new IllegalArgumentException("Generated Type Cannot be NULL!");
-        }
-        if (generator == null) {
-            LOG.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
-            throw new IllegalArgumentException("Code Generator Cannot be NULL!");
-        }
-        final File packageDir = packageToDirectory(parentDir, type.getPackageName());
-
-        if (!packageDir.exists()) {
-            packageDir.mkdirs();
-        }
-
-        if (generator.isAcceptable(type)) {
-            final String generatedCode = generator.generate(type);
-            if (generatedCode.isEmpty()) {
-                throw new IllegalStateException("Generated code should not be empty!");
-            }
-            final File file = new File(packageDir, generator.getUnitName(type) + ".java");
-
-            if (file.exists()) {
-                LOG.warn(
-                        "Naming conflict for type '{}': file with same name already exists and will not be generated.",
-                        type.getFullyQualifiedName());
-                return null;
-            }
-
-            try (final OutputStream stream = buildContext.newFileOutputStream(file)) {
-                try (final Writer fw = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {
-                    try (final BufferedWriter bw = new BufferedWriter(fw)) {
-                        bw.write(generatedCode);
+                final FileKind kind = type instanceof GeneratedTransferObject
+                        && ((GeneratedTransferObject) type).isUnionTypeBuilder()
+                        ? FileKind.PERSISTENT : FileKind.TRANSIENT;
+                final String file = type.getPackageName().replace('.', File.separatorChar)
+                        +  File.separator + generator.getUnitName(type) + ".java";
+
+                if (result.contains(kind, file)) {
+                    if (ignoreDuplicates) {
+                        LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be "
+                                + "generated.", type.getFullyQualifiedName());
+                        continue;
                     }
-                } catch (IOException e) {
-                    LOG.error("Failed to write generate output into {}", file.getPath(), e);
-                    throw e;
+                    throw new IllegalStateException("Duplicate " + kind + " file '" + file + "' for "
+                            + type.getFullyQualifiedName());
                 }
-            }
-            return file;
 
+                result.put(kind, file, new GeneratorStringSupplier(generator, type));
+            }
         }
-        return null;
+
+        return result;
     }
 
     /**
-     * Creates the package directory path as concatenation of
-     * <code>parentDirectory</code> and parsed <code>packageName</code>. The
-     * parsing of <code>packageName</code> is realized as replacement of the
-     * package name dots with the file system separator.
+     * Creates the package directory path as concatenation of <code>parentDirectory</code> and parsed
+     * <code>packageName</code>. The parsing of <code>packageName</code> is realized as replacement of the package name
+     * dots with the file system separator.
      *
-     * @param parentDirectory
-     *            <code>File</code> object with reference to parent directory
-     * @param packageName
-     *            string with the name of the package
-     * @return <code>File</code> object which refers to the new directory for
-     *         package <code>packageName</code>
+     * @param parentDirectory <code>File</code> object with reference to parent directory
+     * @param packageName string with the name of the package
+     * @return <code>File</code> object which refers to the new directory for package <code>packageName</code>
      */
     public static File packageToDirectory(final File parentDirectory, final String packageName) {
         if (packageName == null) {