Fixed bug (sorting in unordered distance spans)

Change-Id: Ic9c6faff2ad0b965829398ebfeee129d345b4da8
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceSpans.java
index 5060b31..ac8fe24 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceSpans.java
@@ -14,16 +14,12 @@
 
 /**
  * Span enumeration of element-based distance span matches. Each match
- * consists
- * of two child spans. The element-distance between the child spans is
- * the
- * difference between the element position numbers where the child
- * spans are.
- * The element-distance unit can be a sentence or a paragraph. All
- * other child
- * spans' occurrences which are not in a sentence or a paragraph (with
- * respect
- * to the element distance type currently used), are ignored.
+ * consists of two child spans. The element-distance between the child
+ * spans is the difference between the element position numbers where
+ * the child spans are. The element-distance unit can be a sentence or
+ * a paragraph. All other child spans' occurrences which are not in a
+ * sentence or a paragraph (with respect to the element distance type
+ * currently used), are ignored.
  * 
  * Note: elements cannot overlap with each other.
  * 
@@ -105,8 +101,7 @@
 
     /**
      * Add new possible (candidate) firstspans. Candidate firstspans
-     * must be in
-     * an element and not too far from the secondspan.
+     * must be in an element and not too far from the secondspan.
      * 
      * @throws IOException
      */
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java
index 2b93ece..60e2a8a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java
@@ -100,12 +100,10 @@
 
     /**
      * Sets the list of matches for the span having the smallest
-     * position (i.e.
-     * between the first and the second spans), and its candidates
-     * (i.e. its
-     * counterparts). The candidates also must have smaller positions.
-     * Simply
-     * remove the span if it does not have any candidates.
+     * position (i.e. between the first and the second spans), and its
+     * candidates (i.e. its counterparts). The candidates also must
+     * have smaller positions. Simply remove the span if it does not
+     * have any candidates.
      * 
      * @throws IOException
      */
@@ -117,13 +115,13 @@
                 hasMoreSecondSpans, firstSpanList);
         // System.out.println("--------------------");
         // System.out.println("firstSpanList:");
-        // for (CandidateSpan cs: firstSpanList) {
-        // System.out.println(cs.getStart() +" "+ cs.getEnd());
+        // for (CandidateSpan cs : firstSpanList) {
+        // System.out.println(cs.getStart() + " " + cs.getEnd());
         // }
         //
         // System.out.println("secondSpanList:");
-        // for (CandidateSpan cs: secondSpanList) {
-        // System.out.println(cs.getStart() +" "+ cs.getEnd());
+        // for (CandidateSpan cs : secondSpanList) {
+        // System.out.println(cs.getStart() + " " + cs.getEnd());
         // }
 
         CandidateSpan currentFirstSpan, currentSecondSpan;
@@ -132,7 +130,7 @@
             currentFirstSpan = firstSpanList.get(0);
             currentSecondSpan = secondSpanList.get(0);
 
-            if (currentFirstSpan.getEnd() < currentSecondSpan.getEnd()
+            if (currentFirstSpan.getStart() < currentSecondSpan.getStart()
                     || isLastCandidateSmaller(currentFirstSpan,
                             currentSecondSpan)) {
                 // log.trace("current target: "
@@ -180,20 +178,16 @@
 
     /**
      * Tells if the last candidate from the secondSpanList has a
-     * smaller end
-     * position than the end position of the the last candidate from
-     * the
-     * firstSpanList.
+     * smaller end position than the end position of the the last
+     * candidate from the firstSpanList.
      * 
      * @param currentFirstSpan
      *            the current firstspan
      * @param currentSecondSpan
      *            the current secondspan
      * @return <code>true</code> if the end position of the last
-     *         candidate from
-     *         the secondSpanList is smaller than that from the
-     *         firstSpanList,
-     *         <code>false</code> otherwise.
+     *         candidate from the secondSpanList is smaller than that
+     *         from the firstSpanList, <code>false</code> otherwise.
      */
     private boolean isLastCandidateSmaller (CandidateSpan currentFirstSpan,
             CandidateSpan currentSecondSpan) {
@@ -285,7 +279,6 @@
                 payloads.addAll(cs.getPayloads());
             }
         }
-
         CandidateSpan match = new CandidateSpan(start, end, doc, cost, payloads);
         match.setChildSpan(cs);
         return match;
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java
index 3e50e76..0b8b055 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java
@@ -137,14 +137,16 @@
             }
 
             // left candidate
-            if (cs.getEnd() < target.getStart())
+            if (cs.getEnd() < target.getStart()) {
                 actualDistance = target.getStart() - cs.getEnd() + 1;
-            else
+            }
+            else {
                 // right candidate
                 actualDistance = cs.getStart() - target.getEnd() + 1;
-
-            if (minDistance <= actualDistance && actualDistance <= maxDistance)
+            }
+            if (minDistance <= actualDistance && actualDistance <= maxDistance) {
                 matches.add(createMatchCandidate(target, cs, false));
+            }
         }
         return matches;
     }