package innovimax.mixthem.operation;

import static innovimax.mixthem.MixConstants.*;
import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.ParamValue;
import innovimax.mixthem.arguments.RuleParam;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* <p>Joins two lines on a common field.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineJoining extends AbstractLineOperation {
	
	private final int col1;
	private final int col2;
	
	/**
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.operation.RuleParam
	* @see innovimax.mixthem.operation.ParamValue
	*/
	public DefaultLineJoining(Map<RuleParam, ParamValue> params) {
		super(params);
		this.col1 = this.params.containsKey(RuleParam._JOIN_COL1) ? this.params.get(RuleParam._JOIN_COL1).asInt() : JoinOperation.DEFAULT_JOIN_COLUMN.toInteger();
		this.col2 = this.params.containsKey(RuleParam._JOIN_COL2) ? this.params.get(RuleParam._JOIN_COL2).asInt() : JoinOperation.DEFAULT_JOIN_COLUMN.toInteger();
	}	

	@Override
	public void process(String line1, String line2, LineResult result) throws MixException {
		result.resetTypes();
		if (line1 !=null && line2 != null) {
			List<String> list1 = Arrays.asList(line1.split(DEFAULT_SPLIT_CELL_REGEX));
			List<String> list2 = Arrays.asList(line2.split(DEFAULT_SPLIT_CELL_REGEX));		
			if (list1.size() >= this.col1 && list2.size() >= this.col2 && list1.get(this.col1 - 1).equals(list2.get(this.col2 - 1))) {
				String part1 = list1.get(this.col1 - 1);
				String part2 = list1.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(DEFAULT_CELL_SEPARATOR));
				String part3 = list2.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(DEFAULT_CELL_SEPARATOR));
				result.setResult(part1 + DEFAULT_CELL_SEPARATOR  + part2 + DEFAULT_CELL_SEPARATOR + part3);
			}
		} else {
			result.ignoreResult();
		}
		result.exploreBothFiles();  // TODO: preserve lines
	}

}

@@ -1311,24 +1311,38 @@
 line1.split(
+CellOperation.
 DEFAULT_SPLI
@@ -1353,16 +1353,27 @@
 LL_REGEX
+.toString()
 ));%0A%09%09%09L
@@ -1418,16 +1418,30 @@
 2.split(
+CellOperation.
 DEFAULT_
@@ -1456,16 +1456,27 @@
 LL_REGEX
+.toString()
 ));%09%09%0A%09%09
@@ -1727,32 +1727,46 @@
 lectors.joining(
+CellOperation.
 DEFAULT_CELL_SEP
@@ -1763,32 +1763,43 @@
 T_CELL_SEPARATOR
+.toString()
 ));%0A%09%09%09%09String p
@@ -1877,16 +1877,30 @@
 joining(
+CellOperation.
 DEFAULT_
@@ -1913,16 +1913,27 @@
 EPARATOR
+.toString()
 ));%0A%09%09%09%09
@@ -1957,16 +1957,30 @@
 part1 + 
+CellOperation.
 DEFAULT_
@@ -1997,20 +1997,53 @@
 ATOR
-  + part2 + 
+.toString()  + %0A%09%09%09%09%09%09 part2 + CellOperation.
 DEFA
@@ -2060,16 +2060,27 @@
 EPARATOR
+.toString()
  + part3

Update DefaultLineJoining.java