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);
}

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.
 *
 * @author Christian Rösch &lt;christianroesch@gmx.net&gt;
 * @since 1.0.0
 */
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);
    }

    /**
     * Adds a token generated by {@link #newToken(String)} to the given children.
     *
     * @param children the collection to enrich by a token
     * @param begin    the offset where this token begins (in the content)
     * @param end      the offset where this token ends (in the content). Must be greater than or equal to begin.
     */
    protected final void addToken(final Collection<Token> children, final int begin, final int end) {
        if (end > begin) {
            children.add(newToken(getContent().substring(begin, end)));
        }
    }

    /**
     * @param i         the offset where to split at
     * @param character the character at the given offset
     * @return {@code true} if the current position is the end of a token or if there is another reason to split the
     * children here.
     */
    protected abstract boolean shouldSplitAt(int i, Character character);

    /**
     * Creates a child token.
     *
     * @param content the textual content of the token. Not {@code null}.
     * @return a child token.
     */
    protected abstract Token newToken(String content);

    /**
     * Adds the last token to the children.
     *
     * @param children  the children to enrich
     * @param begin     the begin of the current token
     * @param character the character at the current position.
     */
    protected abstract void endReached(Collection<Token> children, int begin, Character character);

    /**
     * @param children  the current children
     * @param begin     the current position (aka begin of the divider)
     * @param character the character at the current position
     * @return the size (number of characters) of the added divider
     */
    protected abstract int addDivider(Collection<Token> children, int begin, Character character);
}

Add JavaDoc.