package org.oblodiff.token.text;

import org.oblodiff.token.api.Token;

import java.lang.Character;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * This is a more high level token that is based on a string which can be split at several positions, dividing this
 * token into child tokens and the child token dividers.
 *
 * @since 1.0.0
 * @author Christian Rösch &lt;christianroesch@gmx.net&gt;
 */
public abstract class TextualSplittableToken extends TextualToken<String> {

    /**
     * Dedicated constructor.
     *
     * @param content must not be {@code null}
     */
    public TextualSplittableToken(final String content) {
        super(content);
    }

    @Override
    public final List<Token> getChildren() {
        final List<Token> children = new ArrayList<>();
        int begin = 0;

        for (int i = 0; i < getContent().length(); ++i) {
            final Character character = getContent().charAt(i);

            if (shouldSplitAt(i, character)) {
                addToken(children, begin, i);
                int dividerSize = addDivider(children, i, character);
                begin = i + dividerSize;
                i += dividerSize - 1;
            } else if (i + 1 == getContent().length()) {
                endReached(children, begin, character);
            }
        }

        return Collections.unmodifiableList(children);
    }

    // TODO Add javadoc.
    protected final void addToken(final Collection<Token> children, final int begin, final int end) {
        if (end > begin) {
            children.add(newToken(getContent().substring(begin, end)));
        }
    }

    // TODO Add javadoc.
    protected abstract boolean shouldSplitAt(int i, Character character);
    // TODO Add javadoc.
    protected abstract Token newToken(String content);
    // TODO Add javadoc.
    protected abstract void endReached(Collection<Token> children, int begin, Character character);
    // TODO Add javadoc.
    protected abstract int addDivider(Collection<Token> children, int begin, Character character);
}

@@ -388,24 +388,8 @@
 %0A *%0A
- * @since 1.0.0%0A
  * @
@@ -443,16 +443,32 @@
 net&gt;%0A
+ * @since 1.0.0%0A
  */%0Apubl
@@ -1442,270 +1442,839 @@
    /
-/ TODO Add javadoc.%0A    protected final void addToken(final Collection%3CToken%3E children, final int begin, final int end) %7B%0A        if (end %3E begin) %7B%0A            children.add(newToken(getContent().substring(begin, end)));%0A        %7D%0A    %7D%0A%0A    // TODO Add javadoc.
+**%0A     * Adds a token generated by %7B@link #newToken(String)%7D to the given children.%0A     *%0A     * @param children the collection to enrich by a token%0A     * @param begin    the offset where this token begins (in the content)%0A     * @param end      the offset where this token ends (in the content). Must be greater than or equal to begin.%0A     */%0A    protected final void addToken(final Collection%3CToken%3E children, final int begin, final int end) %7B%0A        if (end %3E begin) %7B%0A            children.add(newToken(getContent().substring(begin, end)));%0A        %7D%0A    %7D%0A%0A    /**%0A     * @param i         the offset where to split at%0A     * @param character the character at the given offset%0A     * @return %7B@code true%7D if the current position is the end of a token or if there is another reason to split the%0A     * children here.%0A     */
 %0A   
@@ -2348,32 +2348,165 @@
 r);%0A
+%0A
     /
-/ TODO Add javadoc.
+**%0A     * Creates a child token.%0A     *%0A     * @param content the textual content of the token. Not %7B@code null%7D.%0A     * @return a child token.%0A     */
 %0A   
@@ -2561,32 +2561,240 @@
 t);%0A
+%0A
     /
-/ TODO Add javadoc.
+**%0A     * Adds the last token to the children.%0A     *%0A     * @param children  the children to enrich%0A     * @param begin     the begin of the current token%0A     * @param character the character at the current position.%0A     */
 %0A   
@@ -2894,32 +2894,271 @@
 r);%0A
+%0A
     /
-/ TODO Add javadoc.
+**%0A     * @param children  the current children%0A     * @param begin     the current position (aka begin of the divider)%0A     * @param character the character at the current position%0A     * @return the size (number of characters) of the added divider%0A     */
 %0A   

Add JavaDoc.