
package forager.server;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

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

    private static final Logger logger = Logger.getLogger("forager");

    private static final String COMPLETED_EXT = ".done";

    private String taskListName;

    private FileOutputStream taskListOut;
    private PrintWriter taskListWriter;

    private FileOutputStream completedListOut;
    private PrintWriter completedListWriter;

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

    public ListManager(String taskListName, boolean append)
    throws IOException {
        this.taskListName = taskListName;

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

        String completedName = taskListName + COMPLETED_EXT;
        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();
    }

    public static boolean listsExist(String taskListName) {
        File listFile = new File(taskListName);
        File doneFile = new File(taskListName + COMPLETED_EXT);
        return (listFile.exists() || doneFile.exists());
    }

    public static List<String> getPendingTasks(String taskListName)
    throws IOException {
        List<String> allTasks = Files.readAllLines(
                Paths.get(taskListName), Charset.defaultCharset());
        List<String> completedTasks = Files.readAllLines(
                Paths.get(taskListName + COMPLETED_EXT));

        for (String command : completedTasks) {
            boolean result = allTasks.remove(command);
            if (result == false) {
                logger.log(Level.WARNING,
                        "Completed task not found in master task list: {0}",
                        command);
            }
        }

        return allTasks;
    }
}

@@ -255,16 +255,44 @@
 .Paths;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1996,23 +1996,16 @@
  public 
-static 
 boolean 
@@ -2015,35 +2015,16 @@
 tsExist(
-String taskListName
 ) %7B%0A    
@@ -2044,32 +2044,37 @@
 File = new File(
+this.
 taskListName);%0A 
@@ -2105,16 +2105,21 @@
 ew File(
+this.
 taskList
@@ -2216,23 +2216,16 @@
  public 
-static 
 List%3CStr
@@ -2229,19 +2229,20 @@
 String%3E 
-get
+read
 PendingT
@@ -2246,35 +2246,16 @@
 ngTasks(
-String taskListName
 )%0A    th
@@ -2265,32 +2265,117 @@
 s IOException %7B%0A
+        if (listsExist() != true) %7B%0A            return new ArrayList%3C%3E();%0A        %7D%0A%0A
         List%3CStr
@@ -2428,32 +2428,37 @@
       Paths.get(
+this.
 taskListName), C
@@ -2567,16 +2567,21 @@
 ths.get(
+this.
 taskList

Convert static to instance methods