package org.fool.glorious.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DirScanner {

    static public List<String> findAll(String path, String[] exts, boolean recursive) {
        StringBuilder sb = new StringBuilder();
        for (String ext :exts) {
            sb.append("(^.*").append(ext).append("$)").append("|");
        }

        if (sb.length() == 0) {
            sb.append(".*|");
        }

        List<String> ret = new ArrayList();
        File dir = new File(path);
        for (File file : dir.listFiles()) {
            if (!file.isDirectory()) {
                if (file.getName().matches(sb.substring(0, sb.length() - 1))){
                    ret.add(file.getAbsolutePath());
                }
            } else if (recursive){
                ret.addAll(findAll(file.getAbsolutePath(), exts, recursive));
            }
        }
        return ret;
    }
}
