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() : 1;
		this.col2 = this.params.containsKey(RuleParam._JOIN_COL2) ? this.params.get(RuleParam._JOIN_COL2).asInt() : 1;		
	}	

	@Override
	public String process(String line1, String line2) throws MixException {
		String join = null;
		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));
				join = part1 + DEFAULT_CELL_SEPARATOR  + part2 + DEFAULT_CELL_SEPARATOR + part3;				
			}
		}
		return join;
	}

}

@@ -1016,16 +1016,291 @@
 %09%09%0A%09%7D%09%0A%0A
+%09/**%0A %09* Returns the joined lines in a LineResult object.%0A%09* @param line1 The first line to join%0A %09* @param line2 The second line to join%0A %09* @return The alternated line%0A %09* @throws MixException - If an mixing error occurs%0A%09* @see innovimax.mixthem.operation.LineResult%0A %09*/%0A
 %09@Overri
@@ -1302,16 +1302,16 @@
 verride%0A
-
 %09public 
@@ -1381,26 +1381,44 @@
 %7B%0A%09%09
-String join = null
+LineResult result = new LineResult()
 ;%0A%09%09
@@ -2025,15 +2025,25 @@
 %09%09%09%09
-join = 
+reseut.setResult(
 part
@@ -2114,22 +2114,105 @@
 art3
-;%09%09%09%09%0A%09%09%09%7D%0A%09%09%7D
+);%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09result.ignoreResult();%0A%09%09%7D%0A%09%09result.exploreBoth();  // TODO: preserve lines
 %0A%09%09r
@@ -2217,20 +2217,22 @@
 %09return 
-join
+result
 ;%0A%09%7D%0A%0A%7D%0A

Update DefaultLineJoining.java