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.Map;
import java.util.List;

/**
* <p>Zips two lines on a common field.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineZipping extends AbstractLineOperation {
	
	private final static String DEFAULT_ZIP_SEPARATOR = "";

	private final ZipType type;
	
	/**
	* Constructor
	* @param type The type of zip to process
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.operation.ZipType
	* @see innovimax.mixthem.arguments.RuleParam
	* @see innovimax.mixthem.arguments.ParamValue
	*/
	public DefaultLineZipping(ZipType type, Map<RuleParam, ParamValue> params) {
		super(params);
		this.type = type;		
	}
	
	/**
 	* Returns the result of zipping two lines.
	* @param line1 The first line to zip
 	* @param line2 The second line to zip	
 	* @return The result of zipping two lines
 	* @throws MixException - If an mixing error occurs
 	*/
	@Override
	public String process(String line1, String line2) throws MixException {
		String zip = null;
		String sep = DEFAULT_ZIP_SEPARATOR;
		if (this.params.containsKey(RuleParam._ZIP_SEP)) {
			sep = this.params.get(RuleParam._ZIP_SEP).asString();
		}		
		if (line1 != null && line2 != null) {
			switch (this.type) {
				case _LINE:
					zip = line1 + sep + line2;
					break;
				case _CELL:					
					List<String> list1 = Arrays.asList(line1.split("\\s"));
					List<String> list2 = Arrays.asList(line2.split("\\s"));
					int index = 0;
					while (index < list1.size() && index < list2.size()) {						
						if (index == 0) {
							zip = "";
						} else {
							zip += " ";  // cell separator
						}
						zip += list1.get(index) + sep + list2.get(index);
						index++;
					}
			}
		}
		return zip;
	}

}

@@ -1371,49 +1371,8 @@
 %7D%09%09%0A
-%09%09if (line1 != null && line2 != null) %7B%0A%09
 %09%09sw
@@ -1389,25 +1389,24 @@
 .type) %7B%0A%09%09%09
-%09
 case _LINE:%0A
@@ -1405,17 +1405,16 @@
  _LINE:%0A
-%09
 %09%09%09%09zip 
@@ -1444,17 +1444,15 @@
 %09%09%09%09
-%09
 break;%0A
-%09
 %09%09%09c
@@ -1463,25 +1463,24 @@
 _CELL:%09%09%09%09%09%0A
-%09
 %09%09%09%09List%3CStr
@@ -1523,25 +1523,24 @@
 it(%22%5C%5Cs%22));%0A
-%09
 %09%09%09%09List%3CStr
@@ -1591,17 +1591,16 @@
 ));%0A%09%09%09%09
-%09
 int inde
@@ -1606,17 +1606,16 @@
 ex = 0;%0A
-%09
 %09%09%09%09whil
@@ -1676,17 +1676,16 @@
 %09%09%0A%09%09%09%09%09
-%09
 if (inde
@@ -1694,17 +1694,16 @@
 == 0) %7B%0A
-%09
 %09%09%09%09%09%09zi
@@ -1715,17 +1715,16 @@
 %22;%0A%09%09%09%09%09
-%09
 %7D else %7B
@@ -1724,17 +1724,16 @@
  else %7B%0A
-%09
 %09%09%09%09%09%09zi
@@ -1766,20 +1766,18 @@
 or%0A%09%09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%09%09zip
@@ -1828,17 +1828,16 @@
 );%0A%09%09%09%09%09
-%09
 index++;
@@ -1838,22 +1838,16 @@
 dex++;%0A%09
-%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

remove useless if