package innovimax.mixthem.operation;

import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.RuleParam;
import innovimax.mixthem.arguments.ParamValue;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* <p>Zips two or more lines cell by cell.</p>
* <p>Zipping stops when either a line is missing or a cell is missing.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultCellZipping extends DefaultLineZipping {
	
	/**
	* Constructor	
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.arguments.RuleParam
	* @see innovimax.mixthem.arguments.ParamValue
	*/
	public DefaultCellZipping(final Map<RuleParam, ParamValue> params) {
		super(params);		
	}
	
	@Override
	public void process(final List<String> lineRange, final LineResult result) throws MixException {
		result.reset();
		//System.out.println("RANGE="+lineRange.toString());
		if (linesZipable(lineRange)) {
			StringBuilder zip = new StringBuilder();
			final List<Iterator<String>> cellIterators = new ArrayList<Iterator<String>>();
			for (String line : lineRange) {
				cellIterators.add(Arrays.asList(line.split(CellOperation.DEFAULT_SPLIT_CELL_REGEX.getValue().asString())).iterator());
			}				
			while (hasCellRange(cellIterators)) {
				if (zip.length() > 0) {
					zip.append(CellOperation.DEFAULT_CELL_SEPARATOR.getValue().asString());
				}
				final List<String> cellRange = nextCellRange(cellIterators);
				//System.out.println("CELLS="+cellRange);
				int index = 0;
				for (String cell : cellRange) {
					if (index > 0) {
						zip.append(this.sep);
					}
					zip.append(cell);
					index++;
				}
			}			
			result.setResult(zip.toString());
		}
	}

	private boolean hasCellRange(List<Iterator<String>> cellIterators) {
		for (Iterator<String> cellIterator : cellIterators) {
			if (!cellIterator.hasNext()) {
				return false;
			}
		}
		return true;
	}
	
	private List<String> nextCellRange(List<Iterator<String>> cellIterators) {
		final List<String> cellRange = new ArrayList<String>();
		for (Iterator<String> cellIterator : cellIterators) {
			cellRange.add(cellIterator.next());
		}
		return cellRange;
	}
	
}

@@ -991,42 +991,8 @@
 ));%0A
-%09%09if (linesZipable(lineRange)) %7B%0A%09
 %09%09St
@@ -1030,17 +1030,16 @@
 lder();%0A
-%09
 %09%09final 
@@ -1114,17 +1114,16 @@
 %3E%3E();%0A%09%09
-%09
 for (Str
@@ -1146,17 +1146,16 @@
 ange) %7B%0A
-%09
 %09%09%09cellI
@@ -1274,16 +1274,14 @@
 ;%0A%09%09
-%09
 %7D%09%09%09%09%0A
-%09
 %09%09wh
@@ -1319,17 +1319,16 @@
 )) %7B%0A%09%09%09
-%09
 if (zip.
@@ -1339,25 +1339,24 @@
 th() %3E 0) %7B%0A
-%09
 %09%09%09%09zip.appe
@@ -1422,20 +1422,18 @@
 ());%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09final
@@ -1488,17 +1488,16 @@
 ators);%0A
-%09
 %09%09%09//Sys
@@ -1536,17 +1536,16 @@
 ge);%0A%09%09%09
-%09
 int inde
@@ -1551,17 +1551,16 @@
 ex = 0;%0A
-%09
 %09%09%09for (
@@ -1590,17 +1590,16 @@
 ) %7B%0A%09%09%09%09
-%09
 if (inde
@@ -1607,17 +1607,16 @@
  %3E 0) %7B%0A
-%09
 %09%09%09%09%09zip
@@ -1638,20 +1638,18 @@
 p);%0A%09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%09zip.
@@ -1662,17 +1662,16 @@
 (cell);%0A
-%09
 %09%09%09%09inde
@@ -1682,20 +1682,17 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D%09%09%09%0A
-%09
 %09%09re
@@ -1723,20 +1723,16 @@
 ing());%0A
-%09%09%7D%0A
 %09%7D%0A%0A%09pri

Update DefaultCellZipping.java