Simplify boolean expressions
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / Util.java
index b9287185b3ba47d9d27adb1d9201f13eadc3d1c0..8ecf8f65ab289465c4d0fd79a043379fd6a9467d 100644 (file)
@@ -7,12 +7,14 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FilenameFilter;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -24,7 +26,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -33,16 +34,12 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.repository.RepositorySystem;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
-import org.apache.maven.repository.RepositorySystem;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 final class Util {
 
@@ -54,6 +51,7 @@ final class Util {
 
     static final String YANG_SUFFIX = "yang";
 
+    private static final Logger LOG = LoggerFactory.getLogger(Util.class);
     private static final int CACHE_SIZE = 10;
     // Cache for listed directories and found yang files. Typically yang files
     // are utilized twice. First: code is generated during generate-sources
@@ -81,11 +79,11 @@ final class Util {
         return yangFiles;
     }
 
-    static Collection<File> listFiles(File root, File[] excludedFiles, Log log) throws FileNotFoundException {
+    static Collection<File> listFiles(File root, File[] excludedFiles) throws FileNotFoundException {
         if (!root.exists()) {
-            if (log != null) {
-                log.warn(Util.message("YANG source directory %s not found. No code will be generated.", YangToSourcesProcessor.LOG_PREFIX, root.toString()));
-            }
+            LOG.warn("{} YANG source directory {} not found. No code will be generated.", YangToSourcesProcessor
+                    .LOG_PREFIX, root.toString());
+
             return Collections.emptyList();
         }
         Collection<File> result = new ArrayList<>();
@@ -99,10 +97,8 @@ final class Util {
                 }
             }
             if (excluded) {
-                if (log != null) {
-                    log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
-                            Util.YANG_SUFFIX.toUpperCase(), f));
-                }
+                LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(),
+                        f);
             } else {
                 result.add(f);
             }
