/**
 * 
 */
package org.forkalsrud.album.exif;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author knut
 *
 */
public class EntryWithChildren<T extends Entry> extends Entry {

    protected List<T> children = new ArrayList<>();

    protected EntryWithChildren(Entry root) {
        super(root);
    }

    protected EntryWithChildren(File path) {
        super(path, "dir");
    }


    void fillLinkedList() {

        Entry prev = null;
        for (Entry e : children) {
            e.prev = prev;
            if (prev != null) {
                prev.next = e;
            }
            prev = e;
        }
        if (prev != null) {
            prev.next = null;
        }
    }

    public List<T> getContents() {

        return children;
    }

    public void addContents(T entry) {
        children.add(entry);
    }

    public Entry get(File f) {
        for (Entry e : children) {
            if (f.equals(e.file)) {
                return e;
            }
        }
        return null;
    }


    @Override
    protected void appendDiagnostic(StringBuilder buf) {
        super.appendDiagnostic(buf);
        buf.append(",");
        buf.append("children: list of ");
        buf.append(children != null ? String.valueOf(children.size()) : "null");
    }
}
