/*
 * Copyright (c) 2016 eilslabs.
 *
 * Distributed under the MIT License (license terms are at https://www.github.com/eilslabs/Roddy/LICENSE.txt).
 */

package de.dkfz.roddy.knowledge.files;

import de.dkfz.roddy.config.ConfigurationError;
import de.dkfz.roddy.core.ExecutionContext;

import java.util.LinkedList;
import java.util.List;

/**
 * Filegroups group multiple files into one group. I.e. BamFiles can be grouped
 * to a BamFileGroup which can then be merged to a single BamFile
 *
 * @author michael
 */
public abstract class FileGroup<F extends BaseFile> extends FileObject {

    // In some cases a group has another group as a parent.
    protected FileGroup parentGroup;

    protected final List<F> filesInGroup = new LinkedList<F>();

    public FileGroup(List<F> files) {
        super(null);
        if (files != null) {
            addFiles(files);
        }
    }

    /**
     * Calls runDefaultOperations on the contained files.
     */
    @Override
    public void runDefaultOperations() throws ConfigurationError {
        for (F file : filesInGroup) {
            file.runDefaultOperations();
        }
    }

    @Override
    public ExecutionContext getExecutionContext() {
        if (filesInGroup.size() > 0) {
            return filesInGroup.get(0).getExecutionContext();
        } else {
            throw new RuntimeException("Cannot call getExecutionContext if FileGroup has no files in it.");
        }
    }

    public final void addFile(F file) {
        filesInGroup.add(file);
        file.addFileGroup(this);
    }

    public final void addFiles(List<F> files) {
        filesInGroup.addAll(files);
        for(BaseFile file : files)
            file.addFileGroup(this);
    }

    public final F getAt(int index) {
        return filesInGroup.get(index);
    }

    public final F get(int index) {
        return filesInGroup.get(index);
    }

    public List<F> getFilesInGroup() {
        return new LinkedList<F>(filesInGroup);
    }

    public FileGroup getParentGroup() {
        return parentGroup;
    }

    public void setParentGroup(FileGroup parentGroup) {

        this.parentGroup = parentGroup;
    }
}

@@ -280,16 +280,43 @@
 ntext;%0A%0A
+import java.util.Iterator;%0A
 import j
@@ -358,16 +358,82 @@
 il.List;
+%0Aimport java.util.Spliterator;%0Aimport java.util.function.Consumer;
 %0A%0A/**%0A *
@@ -672,16 +672,39 @@
 leObject
+ implements Iterable%3CF%3E
  %7B%0A%0A    
@@ -992,24 +992,340 @@
    %7D%0A    %7D%0A%0A
+    @Override%0A    public Iterator%3CF%3E iterator() %7B%0A        return filesInGroup.iterator();%0A    %7D%0A%0A    @Override%0A    public void forEach(Consumer%3C? super F%3E action) %7B%0A        filesInGroup.forEach(action);%0A    %7D%0A%0A    @Override%0A    public Spliterator%3CF%3E spliterator() %7B%0A        return filesInGroup.spliterator();%0A    %7D%0A%0A
     /**%0A    
@@ -1864,32 +1864,339 @@
        %7D%0A    %7D%0A%0A
+    public final void add(F file) %7B%0A        addFile(file);%0A    %7D%0A%0A    public final void leftShift(F file) %7B%0A        addFile(file);%0A    %7D%0A%0A    public final void plus(F file) %7B%0A        addFile(file);%0A    %7D%0A%0A    public final void plus(FileGroup%3CF%3E filegroup) %7B%0A        addFiles(filegroup.filesInGroup);%0A    %7D%0A%0A
     public final

Improve FileGroup usability