package org.agmip.ace;

import java.io.IOException;
import java.util.AbstractCollection;
import java.util.Iterator;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import org.agmip.ace.util.JsonFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AceRecordCollection extends AbstractCollection<AceRecord> {
    private static final Logger log = LoggerFactory.getLogger(AceRecordCollection.class);
    private byte[] collection;
    private int length;

    public AceRecordCollection(byte[] source) throws IOException {
        super();
        this.collection = source;
        this.length = this.countRecords();
    }

    @Override
    public int size() {
        return this.length;
    }

    @Override
    public Iterator<AceRecord> iterator() {
        try {
            return new AceRecordIterator(collection);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    private int countRecords() throws IOException {
        JsonParser p = JsonFactoryImpl.INSTANCE.getParser(collection);
        JsonToken t = p.nextToken();
        int i = 0;
        if (t != JsonToken.START_ARRAY) {
            log.error("Not starting with START_ARRAY, is {}", (t == null ? "NULL" : t.asString()));
            //DEBUG
            log.debug("Value is: {}",new String(collection, "UTF-8"));
            return i;
        }
        while(t != null) {
            if (t == JsonToken.START_OBJECT) {
                i++;
                p.skipChildren();
            }
            t = p.nextToken();
        }
        p.close();
        return i;
    }
}

@@ -979,24 +979,1158 @@
      %7D%0A    %7D
+%0A    %0A    @Override%0A    public boolean add(AceRecord e) %7B%0A        try %7B%0A            JsonParser p = JsonFactoryImpl.INSTANCE.getParser(collection);%0A            JsonToken t = p.nextToken();%0A            if (t != JsonToken.START_ARRAY) %7B%0A                log.error(%22Not starting with START_ARRAY, is %7B%7D%22, (t == null ? %22NULL%22 : t.asString()));%0A                //DEBUG%0A                log.debug(%22Value is: %7B%7D%22,new String(collection, %22UTF-8%22));%0A                return false;%0A            %7D%0A            p.close();%0A            StringBuilder sb = new StringBuilder(new String(collection));%0A            if (sb.toString().equals(%22%5B%5D%22)) %7B%0A                sb = new StringBuilder();%0A                sb.append(%22%5B%22);%0A            %7D else %7B%0A                sb.deleteCharAt(sb.length() - 1);%0A                sb.append(%22,%22);%0A            %7D%0A            sb.append(new String(e.getRawComponent()));%0A            sb.append(%22%5D%22);%0A            collection = sb.toString().getBytes(%22UTF-8%22);%0A%0A        %7D catch (IOException ex) %7B%0A            log.error(%22Fail to insert record caused by: %7B%7D%22, ex.getMessage());%0A            return false;%0A        %7D%0A        return true;%0A    %7D
 %0A%0A    privat

Implement the add method in the AceRecordCollection class