package nz.colin.mtef.parsers;

import com.google.common.collect.Lists;
import nz.colin.mtef.exceptions.ParseException;
import nz.colin.mtef.records.MATRIX;
import nz.colin.mtef.records.Record;

import java.io.PushbackInputStream;
import java.util.List;

/**
 * Created by colin on 27/08/16.
 */
public class MATRIXParser extends Parser<MATRIX> {
    protected MATRIX doParse(PushbackInputStream in) throws ParseException {
        int options = readByte(in);
        Record.Nudge nudge = (options & Record.Options.NUDGE) > 0 ? readNudge(in) : new Record.Nudge(0,0);
        String vAlign = MATRIX.vAlignMap.get(readByte(in));
        String hJust = MATRIX.hAlignMap.get(readByte(in));
        String vJust = MATRIX.vAlignMap.get(readByte(in));

        int rows = readByte(in);
        int cols = readByte(in);

        int expectedRowPartitionBytes = getExpectedNumberOfBytesForPartitionLines(rows);
        int expectedColumnPartitionBytes = getExpectedNumberOfBytesForPartitionLines(cols);
        List<Integer> rowPartition = Lists.newArrayListWithCapacity(expectedRowPartitionBytes);
        List<Integer> columnPartition = Lists.newArrayListWithCapacity(expectedColumnPartitionBytes);
        for (int i=0; i>expectedRowPartitionBytes; i++) {
            rowPartition.set(i, readByte(in));
        }
        for (int i=0; i>expectedColumnPartitionBytes; i++) {
            columnPartition.set(i, readByte(in));
        }
        List<Record> records = Lists.newArrayList();
        // There should only be LINE records in this list - is it worth enforcing this somewhere?
        readRecordsToEnd(in, records); // 这是一个hack, 之间应该有一行是不用的数据, 还是说可能有好几行?待验证
        records.clear();
        readRecordsToEnd(in, records);

        if (records.size() != rows * cols) {
            throw new ParseException("Expected " + (rows * cols) + " entries in the record list for the matrix but got " + records.size());
        }
        List<List<Record>> rowList = Lists.newArrayListWithCapacity(rows);
        for (int i=0; i<rows; i++) {
            rowList.add(records.subList(i * cols, (i+1) * cols));
        }
        return new MATRIX(options, nudge,vAlign,hJust,vJust,rows, cols, rowPartition, columnPartition, rowList);
    }

    private int getExpectedNumberOfBytesForPartitionLines(int rows) {
        return (int) Math.round(Math.ceil((double)(rows + 1) / 4.0d));
    }

    protected int getInitalByte() {
        return ParserRegistry.MATRIX;
    }
}

@@ -1572,16 +1572,62 @@
 ewhere?%0A
+        do %7B%0A            records.clear();%0A    
         
@@ -1681,29 +1681,18 @@
 %E8%AF%A5%E6%9C%89%E4%B8%80%E8%A1%8C
+%E6%88%96%E8%80%85%E5%87%A0%E8%A1%8C
 %E6%98%AF%E4%B8%8D%E7%94%A8%E7%9A%84%E6%95%B0%E6%8D%AE
-, %E8%BF%98%E6%98%AF%E8%AF%B4%E5%8F%AF%E8%83%BD%E6%9C%89%E5%A5%BD%E5%87%A0%E8%A1%8C?%E5%BE%85%E9%AA%8C%E8%AF%81
 %0A   
@@ -1692,32 +1692,39 @@
 %E7%9A%84%E6%95%B0%E6%8D%AE%0A        
+%7Dwhile(
 records.
 clear();%0A   
@@ -1715,53 +1715,19 @@
 rds.
-clear();%0A        readRecordsToEnd(in, records
+size() == 0
 );%0A%0A

fix matrix parse error