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 d6556157562fa89f1a5d7d939e17dac26f53fab3..f863d14801c1cc57f67c9a0c8bf3534279be6ba7 100644 (file)
@@ -14,8 +14,8 @@ 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;
@@ -43,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
@@ -61,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());
@@ -80,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);
     }
 
@@ -172,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)) {
@@ -183,6 +191,7 @@ public final class GeneratorJavaFile {
                 }
             }
             return file;
+
         }
         return null;
     }
@@ -200,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!");
         }