package misc;

import lombok.Data;

import java.io.File;
import java.util.*;

@Data
public class JobBuilder {
    /** The name of the Job. */
    private String name;
    /** The output directory. */
    private String outputDirectory;
    /** The file(s) belonging to the Job.*/
    private List<File> files;
    /** Whether the Job is an Encode Job or a Decode Job. */
    private boolean isEncodeJob = true;
    /** Whether to pack all of the files into a single archive before encoding. */
    private boolean singleArchive = false;

    /** Constructs a new JobBuilder. */
    public JobBuilder() {
        reset();
    }

    /**
     * Build a new job.
     *
     * @return
     *         The job.
     */
    public Job build() {
        checkState();
        return new Job(this);
    }

    /**
     * Checks the state of the builder.
     *
     * @throws java.lang.NullPointerException
     *          If the output directory or files list is null.
     *
     * @throws java.lang.IllegalArgumentException
     *          If the output directory is empty.
     */
    private void checkState() {
        Objects.requireNonNull(outputDirectory);
        Objects.requireNonNull(files);

        if (name == null || name.isEmpty()) {
            final UUID uuid = UUID.randomUUID();
            name = uuid.toString();
        }

        if (outputDirectory.isEmpty()) {
            throw new IllegalArgumentException("The output directory of a Job cannot be empty.");
        }

        // Ensure output directory has the correct trailing slash:
        if (!outputDirectory.endsWith("\\") && !outputDirectory.endsWith("/")) {
            outputDirectory += "/";
        }

        // Ensure the files are sorted from smallest to largest:
        this.files.sort(Comparator.comparing(File::length));
    }

    /** Resets the state of the builder. */
    public void reset() {
        name = null;
        outputDirectory = null;
        files = new ArrayList<>();
        isEncodeJob = true;
        singleArchive = false;
    }
}

@@ -44,16 +44,91 @@
 java
-.io.File
+x.swing.filechooser.FileSystemView;%0Aimport java.io.File;%0Aimport java.io.IOException
 ;%0Aim
@@ -1420,136 +1420,156 @@
     
-if (outputDirectory.isEmpty()) %7B%0A            throw new IllegalArgumentException(%22The output directory of a Job cannot be empty.%22
+// If the output dir isn't specified, try setting it to the home dir.%0A        if (outputDirectory.isEmpty()) %7B%0A            setOutputToHomeDirectory(
 );%0A 
@@ -1997,38 +1997,47 @@
 = null;%0A        
-o
+setO
 utput
+ToHome
 Directory = null
@@ -2029,23 +2029,18 @@
 irectory
- = null
+()
 ;%0A      
@@ -2092,24 +2092,24 @@
 Job = true;%0A
-
         sing
@@ -2132,11 +2132,446 @@
 e;%0A    %7D
+%0A%0A    /** Sets the output directory to the home directory. */%0A    private void setOutputToHomeDirectory() %7B%0A        try %7B%0A            final File home = FileSystemView.getFileSystemView().getHomeDirectory();%0A            outputDirectory = home.getCanonicalPath() + %22/%22;%0A        %7D catch (final IOException e) %7B%0A            // todo Throw an error or something if this happens, not sure yet.%0A            e.printStackTrace();%0A        %7D%0A    %7D
 %0A%7D%0A

Set the default value of the output directory to the user's home directory.