Removed KorapSpans
diff --git a/Changes b/Changes
index 38fca92..edeae96 100644
--- a/Changes
+++ b/Changes
@@ -9,7 +9,8 @@
 	  Warning: This is a breaking change!
 	- [performance] Payloads in ElementSpans can now be lazily loaded (diewald)
 	- [cleanup] Renamed /filter to /collection, 
-	  merge KorapHTML and KorapString (diewald)
+	  merge KorapHTML and KorapString,
+	  removed KorapSpan, KorapTermSpan and KorapLongSpan (diewald)
 
 0.49.3 2015-02-03
         - [documentation] Improved documentation for API classes (diewald)
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
index 4b087cd..411a50b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
@@ -17,11 +17,16 @@
 import org.slf4j.LoggerFactory;
 
 /**
+ * Add a payload to the span with an identification number (a class)
+ * And the start and end position of the span, so this information
+ * can bubble up for later processing (similar to captures in regular
+ * expression).
+ *
  * @author diewald
  */
 
 public class ClassSpans extends Spans {
-    private List<byte[]> highlightedPayload;
+    private List<byte[]> classedPayload;
     private final Spans spans;
     private byte number;
     private SpanQuery operand;
@@ -30,114 +35,128 @@
     private ByteBuffer bb = ByteBuffer.allocate(9);
 
     private final static Logger log = LoggerFactory.getLogger(ClassSpans.class);
+
     // This advices the java compiler to ignore all loggings
     public static final boolean DEBUG = false;
 
+    /**
+     * Construct a new ClassSpans object.
+     *
+     * @param operand An arbitrary nested {@link SpanQuery}.
+     * @param context The {@link AtomicReaderContext}.
+     * @param acceptDocs Bit vector representing the documents
+     *        to be searched in.
+     * @param termContexts A map managing {@link TermState TermStates}.
+     * @param number The identifying class number.
+     */
     public ClassSpans (SpanQuery operand,
-		       AtomicReaderContext context,
-		       Bits acceptDocs,
-		       Map<Term,TermContext> termContexts,
-		       byte number) throws IOException {
-	spans = operand.getSpans(context, acceptDocs, termContexts);
-	this.number = number;
-	this.operand = operand;
-	this.highlightedPayload = new ArrayList<byte[]>(6);
+                       AtomicReaderContext context,
+                       Bits acceptDocs,
+                       Map<Term,TermContext> termContexts,
+                       byte number) throws IOException {
+        spans = operand.getSpans(context, acceptDocs, termContexts);
+
+        // The number of the class
+        this.number  = number;
+
+        // The current operand
+        this.operand = operand;
+
+        // The highlighted payload
+        this.classedPayload = new ArrayList<byte[]>(3);
     };
 
+
     @Override
-    public Collection<byte[]> getPayload() throws IOException {
-	return highlightedPayload;
+    public Collection<byte[]> getPayload () throws IOException {
+        return classedPayload;
     };
 
+
     @Override
-    public boolean isPayloadAvailable() {
-	return true;
+    public boolean isPayloadAvailable () {
+        // We set payloads here - so it's always true
+        return true;
     };
 
-    public int doc() { return spans.doc(); }
 
-    // inherit javadocs
     @Override
-    public int start() { return spans.start(); }
+    public int doc () {
+        return spans.doc();
+    };
 
-    // inherit javadocs
+
     @Override
-    public int end() { return spans.end(); }
+    public int start () {
+        return spans.start();
+    };
 
 
-    // inherit javadocs
     @Override
-    public boolean next() throws IOException {
-	if (DEBUG)
-	    log.trace("Forward next");
+    public int end () {
+        return spans.end();
+    };
 
-	if (spans.next()) {
-		addClassPayload();
-	    return true;
-	};
-	hasmorespans = false;
-	return false;
+
+    @Override
+    public boolean next () throws IOException {
+        if (DEBUG) log.trace("Forward next");
+
+        if (spans.next())
+            return this.addClassPayload();
+
+        hasmorespans = false;
+        return false;
     };
     
-    private void addClassPayload() throws IOException {
+    private boolean addClassPayload () throws IOException {
     	hasmorespans = true;
 
-	    highlightedPayload.clear();
+	    classedPayload.clear();
 
+        // Subquery has payloads
 	    if (spans.isPayloadAvailable()) {
-		highlightedPayload.addAll(spans.getPayload());
-		if (DEBUG)
-		    log.trace("Found payload");
+            classedPayload.addAll(spans.getPayload());
+            if (DEBUG) log.trace("Found payload in nested SpanQuery");
 	    };
 
-	    if (DEBUG)
-		log.trace("Start to create class {} with span {} - {}",
-			  number,
-			  spans.start(),
-			  spans.end());
+	    if (DEBUG) {
+            log.trace(
+                "Wrap class {} around span {} - {}",
+                number,
+                spans.start(),
+                spans.end()
+            );
+        };
 
 	    // Todo: Better allocate using a Factory!
-
-	    //private
 	    bb.clear();
 	    bb.putInt(spans.start()).putInt(spans.end()).put(number);
-	   // System.out.println(number+":"+spans.start()+","+spans.end());
-	    /*
-	    if (DEBUG)
-		log.trace("Results in {} with {}", bb.toString(), bb.array());
-	    */
-	    // Add highlight information as byte after offsets
-	    highlightedPayload.add(bb.array());
-	    /*
-	    if (DEBUG) {
-		bb.rewind();
-		log.trace("That was a class from {}-{} of class {}", bb.getInt(), bb.getInt(), bb.get());
-	    };
-	    */
-		
-	}
 
-    // inherit javadocs
+	    // Add highlight information as byte array
+	    classedPayload.add(bb.array());
+        return true;
+	};
+
+
     @Override
-    public boolean skipTo(int target) throws IOException {
-	highlightedPayload.clear();
-	if (hasmorespans && spans.doc() < target &&
-			spans.skipTo(target)){
-		addClassPayload();
-	    return true;
-	}
-	return false;
+    public boolean skipTo (int target) throws IOException {
+        classedPayload.clear();
+        if (hasmorespans && spans.doc() < target && spans.skipTo(target))
+            return this.addClassPayload();
+        return false;
     };
 
+
     @Override
-    public String toString() {
-	return getClass().getName() + "(" + this.operand.toString() + ")@" +
-	    (doc() + ":" + start() + "-" + end());
+    public String toString () {
+        return getClass().getName() + "(" + this.operand.toString() + ")@" +
+            (doc() + ":" + start() + "-" + end());
     };
 
 
     @Override
     public long cost() {
-	return spans.cost();
-    }
+        return spans.cost();
+    };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
index fc2408e..cc9d064 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
@@ -20,8 +20,6 @@
 /**
  * Enumeration of special spans which length is stored in their payload,
  * representing elements such as phrases, sentences and paragraphs.
- *
- * Payloads are 
  * 
  * @author margaretha
  * @author diewald
@@ -33,10 +31,11 @@
     /**
      * Constructs ElementSpans for the given {@link SpanElementQuery}.
      * 
-     * @param spanElementQuery a SpanElementQuery
-     * @param context
-     * @param acceptDocs
-     * @param termContexts
+     * @param spanElementQuery A {@link SpanElementQuery}.
+     * @param context The {@link AtomicReaderContext}.
+     * @param acceptDocs Bit vector representing the documents
+     *        to be searched in.
+     * @param termContexts A map managing {@link TermState TermStates}.
      * @throws IOException
      */
     public ElementSpans(SpanElementQuery spanElementQuery,
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/KorapLongSpan.java b/src/main/java/de/ids_mannheim/korap/query/spans/KorapLongSpan.java
deleted file mode 100644
index 26a1367..0000000
--- a/src/main/java/de/ids_mannheim/korap/query/spans/KorapLongSpan.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package de.ids_mannheim.korap.query.spans;
-
-import de.ids_mannheim.korap.query.spans.KorapSpan;
-
-import java.util.Collection;
-
-public class KorapLongSpan extends KorapSpan {
-    public Collection<byte[]> payload;
-    
-    @Override
-    public Object clone() {
-	KorapLongSpan span = new KorapLongSpan();
-	span.start = this.start;
-	span.end = this.end;
-	span.doc = this.doc;
-	span.payload.addAll(this.payload);
-	return span;
-    };
-
-    public KorapSpan copyFrom (KorapLongSpan o) {
-	super.copyFrom((KorapSpan) o);
-	this.payload.addAll(o.payload);
-	return this;
-    };
-
-    @Override
-    public void clearPayload () {
-	if (this.payload != null)
-	    this.payload.clear();
-    };
-
-    @Override
-    public void initPayload () {
-    };
-
-    @Override
-    public String toString () {
-	StringBuilder sb = new StringBuilder("[");
-	return sb.append(this.start).append('-')
-	    .append(this.end)
-	    .append('(').append(this.doc).append(')')
-	    .append(']')
-	    .toString();
-    };
-
-};
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/KorapSpan.java b/src/main/java/de/ids_mannheim/korap/query/spans/KorapSpan.java
deleted file mode 100644
index 3905996..0000000
--- a/src/main/java/de/ids_mannheim/korap/query/spans/KorapSpan.java
+++ /dev/null
@@ -1,71 +0,0 @@
-package de.ids_mannheim.korap.query.spans;
-
-import java.lang.StringBuilder;
-
-public abstract class KorapSpan implements Comparable<KorapSpan>, Cloneable {
-    public int
-	start = -1,
-	end   = -1,
-	doc   = -1;
-    
-    public short elementRef = -1;
-    
-    public KorapSpan () {
-	initPayload();
-    };
-
-    public void clear () {
-	this.start = -1;
-	this.end = -1;
-	this.doc = -1;
-	clearPayload();
-    };
-
-    public KorapSpan copyFrom (KorapSpan o) {
-	this.start = o.start;
-	this.end = o.end;
-	this.doc = o.doc;
-	this.clearPayload();
-	return this;
-    };
-
-    public abstract void clearPayload ();
-    public abstract void initPayload ();
-
-    @Override
-    public int compareTo (KorapSpan o) {
-	/* optimizable for short numbers to return o.end - this.end */
-	if (this.doc < o.doc) {
-	    return -1;
-	}
-	else if (this.doc == o.doc) {
-	    if (this.start < o.start) {
-		return -1;
-	    }
-	    else if (this.start == o.start) {
-		if (this.end < o.end)
-		    return -1;
-	    };
-	};
-	return 1;
-    };
-
-    public String toString () {
-	StringBuilder sb = new StringBuilder("[");
-	return sb.append(this.start).append('-')
-	    .append(this.end)
-	    .append('(').append(this.doc).append(')')
-	    .append(']')
-	    .toString();
-    }
-
-	public short getElementRef() {
-		return elementRef;
-	}
-
-	public void setElementRef(short elementRef) {
-		this.elementRef = elementRef;
-	};
-
-    // equals und hashcode implementieren
-};
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/KorapTermSpan.java b/src/main/java/de/ids_mannheim/korap/query/spans/KorapTermSpan.java
deleted file mode 100644
index 184ee4b..0000000
--- a/src/main/java/de/ids_mannheim/korap/query/spans/KorapTermSpan.java
+++ /dev/null
@@ -1,112 +0,0 @@
-package de.ids_mannheim.korap.query.spans;
-
-import java.nio.ByteBuffer;
-import de.ids_mannheim.korap.query.spans.KorapSpan;
-
-// TODO: Store payloads in 12 byte instead of the complicated ByteBuffer stuff!
-
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class KorapTermSpan extends KorapSpan {
-
-    public ByteBuffer payload;
-    public boolean isPayloadRead = false;
-    
-    private final Logger log = LoggerFactory.getLogger(ElementSpans.class);
-    // This advices the java compiler to ignore all loggings
-    public static final boolean DEBUG = false;
-
-    @Override
-    public Object clone() {
-	KorapTermSpan span = new KorapTermSpan();
-	span.start = this.start;
-	span.end   = this.end;
-	span.doc   = this.doc;
-	span.isPayloadRead = this.isPayloadRead;
-	span.elementRef = this.elementRef;
-
-	if (this.payload != null) {
-	    this.payload.rewind();
-	    span.payload.put(this.payload);
-
-	    if (DEBUG) {
-		log.trace("[TS] Clone payload {} to payload {} ...",
-			  this.payload.toString(),
-			  span.payload.toString());
-		log.trace("[TS] ... from {}-{} to {}-{}",
-			  this.startChar(),
-			  this.endChar(),
-			  span.startChar(),
-			  span.endChar());
-	    };
-	};
-
-	return span;
-    };
-
-    public KorapSpan copyFrom (KorapTermSpan o) {
-	this.start = o.start;
-	this.end = o.end;
-	this.doc = o.doc;
-	this.payload.rewind();
-	this.payload.put(o.payload.array());
-	return this;
-    };
-
-    public KorapSpan shallowCopyFrom (KorapTermSpan o) {
-	this.start = o.start;
-	this.end = o.end;
-	this.doc = o.doc;
-	this.payload = o.payload;
-	return this;
-    };
-
-
-    @Override
-    public void clearPayload () {
-	if (this.payload != null) {
-	    this.payload.clear();
-	    // this.payload.rewind();
-	};
-    };
-
-    @Override
-    public void initPayload () {
-	this.payload = ByteBuffer.allocate(128);
-    };
-
-    @Override
-    public String toString () {
-	StringBuilder sb = new StringBuilder("[");
-	return sb.append(this.start).append('-')
-	    .append(this.end)
-	    .append("#")
-	    .append(this.startChar()).append('-').append(this.endChar())
-	    .append('(').append(this.doc).append(')')
-	    .append('$').append(this.payload.toString())
-	    .append(']')
-	    .toString();
-    };
-
-    public int startChar () {
-	return this.payload.getInt(0);
-    };
-
-    public int endChar () {
-	return this.payload.getInt(4);
-    };
-    
-    public short elementRef(){
-    	return this.payload.getShort(8);
-    }
-    
-    public void reset () {
-	this.clearPayload();
-	this.start = -1;
-	this.end = -1;
-	this.doc = -1;
-	this.isPayloadRead = false;
-    };
-};
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
index 44045ef..cfd7694 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
@@ -21,7 +21,8 @@
  * 
  * The implementation allows multiple matches at the same firstspan position.
  * 
- * @author margaretha, diewald
+ * @author margaretha
+ * @author diewald
  */
 public class NextSpans extends SimpleSpans {
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
index 219fcb5..dc68911 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
@@ -103,7 +103,7 @@
     private boolean tryMatch = true;
 
     // Two buffers for storing candidates
-    private LinkedList<KorapLongSpan>
+    private LinkedList<WithinSpan>
         spanStore1,
         spanStore2;
 
@@ -114,7 +114,8 @@
      * @param context The {@link AtomicReaderContext}.
      * @param acceptDocs Bit vector representing the documents
      *        to be searched in.
-     * @param termContexts A map managing {@link TermState}
+     * @param termContexts A map managing {@link TermState TermStates}.
+     * @param flag A byte flag indicating the positional condition of the sub spans.
      */
     public WithinSpans (SpanWithinQuery spanWithinQuery,
                         AtomicReaderContext context,
@@ -143,8 +144,8 @@
         this.flag = flag;
 
         // SpanStores for backtracking
-        this.spanStore1 = new LinkedList<KorapLongSpan>();
-        this.spanStore2 = new LinkedList<KorapLongSpan>();
+        this.spanStore1 = new LinkedList<WithinSpan>();
+        this.spanStore2 = new LinkedList<WithinSpan>();
 
         // kept for toString() only.
         this.query = spanWithinQuery;
@@ -215,7 +216,7 @@
                 if (DEBUG)
                     log.trace("In the next embedded branch");
 		
-                KorapLongSpan current = null;
+                WithinSpan current = null;
 
                 // New - fetch until theres a span in the correct doc or bigger
                 while (!this.spanStore2.isEmpty()) {
@@ -342,7 +343,7 @@
             if (!this.spanStore1.isEmpty()) {
                 if (DEBUG) {
                     log.trace("Move everything from SpanStore 1 to SpanStore 2:");
-                    for (KorapLongSpan i : this.spanStore1) {
+                    for (WithinSpan i : this.spanStore1) {
                         log.trace("     | {}", i.toString());
                     };
                 };
@@ -350,13 +351,13 @@
                 // Move everything to spanStore2
                 this.spanStore2.addAll(
                     0,
-                    (LinkedList<KorapLongSpan>) this.spanStore1.clone()
+                    (LinkedList<WithinSpan>) this.spanStore1.clone()
                 );
                 this.spanStore1.clear();
 
                 if (DEBUG) {
                     log.trace("SpanStore 2 now is:");
-                    for (KorapLongSpan i : this.spanStore2) {
+                    for (WithinSpan i : this.spanStore2) {
                         log.trace("     | {}", i.toString());
                     };
                 };
@@ -800,16 +801,16 @@
     };
     
 
-    private KorapLongSpan _currentWrap () {
-        KorapLongSpan _wrap = new KorapLongSpan();
+    private WithinSpan _currentWrap () {
+        WithinSpan _wrap = new WithinSpan();
         _wrap.start = this.wrapStart != -1 ? this.wrapStart : this.wrapSpans.start();
         _wrap.end   = this.wrapEnd   != -1 ? this.wrapEnd   : this.wrapSpans.end();
         _wrap.doc   = this.wrapDoc   != -1 ? this.wrapDoc   : this.wrapSpans.doc();
         return _wrap;
     };
 	    
-    private KorapLongSpan _currentEmbedded () {
-        KorapLongSpan _embedded = new KorapLongSpan();
+    private WithinSpan _currentEmbedded () {
+        WithinSpan _embedded = new WithinSpan();
         _embedded.start = this.embeddedStart != -1 ?
                           this.embeddedStart : this.embeddedSpans.start();
         _embedded.end   = this.embeddedEnd   != -1 ?
@@ -892,7 +893,7 @@
     private void storeEmbedded () throws IOException {
 
         // Create a current copy
-        KorapLongSpan embedded = new KorapLongSpan();
+        WithinSpan embedded = new WithinSpan();
         embedded.start = this.embeddedStart != -1 ?
                          this.embeddedStart : this.embeddedSpans.start();
         embedded.end   = this.embeddedEnd   != -1 ?
@@ -1102,4 +1103,84 @@
 	return getClass().getName() + "("+query.toString()+")@"+
 	    (embeddedDoc <= 0?"START":(more?(doc()+":"+start()+"-"+end()):"END"));
     };
+
+
+    // This was formerly the default candidate span class,
+    // before it was refactored out
+    private class WithinSpan implements Comparable<WithinSpan>, Cloneable  {
+        public int
+            start = -1,
+            end   = -1,
+            doc   = -1;
+
+        public Collection<byte[]> payload;
+
+        public short elementRef = -1;
+    
+        public void clear () {
+            this.start = -1;
+            this.end = -1;
+            this.doc = -1;
+            clearPayload();
+        };
+    
+        @Override
+        public int compareTo (WithinSpan o) {
+            /* optimizable for short numbers to return o.end - this.end */
+            if (this.doc < o.doc) {
+                return -1;
+            }
+            else if (this.doc == o.doc) {
+                if (this.start < o.start) {
+                    return -1;
+                }
+                else if (this.start == o.start) {
+                    if (this.end < o.end)
+                        return -1;
+                };
+            };
+            return 1;
+        };
+
+        public short getElementRef() {
+            return elementRef;
+        }
+
+        public void setElementRef(short elementRef) {
+            this.elementRef = elementRef;
+        };
+    
+        @Override
+        public Object clone() {
+            WithinSpan span = new WithinSpan();
+            span.start = this.start;
+            span.end = this.end;
+            span.doc = this.doc;
+            span.payload.addAll(this.payload);
+            return span;
+        };
+
+        public WithinSpan copyFrom (WithinSpan o) {
+            this.start = o.start;
+            this.end = o.end;
+            this.doc = o.doc;
+            // this.clearPayload();
+            this.payload.addAll(o.payload);
+            return this;
+        };
+        
+        public void clearPayload () {
+            if (this.payload != null)
+                this.payload.clear();
+        };
+
+        public String toString () {
+            StringBuilder sb = new StringBuilder("[");
+            return sb.append(this.start).append('-')
+                .append(this.end)
+                .append('(').append(this.doc).append(')')
+                .append(']')
+                .toString();
+        };
+    };
 };