package com.fathomdb.cli.output;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;


import com.fathomdb.cli.formatter.FormatterRegistry;
import com.google.common.collect.Lists;

public class FormattedList<T> extends ArrayList<T> {
    private static final long serialVersionUID = 1L;
    private final FormatterRegistry registry;

    final boolean decorate;

    public FormattedList(FormatterRegistry registry, Iterable<T> items, boolean decorate) {
        super(Lists.newArrayList(items));
        this.registry = registry;
        this.decorate = decorate;
    }

    public FormattedList(FormatterRegistry registry, boolean decorate) {
        this(registry, Collections.<T> emptyList(), decorate);
    }

    public static <T> FormattedList<T> build(FormatterRegistry registry, Iterable<T> items, boolean decorate) {
        return new FormattedList<T>(registry, items, decorate);
    }

    @Override
    public String toString() {
        Writer writer = new StringWriter();
        PrintWriter out = new PrintWriter(writer);
        TextOutputSink sink = new TextOutputSink(registry, out, decorate);

        try {
            for (T item : this) {
                sink.visitObject(item);
            }
            sink.finishOutput();
        } catch (IOException e) {
            throw new IllegalArgumentException("Error formatting list", e);
        }

        return writer.toString();
    }

}

@@ -193,16 +193,66 @@
 ctions;%0A
+import java.util.Iterator;%0Aimport java.util.List;%0A
 %0A%0Aimport
@@ -373,25 +373,27 @@
 %3CT%3E 
-extends ArrayList
+implements Iterable
 %3CT%3E 
@@ -523,16 +523,42 @@
 orate;%0A%0A
+    final List%3CT%3E items;%0A%0A
     publ
@@ -653,14 +653,21 @@
     
-super(
+this.items = 
 List
@@ -687,17 +687,16 @@
 t(items)
-)
 ;%0A      
@@ -1603,10 +1603,84 @@
 %0A    %7D%0A%0A
+%09@Override%0A%09public Iterator%3CT%3E iterator() %7B%0A%09%09return items.iterator();%0A%09%7D%0A
 %7D%0A

Fix FormattedList