Introduce top-level pom file.
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / GeneratorJavaFile.java
index e2d3fb28e0788293ff208a65599d697a286cbfb4..f863d14801c1cc57f67c9a0c8bf3534279be6ba7 100644 (file)
@@ -14,10 +14,11 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
-import java.util.Set;
 
 import org.opendaylight.yangtools.sal.binding.model.api.CodeGenerator;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,7 +43,7 @@ public final class GeneratorJavaFile {
     /**
      * Set of <code>Type</code> instances for which the JAVA code is generated.
      */
-    private final Set<? extends Type> types;
+    private final Collection<? extends Type> types;
 
     /**
      * BuildContext used for instantiating files
@@ -60,7 +61,7 @@ public final class GeneratorJavaFile {
      * @param types
      *            set of types for which JAVA code should be generated
      */
-    public GeneratorJavaFile(final BuildContext buildContext, final Set<? extends Type> types) {
+    public GeneratorJavaFile(final BuildContext buildContext, final Collection<? extends Type> types) {
         this.buildContext = Preconditions.checkNotNull(buildContext);
         this.types = Preconditions.checkNotNull(types);
         generators.add(new InterfaceGenerator());
@@ -79,7 +80,7 @@ public final class GeneratorJavaFile {
      * @param types
      *            set of types for which JAVA code should be generated
      */
-    public GeneratorJavaFile(final Set<? extends Type> types) {
+    public GeneratorJavaFile(final Collection<? extends Type> types) {
         this(new DefaultBuildContext(), types);
     }
 
@@ -87,18 +88,33 @@ public final class GeneratorJavaFile {
      * 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 parentDirectory
+     * @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 parentDirectory) throws IOException {
+    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<>();
         for (Type type : types) {
             if (type != null) {
                 for (CodeGenerator generator : generators) {
-                    File generatedJavaFile = generateTypeToJavaFile(parentDirectory, type, generator);
+                    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);
                     }
@@ -156,6 +172,14 @@ public final class GeneratorJavaFile {
                 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)) {
                     try (final BufferedWriter bw = new BufferedWriter(fw)) {
@@ -167,6 +191,7 @@ public final class GeneratorJavaFile {
                 }
             }
             return file;
+
         }
         return null;
     }
@@ -184,7 +209,7 @@ public final class GeneratorJavaFile {
      * @return <code>File</code> object which refers to the new directory for
      *         package <code>packageName</code>
      */
-    private File packageToDirectory(final File parentDirectory, final String packageName) {
+    public static File packageToDirectory(final File parentDirectory, final String packageName) {
         if (packageName == null) {
             throw new IllegalArgumentException("Package Name cannot be NULL!");
         }