Updated expanded exclusion span to allow "not clause" in the "main clause"
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java
index 853b1a3..80904f2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java
@@ -13,6 +13,11 @@
 
 import de.ids_mannheim.korap.query.SpanExpansionQuery;
 
+/** Spans expanded with min m tokens and max n tokens, and throughout all 
+ * 	the expansions do not contain the notClause. 
+ * 
+ * 	@author margaretha
+ * */
 public class ExpandedExclusionSpans extends SimpleSpans{
 	
 	private int min, max;
@@ -57,7 +62,8 @@
 
 	private boolean advance() throws IOException {
 		while (hasMoreSpans || candidateSpans.size() > 0){
-			if (candidateSpans.size() > 0){				
+			if (candidateSpans.size() > 0){
+				// set a candidate span as a match
 				CandidateSpan cs = candidateSpans.get(0);
 				matchDocNumber = cs.getDoc();
 				matchStartPosition = cs.getStart();
@@ -77,87 +83,86 @@
 	
 	private void findMatches() throws IOException {
 		while (hasMoreNotClause &&	notClause.doc() <= firstSpans.doc()){
-			
 			if (notClause.doc() == firstSpans.doc()){ 
-				
 				if (direction < 0 ){ // left
-					int counter = max;
-					int maxPos = max;
-					CandidateSpan lastNotClause = null;
-					while (hasMoreNotClause && 
-							notClause.start() < firstSpans.start()){
-						
-						// between max and firstspan.start()
-						if (notClause.start() >= firstSpans.start() - counter){
-							maxPos = firstSpans.start() - notClause.start() -1;
-							lastNotClause = new CandidateSpan(notClause);
-							counter--;
-						} 
-						if (!notClause.next()) hasMoreNotClause = false;
-					}									
-					
-					// if a notClause is between max and firstspan.start, 
-					// then maxPos = last NotClause pos -1
-					generateCandidates(min, maxPos, direction);
-					
-					if (lastNotClause != null)
-						while ((hasMoreSpans = firstSpans.next())
-								// the next notClause is not in between max and firstspan.start()
-								&& notClause.start() > firstSpans.start()
-								// the last notClause is in between max and firstspan.start()
-								&& lastNotClause.getStart() < firstSpans.start()								
-								&& lastNotClause.getStart() >= firstSpans.start() - max
-							){
-														
-							maxPos = firstSpans.start() - lastNotClause.getStart() -1;
-							generateCandidates(min, maxPos, direction);
-						}
-					else hasMoreSpans = firstSpans.next();
-				}
-				
-				else { // right
-					
-					int expansionEnd = firstSpans.end() + max;
-					int maxPos = max;
-					boolean isFound = false;
-					
-					CandidateSpan firstNotClause = null;
-					//System.out.println("main start:"+firstSpans.start());
-					while (hasMoreNotClause && notClause.start() < expansionEnd){
-						// between firstspan.end() and expansionEnd
-						if (!isFound && notClause.start() >= firstSpans.start()){							
-							maxPos = notClause.start() - firstSpans.start() -1;
-							firstNotClause = new CandidateSpan(notClause);
-							isFound = true;
-						}						
-						if (!notClause.next()) hasMoreNotClause = false;
-					}
-					// if a notClause is between firstSpan.end and max
-					// then maxPos = the first notClause pos -1 
-					generateCandidates(min, maxPos, direction);
-					
-					if (firstNotClause !=null){
-						while ((hasMoreSpans = firstSpans.next()) 
-								// in between
-								&& firstNotClause.getStart() < firstSpans.end() + max
-								&& firstNotClause.getStart() >= firstSpans.start())								
-							{
-							//System.out.println("first start:"+firstNotClause.getStart()+", main start:"+firstSpans.start());
-							maxPos = firstNotClause.getStart() - firstSpans.start() -1;
-							generateCandidates(min, maxPos, direction);
-						} 
-					}
-					else hasMoreSpans = firstSpans.next();
-				}
-				
-				
+					expandLeft();
+				} // right
+				else { 	expandRight(); }
 				break;
 			}
-			
 			else if (!notClause.next()) hasMoreNotClause = false;
 		}
 	}
 	
+	private void expandLeft() throws IOException{
+		//int counter = max;
+		int maxPos = max;
+		CandidateSpan lastNotClause = null;
+		while (hasMoreNotClause && 
+				notClause.start() < firstSpans.start()){
+			
+			// between max and firstspan.start()
+			if (notClause.start() >= firstSpans.start() - maxPos){
+				maxPos = firstSpans.start() - notClause.start() -1;
+				lastNotClause = new CandidateSpan(notClause);
+				//counter--;
+			} 
+			if (!notClause.next()) hasMoreNotClause = false;
+		}									
+		
+		// if a notClause is between max and firstspan.start, 
+		// then maxPos = last NotClause pos -1
+		generateCandidates(min, maxPos, direction);
+		
+		if (lastNotClause != null)
+			while ((hasMoreSpans = firstSpans.next())
+					// the next notClause is not in between max and firstspan.start()
+					&& notClause.start() > firstSpans.start()
+					// the last notClause is in between max and firstspan.start()
+					&& lastNotClause.getStart() < firstSpans.start()								
+					&& lastNotClause.getStart() >= firstSpans.start() - max
+				){
+											
+				maxPos = firstSpans.start() - lastNotClause.getStart() -1;
+				generateCandidates(min, maxPos, direction);
+			}
+		else hasMoreSpans = firstSpans.next();
+	}
+	
+	private void expandRight() throws IOException{
+		int expansionEnd = firstSpans.end() + max;
+		int maxPos = max;
+		boolean isFound = false;
+		
+		CandidateSpan firstNotClause = null;
+		//System.out.println("main start:"+firstSpans.start());
+		while (hasMoreNotClause && notClause.start() < expansionEnd){
+			// between firstspan.end() and expansionEnd
+			if (!isFound && notClause.start() >= firstSpans.end()){							
+				maxPos = notClause.start() - firstSpans.end() -1;
+				firstNotClause = new CandidateSpan(notClause);
+				isFound = true;
+			}						
+			if (!notClause.next()) hasMoreNotClause = false;
+		}
+		// if a notClause is between firstSpan.end and max
+		// then maxPos = the first notClause pos -1 
+		generateCandidates(min, maxPos, direction);
+		
+		if (firstNotClause !=null){
+			while ((hasMoreSpans = firstSpans.next()) 
+					// in between
+					&& firstNotClause.getStart() < firstSpans.end() + max
+					&& firstNotClause.getStart() >= firstSpans.end())								
+				{
+				//System.out.println("first start:"+firstNotClause.getStart()+", main start:"+firstSpans.start());
+				maxPos = firstNotClause.getStart() - firstSpans.end() -1;
+				generateCandidates(min, maxPos, direction);
+			} 
+		}
+		else hasMoreSpans = firstSpans.next();
+	}
+	
 	private void generateCandidates(int minPos, int maxPos, int direction) 
 			throws IOException {
 		int counter;