Formatting Code base with ktlint
[ccsdk/cds.git] / ms / sdclistener / application / src / main / java / org / onap / ccsdk / cds / sdclistener / util / FileUtil.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.sdclistener.util;
18
19 import org.apache.commons.io.FileUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31
32 import static java.nio.file.Files.walk;
33
34 public final class FileUtil {
35     private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
36
37     private FileUtil() {
38
39     }
40
41     /**
42      * Delete the file.
43      *
44      * @param file - Given file
45      * @param path - Given path
46      */
47     public static void deleteFile(File file, String path) {
48         boolean value = file.delete();
49         if (!value) {
50             LOGGER.error("Failed to delete the file {} at the location {}", file, path);
51         }
52     }
53
54     /**
55      * Extract files from the given path
56      *
57      * @param path where files reside.
58      *
59      * @return list of files.
60      */
61     public static List<File> getFilesFromDisk(Path path) {
62
63         try (Stream<Path> fileTree = walk(path)) {
64             // Get the list of files from the path
65             return fileTree.filter(Files :: isRegularFile)
66                     .map(Path :: toFile)
67                     .collect(Collectors.toList());
68         } catch (IOException e) {
69             LOGGER.error("Failed to find the file due to", e);
70         }
71         return new ArrayList<>();
72     }
73
74 }