@@ -126,26 +122,12 @@ final class Util {
     private static Class<?> resolveClass(String codeGeneratorClass, Class<?> baseType) throws ClassNotFoundException {
         Class<?> clazz = Class.forName(codeGeneratorClass);
 
-        if (!isImplemented(baseType, clazz)) {
+        if (!baseType.isAssignableFrom(clazz)) {
             throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
         }
         return clazz;
     }
 
-    private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
-        for (Class<?> iface : byClazz.getInterfaces()) {
-            if (iface.equals(expectedIface)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    static String message(String message, String logPrefix, Object... args) {
-        String innerMessage = String.format(message, args);
-        return String.format("%s %s", logPrefix, innerMessage);
-    }
-
     static List<File> getClassPath(MavenProject project) {
         List<File> dependencies = Lists.newArrayList();
         for (Artifact element : project.getArtifacts()) {
@@ -169,24 +151,22 @@ final class Util {
      *            local repository
      * @param remoteRepos
      *            remote repositories
-     * @param log
-     *            logger
      */
     static void checkClasspath(MavenProject project, RepositorySystem repoSystem, ArtifactRepository localRepo,
-            List<ArtifactRepository> remoteRepos, Log log) {
+            List<ArtifactRepository> remoteRepos) {
         Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
         if (plugin == null) {
-            log.warn(message("%s not found, dependencies version check skipped", YangToSourcesProcessor.LOG_PREFIX,
-                    YangToSourcesMojo.PLUGIN_NAME));
+            LOG.warn("{} {} not found, dependencies version check skipped", YangToSourcesProcessor.LOG_PREFIX,
+                    YangToSourcesMojo.PLUGIN_NAME);
         } else {
             Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
-            getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos, log);
+            getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
 
             Set<Artifact> projectDependencies = project.getDependencyArtifacts();
             for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
-                checkArtifact(entry.getKey(), projectDependencies, log);
+                checkArtifact(entry.getKey(), projectDependencies);
                 for (Artifact dependency : entry.getValue()) {
-                    checkArtifact(dependency, projectDependencies, log);
+                    checkArtifact(dependency, projectDependencies);
                 }
             }
         }
@@ -205,12 +185,9 @@ final class Util {
      *            local repository
      * @param remoteRepos
      *            list of remote repositories
-     * @param log
-     *            logger
      */
     private static void getPluginTransitiveDependencies(Plugin plugin, Map<Artifact, Collection<Artifact>> map,
-            RepositorySystem repoSystem, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos,
-            Log log) {
+            RepositorySystem repoSystem, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos) {
 
         List<Dependency> pluginDependencies = plugin.getDependencies();
         for (Dependency dep : pluginDependencies) {
@@ -237,18 +214,15 @@ final class Util {
      *            artifact to check
      * @param dependencies
      *            collection of dependencies
-     * @param log
-     *            logger
      */
-    private static void checkArtifact(Artifact artifact, Collection<Artifact> dependencies, Log log) {
+    private static void checkArtifact(Artifact artifact, Collection<Artifact> dependencies) {
         for (org.apache.maven.artifact.Artifact d : dependencies) {
             if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())) {
                 if (!(artifact.getVersion().equals(d.getVersion()))) {
-                    log.warn(message("Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX));
-                    log.warn(message("'%s' dependency [%s] has different version than one "
-                            + "declared in current project [%s]. It is recommended to fix this problem "
-                            + "because it may cause compilation errors.", YangToSourcesProcessor.LOG_PREFIX,
-                            YangToSourcesMojo.PLUGIN_NAME, artifact, d));
+                    LOG.warn("{} Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX);
+                    LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]" +
+                                    ". It is recommended to fix this problem because it may cause compilation errors.",
+                            YangToSourcesProcessor.LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d);
                 }
             }
         }
@@ -257,7 +231,7 @@ final class Util {
     private static final String JAR_SUFFIX = ".jar";
 
     private static boolean isJar(File element) {
-        return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true : false;
+        return (element.isFile() && element.getName().endsWith(JAR_SUFFIX));
     }
 
     static <T> T checkNotNull(T obj, String paramName) {
@@ -265,37 +239,34 @@ final class Util {
     }
 
     static final class YangsInZipsResult implements Closeable {
-        private final List<InputStream> yangStreams;
+        private final List<YangSourceFromDependency> yangStreams;
         private final List<Closeable> zipInputStreams;
 
-        private YangsInZipsResult(List<InputStream> yangStreams, List<Closeable> zipInputStreams) {
+        private YangsInZipsResult(List<YangSourceFromDependency> yangStreams, List<Closeable> zipInputStreams) {
             this.yangStreams = yangStreams;
             this.zipInputStreams = zipInputStreams;
         }
 
         @Override
         public void close() throws IOException {
-            for (InputStream is : yangStreams) {
-                is.close();
-            }
             for (Closeable is : zipInputStreams) {
                 is.close();
             }
         }
 
-        public List<InputStream> getYangStreams() {
+        public List<YangSourceFromDependency> getYangStreams() {
             return this.yangStreams;
         }
     }
 
-    static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
+    static YangsInZipsResult findYangFilesInDependenciesAsStream(MavenProject project)
             throws MojoFailureException {
-        List<InputStream> yangsFromDependencies = new ArrayList<>();
+        List<YangSourceFromDependency> yangsFromDependencies = new ArrayList<>();
         List<Closeable> zips = new ArrayList<>();
         try {
             List<File> filesOnCp = Util.getClassPath(project);
-            log.info(Util.message("Searching for yang files in following dependencies: %s",
-                    YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
+            LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
+                    filesOnCp);
 
             for (File file : filesOnCp) {
                 List<String> foundFilesForReporting = new ArrayList<>();
@@ -310,8 +281,8 @@ final class Util {
                                 return name.endsWith(".yang") && new File(dir, name).isFile();
                             }
                         });
-                        for (File yangFile : yangFiles) {
-                            yangsFromDependencies.add(new NamedFileInputStream(yangFile, YangToSourcesProcessor.META_INF_YANG_STRING + File.separator + yangFile.getName()));
+                        for (final File yangFile : yangFiles) {
+                            yangsFromDependencies.add(new YangSourceFromFile(yangFile));
                         }
                     }
 
@@ -327,16 +298,13 @@ final class Util {
                         if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
                                 && !entry.isDirectory() && entryName.endsWith(".yang")) {
                             foundFilesForReporting.add(entryName);
-                            // This will be closed after all streams are
-                            // parsed.
-                            InputStream entryStream = zip.getInputStream(entry);
-                            yangsFromDependencies.add(entryStream);
+                            yangsFromDependencies.add(new YangSourceInZipFile(zip, entry));
                         }
                     }
                 }
                 if (foundFilesForReporting.size() > 0) {
-                    log.info(Util.message("Found %d yang files in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
-                            foundFilesForReporting.size(), file, foundFilesForReporting));
+                    LOG.info("{} Found {} yang files in {}: {}", YangToSourcesProcessor.LOG_PREFIX,
+                            foundFilesForReporting.size(), file, foundFilesForReporting);
                 }
 
             }
@@ -346,13 +314,24 @@ final class Util {
         return new YangsInZipsResult(yangsFromDependencies, zips);
     }
 
-    static Collection<File> findYangFilesInDependencies(Log log, MavenProject project) throws MojoFailureException {
+    /**
+     * Find all dependencies which contains yang sources
+     *
+     * Returns collection of YANG files and Zip files which contains YANG files.
+     *
+     * FIXME: Rename to what class is actually doing.
+     *
+     * @param project
+     * @return
+     * @throws MojoFailureException
+     */
+    static Collection<File> findYangFilesInDependencies(MavenProject project) throws MojoFailureException {
         final List<File> yangsFilesFromDependencies = new ArrayList<>();
 
         try {
             List<File> filesOnCp = Util.getClassPath(project);
-            log.info(Util.message("Searching for yang files in following dependencies: %s",
-                    YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
+            LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
+                    filesOnCp);
 
             for (File file : filesOnCp) {
                 // is it jar file or directory?
@@ -379,8 +358,8 @@ final class Util {
 
                             if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
                                     && !entry.isDirectory() && entryName.endsWith(".yang")) {
-                                log.debug(Util.message("Found a YANG file in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
-                                        file, entryName));
+                                LOG.debug("{} Found a YANG file in {}: {}", YangToSourcesProcessor.LOG_PREFIX, file,
+                                        entryName);
                                 yangsFilesFromDependencies.add(file);
                                 break;
                             }