package innovimax.mixthem.operation;

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 {
	
	/**
 	* @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);
	}	

	@Override
	public String process(String line1, String line2) throws MixException {
		String join = null;
		int col1 = 1;
		int col2 = 1;
		if (this.params.containsKey(RuleParam._JOIN_COL1)) {
			col1 = this.params.get(RuleParam._JOIN_COL1).asInt();
			col2 = col1;
		}
		if (this.params.containsKey(RuleParam._JOIN_COL2)) {
			col2 = this.params.get(RuleParam._JOIN_COL2).asInt();			
		}
		List<String> list1 = Arrays.asList(line1.split("\\s"));
		List<String> list2 = Arrays.asList(line2.split("\\s"));		
		if (list1.size() >= col1 && list2.size() >= col2 && list1.get(col1 - 1).equals(list2.get(col2 - 1))) {
			String part1 = list1.get(col1 - 1);
			String part2 = list1.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(" "));
			String part3 = list2.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(" "));
			join = part1 + " " + part2 + " " + part3;				
		}		
		return join;
	}

}

@@ -31,16 +31,64 @@
 ation;%0A%0A
+import static innovimax.mixthem.MixConstants.*;%0A
 import i
@@ -1174,21 +1174,40 @@
 1.split(
-%22%5C%5Cs%22
+DEFAULT_SPLIT_CELL_REGEX
 ));%0A%09%09Li
@@ -1255,13 +1255,32 @@
 lit(
-%22%5C%5Cs%22
+DEFAULT_SPLIT_CELL_REGEX
 ));%09
@@ -1511,27 +1511,46 @@
 ors.joining(
-%22 %22
+DEFAULT_CELL_SEPARATOR
 ));%0A%09%09%09Strin
@@ -1635,50 +1635,108 @@
 ing(
-%22 %22));%0A%09%09%09join = part1 + %22 %22 + part2 + %22 %22
+DEFAULT_CELL_SEPARATOR));%0A%09%09%09join = part1 + DEFAULT_CELL_SEPARATOR  + part2 + DEFAULT_CELL_SEPARATOR
  + p

use mix constants