package csci599;

import java.io.*;
import java.util.*;
import java.util.function.*;
import static java.util.stream.Collectors.*;
import org.apache.tika.Tika;

public class FileTypeFilter {
    private static Tika tika = new Tika();

    public static List<String> getMIMETypes(final File sortFolder) {
        return Arrays.stream(sortFolder.listFiles())
                     .map(file -> file.getName().replace(';', '/'))
                     .collect(toList());
    }

    public static void forEach(final File sortFolder, BiConsumer<File, String> callback) {
        for (File file : sortFolder.listFiles()) {
            String mimeType = file.getName().replace(';', '/');
            try (Scanner scanner = new Scanner(file)) {
                while (scanner.hasNextLine()) {
                    callback.accept(new File(scanner.nextLine()), mimeType);
                }
            } catch (Exception ex) {
                System.err.println("Error iterating over files of MIME type " + mimeType);
            }
        }
    }

    public static void sort(final File folder, final List<String> contentTypes) {
        final Map<String, Writer> writers = contentTypes.stream().collect(toMap(Function.identity(), contentType -> {
            try {
                return new OutputStreamWriter(new FileOutputStream(contentType.replace('/', ';')), "UTF-8");
            } catch (Exception ex) {
                System.err.println("Error creating file.");
                System.exit(-1);
                return null;
            }
        }));
        forEachInFolder(folder, file -> {
            try {
                String contentType = tika.detect(file);
                if (contentTypes.contains(contentType)) {
                    writers.get(contentType).write(file.getCanonicalPath() + '\n');
                }
            } catch (IOException ex) {
                System.err.println("Error sorting file: " + file.getPath());
            }
        });
        for (Writer writer : writers.values()) {
            try {
                writer.close();
            } catch (IOException ex) {
                System.err.println("Error closing file.");
            }
        }
    }

    private static void forEachInFolder(final File folder, Consumer<File> callback) {
        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                forEachInFolder(file, callback);
            } else if (file.length() > 0) {
                callback.accept(file);
            }
        }
    }
}

@@ -548,32 +548,459 @@
 ng%3E callback) %7B%0A
+        int line = 0, totalLines = 0;%0A        for (File file : sortFolder.listFiles()) %7B%0A            try (Scanner scanner = new Scanner(file)) %7B%0A                while (scanner.hasNextLine()) %7B%0A                    scanner.nextLine();%0A                    totalLines++;%0A                %7D%0A            %7D catch (Exception ex) %7B%0A                System.out.println(%22Error counting lines in %22 + file.getPath());%0A            %7D%0A        %7D%0A
         for (Fil
@@ -1026,32 +1026,32 @@
 .listFiles()) %7B%0A
-
             Stri
@@ -1094,24 +1094,66 @@
 (';', '/');%0A
+            System.out.println(mimeType);%0A
             
@@ -1236,32 +1236,32 @@
 asNextLine()) %7B%0A
-
                 
@@ -1313,32 +1313,140 @@
 ()), mimeType);%0A
+                    line++;%0A                    System.out.print((int)(100.0 * line / totalLines) + %22%25%5Cr%22);%0A
                 

add progress indicator