// Path: src/main/java/at/favre/tools/uberadb/util/FileUtil.java
// public final class FileUtil {
// 
//     private FileUtil() {
//     }
// 
//     public static String getFileExtension(File file) {
//         if (file == null) {
//             return "";
//         }
//         return file.getName().substring(file.getName().lastIndexOf(".") + 1);
//     }
// 
//     public static String getFileNameWithoutExtension(File file) {
//         String fileName = file.getName();
//         int pos = fileName.lastIndexOf(".");
//         if (pos > 0) {
//             fileName = fileName.substring(0, pos);
//         }
//         return fileName;
//     }
// 
//     public static String createChecksum(File file, String shaAlgo) {
//         try {
//             InputStream fis = new FileInputStream(file);
//             byte[] buffer = new byte[1024];
//             MessageDigest complete = MessageDigest.getInstance(shaAlgo);
//             int numRead;
// 
//             do {
//                 numRead = fis.read(buffer);
//                 if (numRead > 0) {
//                     complete.update(buffer, 0, numRead);
//                 }
//             } while (numRead != -1);
// 
//             fis.close();
//             return new BigInteger(1, complete.digest()).toString(16).toLowerCase();
//         } catch (Exception e) {
//             throw new IllegalStateException("could not create checksum for " + file + " and algo " + shaAlgo + ": " + e.getMessage(), e);
//         }
//     }
// 
//     public static void removeRecursive(Path path) {
//         try {
//             Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
//                 @Override
//                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
//                     Files.delete(file);
//                     return FileVisitResult.CONTINUE;
//                 }
// 
//                 @Override
//                 public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
//                     Files.delete(file);
//                     return FileVisitResult.CONTINUE;
//                 }
// 
//                 @Override
//                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
//                     if (exc == null) {
//                         Files.delete(dir);
//                         return FileVisitResult.CONTINUE;
//                     } else {
//                         throw exc;
//                     }
//                 }
//             });
//         } catch (Exception e) {
//             throw new IllegalStateException("could not delete " + path + ": " + e.getMessage(), e);
//         }
//     }
// 
//     public static String getFileSizeMb(File file) {
//         try {
//             DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
//             df.applyPattern("0.0#");
//             long fileSizeInBytes = file.length();
//             return df.format(fileSizeInBytes / (1024.0f * 1024.0f)) + " MiB";
//         } catch (Exception e) {
//             return "<null>";
//         }
//     }
// }


import at.favre.tools.uberadb.util.FileUtil;
import java.io.File;
import java.util.*;
/*
 *
 *  *  Copyright 2016 Patrick Favre-Bulle
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *
 */

package at.favre.tools.uberadb.ui;



/**
 * Parses and checks the file input argument
 */
public class FileArgParser {

    public List<File> parseAndSortUniqueFilesNonRecursive(String[] files, String extensionFilter) {
        if (files == null) {
            throw new IllegalArgumentException("input files must not be null");
        }

        if (files.length == 0) {
            return Collections.emptyList();
        }

        Set<File> fileSet = new HashSet<>();

        for (String file : files) {
            File apkFile = new File(file);

            if (apkFile.exists() && apkFile.isDirectory()) {
                for (File dirFile : apkFile.listFiles()) {
                    if (isCorrectFile(dirFile, extensionFilter)) {
                        fileSet.add(dirFile);
                    }
                }
            } else if (isCorrectFile(apkFile, extensionFilter)) {
                fileSet.add(apkFile);
            } else {
                throw new IllegalArgumentException("provided apk path or file '" + file + "' does not exist");
            }
        }

        List<File> resultList = new ArrayList<>(fileSet);
        Collections.sort(resultList);
        return resultList;
    }

