package innovimax.mixthem.join;

import innovimax.mixthem.exceptions.MixException;
import innovimax.mixthem.interfaces.IJoinLine;

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

/**
* <p>Joins two lines on a common field.</p>
* <p>This is the default implementation of IJoinLine.</p>
* @see IJoinLine
* @author Innovimax
* @version 1.0
*/
public class DefaultLineJoining implements IJoinLine {

	@Override
	public String join(String line1, String line2) throws MixException {
		if (line1 != null && line2 != null) {
			List<String> list1 = Arrays.asList(line1.split("\\s"));
			List<String> list2 = Arrays.asList(line2.split("\\s"));
			if (list1.size() > 0 && list2.contains(list1.get(0))) {
				String part1 = list1.stream().collect(Collectors.joining(" "));
				String part2 = list2.stream().filter(s -> !list1.contains(s)).collect(Collectors.joining(" "));
				return part1 + " " + part2;				
			}
		}
		return null;
	}

	@Override
	public String join(String line1, String line2, int index) throws MixException {
		if (line1 != null && line2 != null) {
			List<String> list1 = Arrays.asList(line1.split("\\s"));
			List<String> list2 = Arrays.asList(line2.split("\\s"));
			if (list1.size() <= index && list2.size() <= index && list1.get(index).equals(list2.get(index))) {
				String part1 = list1.get(index);					
				String part2 = list1.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(" "));
				String part3 = list2.stream().filter(s -> !list1.contains(s)).collect(Collectors.joining(" "));
				return part1 + " " + part2 + " " + part3;				
			}
		}
		return null;
	}

	@Override
	public String join(String line1, String line2, int index1, int index2) throws MixException {
		System.out.println("join col1 col2 todo");
		return null;
	}

}

@@ -1040,32 +1040,203 @@
  MixException %7B%0A
+%09%09System.out.println(%22trace%3E join on same column : %22 + index);%0A%09%09System.out.println(%22trace%3E first line : %22 + line1);%0A%09%09System.out.println(%22trace%3E first line : %22 + line2);%0A
 %09%09if (line1 != n
@@ -1389,33 +1389,33 @@
 f (list1.size() 
-%3C
+%3E
 = index && list2
@@ -1422,17 +1422,17 @@
 .size() 
-%3C
+%3E
 = index 
@@ -1449,16 +1449,20 @@
 et(index
+ - 1
 ).equals
@@ -1477,16 +1477,20 @@
 et(index
+ - 1
 ))) %7B%0A%09%09
@@ -1525,15 +1525,14 @@
 ndex
-);%09%09%09%09%09
+ - 1);
 %0A%09%09%09

Update DefaultLineJoining.java