package innovimax.mixthem.operation;

import static innovimax.mixthem.MixConstants.*;
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 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;		
	}
	
	@Override
	public String process(String line1, String line2) throws MixException, ProcessException {
		if (line1 == null || line2 == null) {
			throw new ProcessException();
		}
		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;
	}

}

@@ -237,16 +237,43 @@
 Arrays;%0A
+import java.util.Iterator;%0A
 import j
@@ -979,105 +979,10 @@
 tion
-, ProcessException %7B%0A%09%09if (line1 == null %7C%7C line2 == null) %7B%0A%09%09%09throw new ProcessException();%0A%09%09%7D
+ %7B
 %0A%09%09S
@@ -1201,21 +1201,44 @@
 %09%09zip = 
+(
 line1
+ != null ? line1 : %22%22)
  + sep +
@@ -1238,21 +1238,44 @@
 + sep + 
+(
 line2
+ != null ? line2 : %22%22)
 ;%0A%09%09%09%09br
@@ -1323,16 +1323,32 @@
  list1 =
+ line1 != null ?
  Arrays.
@@ -1369,24 +1369,50 @@
 plit(%22%5C%5Cs%22))
+ : Collections.emptyList()
 ;%0A%09%09%09%09List%3CS
@@ -1425,16 +1425,32 @@
  list2 =
+ line2 != null ?
  Arrays.
@@ -1479,163 +1479,436 @@
 s%22))
-;%0A%09%09%09%09int index = 0;%0A%09%09%09%09while (index %3C list1.size() && index %3C list2.size()) %7B%09%09%09%09%09%09%0A%09%09%09%09%09if (index == 0) %7B%0A%09%09%09%09%09%09zip = %22%22;%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09zip += 
+ : Collections.emptyList();%0A%09%09%09%09Iterator%3CString%3E iterator1 = list1.iterator();%0A%09%09%09%09Iterator%3CString%3E iterator2 = list2.iterator();%0A%09%09%09%09StringBuffer buf = new StringBuffer();%0A%09%09%09%09while (iterator1.hasNext() %7C%7C iterator2.hasNext()) %7B%09%09%09%09%09%09%0A%09%09%09%09%09String cell1 = iterator1.hasNext() ? iterator1.next() : %22%22;%0A%09%09%09%09%09String cell2 = iterator2.hasNext() ? iterator2.next() : %22%22;%09%09%09%09%09%0A%09%09%09%09%09if (buf.length() %3E 0) %7B%09%09%09%09%09%09%0A%09%09%09%09%09%09buff.append(
 %22 %22
+)
 ;  /
@@ -1934,83 +1934,88 @@
 %09%09%09%7D
+%09%09%09%09%09
 %0A%09%09%09%09%09
-zip += list1.get(index) + sep + list2.get(index
+buf.append(cell1 + sep + cell2
 );
-%0A
 %09%09%09%09%09
-index++;%0A%09%09%09%09%7D
+%0A%09%09%09%09%7D%0A%09%09%09%09zip = buf.toString();
 %0A%09%09%7D

update zip operation, remove process exception