    private static boolean isCorrectFile(File f, String extensionFilter) {
        if (f != null && f.exists() && f.isFile()) {
// Path: src/main/java/at/favre/tools/uberadb/util/FileUtil.java
// public final class FileUtil {
// 
//     private FileUtil() {
//     }
// 
//     public static String getFileExtension(File file) {
//         if (file == null) {
//             return "";
//         }
//         return file.getName().substring(file.getName().lastIndexOf(".") + 1);
//     }
// 
//     public static String getFileNameWithoutExtension(File file) {
//         String fileName = file.getName();
//         int pos = fileName.lastIndexOf(".");
//         if (pos > 0) {
//             fileName = fileName.substring(0, pos);
//         }
//         return fileName;
//     }
// 
//     public static String createChecksum(File file, String shaAlgo) {
//         try {
//             InputStream fis = new FileInputStream(file);
//             byte[] buffer = new byte[1024];
//             MessageDigest complete = MessageDigest.getInstance(shaAlgo);
//             int numRead;
// 
//             do {
//                 numRead = fis.read(buffer);
//                 if (numRead > 0) {
//                     complete.update(buffer, 0, numRead);
//                 }
//             } while (numRead != -1);
// 
//             fis.close();
//             return new BigInteger(1, complete.digest()).toString(16).toLowerCase();
//         } catch (Exception e) {
//             throw new IllegalStateException("could not create checksum for " + file + " and algo " + shaAlgo + ": " + e.getMessage(), e);
//         }
//     }
// 
//     public static void removeRecursive(Path path) {
//         try {
//             Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
//                 @Override
//                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
//                     Files.delete(file);
//                     return FileVisitResult.CONTINUE;
//                 }
// 
//                 @Override
//                 public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
//                     Files.delete(file);
//                     return FileVisitResult.CONTINUE;
//                 }
// 
//                 @Override
//                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
//                     if (exc == null) {
//                         Files.delete(dir);
//                         return FileVisitResult.CONTINUE;
//                     } else {
//                         throw exc;
//                     }
//                 }
//             });
//         } catch (Exception e) {
//             throw new IllegalStateException("could not delete " + path + ": " + e.getMessage(), e);
//         }
//     }
// 
//     public static String getFileSizeMb(File file) {
//         try {
//             DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
//             df.applyPattern("0.0#");
//             long fileSizeInBytes = file.length();
//             return df.format(fileSizeInBytes / (1024.0f * 1024.0f)) + " MiB";
//         } catch (Exception e) {
//             return "<null>";
//         }
//     }
// }


// Path: src/main/java/at/favre/tools/uberadb/ui/FileArgParser.java
import at.favre.tools.uberadb.util.FileUtil;
import java.io.File;
import java.util.*;

/*
 *
 *  *  Copyright 2016 Patrick Favre-Bulle
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *
 */

package at.favre.tools.uberadb.ui;



/**
 * Parses and checks the file input argument
 */
public class FileArgParser {

    public List<File> parseAndSortUniqueFilesNonRecursive(String[] files, String extensionFilter) {
        if (files == null) {
            throw new IllegalArgumentException("input files must not be null");
        }

        if (files.length == 0) {
            return Collections.emptyList();
        }

        Set<File> fileSet = new HashSet<>();

        for (String file : files) {
            File apkFile = new File(file);

            if (apkFile.exists() && apkFile.isDirectory()) {
                for (File dirFile : apkFile.listFiles()) {
                    if (isCorrectFile(dirFile, extensionFilter)) {
                        fileSet.add(dirFile);
                    }
                }
            } else if (isCorrectFile(apkFile, extensionFilter)) {
                fileSet.add(apkFile);
            } else {
                throw new IllegalArgumentException("provided apk path or file '" + file + "' does not exist");
            }
        }

        List<File> resultList = new ArrayList<>(fileSet);
        Collections.sort(resultList);
        return resultList;
    }

    private static boolean isCorrectFile(File f, String extensionFilter) {
        if (f != null && f.exists() && f.isFile()) {
            return FileUtil.getFileExtension(f).equalsIgnoreCase(extensionFilter);