
package forager.server;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Handles the active and completed task lists and allows them to be flushed
 * and synced to disk.
 *
 * @author malensek
 */
public class ListManager {

    private FileOutputStream taskListOut;
    private PrintWriter taskListWriter;

    private FileOutputStream completedListOut;
    private PrintWriter completedListWriter;

    public ListManager(String taskListName)
    throws IOException {
        this(taskListName, false);
    }

    public ListManager(String taskListName, boolean clearExisting)
    throws IOException {
        boolean append = !(clearExisting);

        taskListOut = new FileOutputStream(taskListName, append);
        taskListWriter = new PrintWriter(
                new BufferedOutputStream(taskListOut));

        String completedName = taskListName + ".done";
        completedListOut = new FileOutputStream(completedName, append);
        completedListWriter = new PrintWriter(
                new BufferedOutputStream(completedListOut));
    }

    public void addTask(String command) {
        taskListWriter.println(command);
    }

    public void addCompletedTask(String command) {
        completedListWriter.println(command);
    }

    public void syncTasks()
    throws IOException {
        taskListWriter.flush();
        taskListOut.getFD().sync();
    }

    public void syncCompleted()
    throws IOException {
        completedListWriter.flush();
        completedListOut.getFD().sync();
    }
}

@@ -48,32 +48,53 @@
 edOutputStream;%0A
+import java.io.File;%0A
 import java.io.F
@@ -102,32 +102,32 @@
 leOutputStream;%0A
-
 import java.io.I
@@ -165,16 +165,193 @@
 tWriter;
+%0Aimport java.nio.charset.Charset;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport java.util.List;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0A/**%0A *
@@ -503,16 +503,145 @@
 ager %7B%0A%0A
+    private static final Logger logger = Logger.getLogger(%22forager%22);%0A%0A    private static final String COMPLETED_EXT = %22.done%22;%0A%0A
     priv
@@ -1870,32 +1870,32 @@
 Writer.flush();%0A
-
         complete
@@ -1924,11 +1924,925 @@
 );%0A    %7D
+%0A%0A    public static boolean listsExist(String taskListName) %7B%0A        File listFile = new File(taskListName);%0A        File doneFile = new File(taskListName + COMPLETED_EXT);%0A        return (listFile.exists() %7C%7C doneFile.exists());%0A    %7D%0A%0A    public static List%3CString%3E getPendingTasks(String taskListName)%0A    throws IOException %7B%0A        List%3CString%3E allTasks = Files.readAllLines(%0A                Paths.get(taskListName), Charset.defaultCharset());%0A        List%3CString%3E completedTasks = Files.readAllLines(%0A                Paths.get(taskListName + COMPLETED_EXT));%0A%0A        for (String command : completedTasks) %7B%0A            boolean result = allTasks.remove(command);%0A            if (result == false) %7B%0A                logger.log(Level.WARNING,%0A                        %22Completed task not found in master task list: %7B0%7D%22,%0A                        command);%0A            %7D%0A        %7D%0A%0A        return allTasks;%0A    %7D
 %0A%7D%0A

Resolve merge conflicts