diff options
author | Dmitry Puzikov <d.puzikov2@partner.samsung.com> | 2019-11-13 12:51:41 +0100 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2019-12-01 15:25:43 +0000 |
commit | 4a9ddb2618b8dde8820c08d934c708216b0ebca9 (patch) | |
tree | 1dba2999d87ef1717b9f8211b137188e1cefb08a /common/onap-common-configuration-management/onap-configuration-management-core/src/main | |
parent | 5b1f9a16f823c31d26f9306ea1ee540120f6780e (diff) |
Fixing sonar issue
Getting rid of nested ifs-fors
Change-Id: I71ef686683b7070614c27aabbf6cd058b5d35ffd
Issue-ID: SDC-2654
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index df382143c6..17e9ef048f 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -44,6 +44,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -51,7 +52,10 @@ import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Files; +import java.nio.file.FileVisitResult; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -113,20 +117,31 @@ public class ConfigurationUtils { } public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) { - ArrayList<File> collection = new ArrayList<>(); - if (file.isDirectory() && file.exists()) { - File[] files = file.listFiles(); - for (File innerFile : Objects.requireNonNull(files)) { - if (innerFile.isFile() && !onlyDirectory) { - collection.add(innerFile); - } else if (innerFile.isDirectory()) { - collection.add(innerFile); - if (recursive) { - collection.addAll(getAllFiles(innerFile, true, onlyDirectory)); + Path rootPath = file.toPath(); + try { + Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + super.preVisitDirectory(dir,attrs); + if (rootPath.equals(dir)) { + return FileVisitResult.CONTINUE; } + collection.add(dir.toFile()); + return recursive? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; } - } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + super.visitFile(file, attrs); + if (!onlyDirectory) { + collection.add(file.toFile()); + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + LOGGER.error("Failed to walk through directories starting from: {}.", file.toString(), e); } return collection; } |