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, ProcessException {
		if (line1 == null || line2 == null) {
			return null;
		} else {
			String zip = null;
			String sep = DEFAULT_ZIP_SEPARATOR;
			if (this.params.containsKey(RuleParam._ZIP_SEP)) {
				sep = this.params.get(RuleParam._ZIP_SEP).asString();
			}		
			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;
		}
	}

}

@@ -881,239 +881,8 @@
 %7D%0A%09%0A
-%09/**%0A %09* Returns the result of zipping two lines.%0A%09* @param line1 The first line to zip%0A %09* @param line2 The second line to zip%09%0A %09* @return The result of zipping two lines%0A %09* @throws MixException - If an mixing error occurs%0A %09*/%0A
 %09@Ov
@@ -1026,33 +1026,42 @@
 %0A%09%09%09
-return null;%0A%09%09%7D else %7B%0A%09
+throw new ProcessException();%0A%09%09%7D%0A
 %09%09St
@@ -1079,17 +1079,16 @@
 null;%0A%09%09
-%09
 String s
@@ -1115,17 +1115,16 @@
 ARATOR;%0A
-%09
 %09%09if (th
@@ -1168,17 +1168,16 @@
 SEP)) %7B%0A
-%09
 %09%09%09sep =
@@ -1231,14 +1231,12 @@
 ;%0A%09%09
-%09
 %7D%09%09%0A
-%09
 %09%09sw
@@ -1253,25 +1253,24 @@
 .type) %7B%0A%09%09%09
-%09
 case _LINE:%0A
@@ -1269,17 +1269,16 @@
  _LINE:%0A
-%09
 %09%09%09%09zip 
@@ -1308,17 +1308,15 @@
 %09%09%09%09
-%09
 break;%0A
-%09
 %09%09%09c
@@ -1331,25 +1331,24 @@
 L:%09%09%09%09%09%0A%09%09%09%09
-%09
 List%3CString%3E
@@ -1387,25 +1387,24 @@
 it(%22%5C%5Cs%22));%0A
-%09
 %09%09%09%09List%3CStr
@@ -1451,17 +1451,16 @@
 %5C%5Cs%22));%0A
-%09
 %09%09%09%09int 
@@ -1474,17 +1474,16 @@
  0;%0A%09%09%09%09
-%09
 while (i
@@ -1535,17 +1535,16 @@
 %7B%09%09%09%09%09%09%0A
-%09
 %09%09%09%09%09if 
@@ -1560,25 +1560,24 @@
  0) %7B%0A%09%09%09%09%09%09
-%09
 zip = %22%22;%0A%09%09
@@ -1579,17 +1579,16 @@
 %22;%0A%09%09%09%09%09
-%09
 %7D else %7B
@@ -1588,17 +1588,16 @@
  else %7B%0A
-%09
 %09%09%09%09%09%09zi
@@ -1625,17 +1625,16 @@
 parator%0A
-%09
 %09%09%09%09%09%7D%0A%09
@@ -1633,25 +1633,24 @@
 %09%09%09%09%09%7D%0A%09%09%09%09%09
-%09
 zip += list1
@@ -1692,17 +1692,16 @@
 );%0A%09%09%09%09%09
-%09
 index++;
@@ -1709,17 +1709,14 @@
 %09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D%0A
-%09
 %09%09re
@@ -1725,18 +1725,14 @@
 rn zip;%0A
-%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

update zip operation