package org.oblodiff.subject.text;

import org.oblodiff.api.Subject;

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

/**
 * This is a more high level subject that is based on a string which can be split at several positions, dividing this
 * subject into sub-subjects and the sub-subject dividers.
 *
 * @author Christian Rösch &lt;christianroesch@gmx.net&gt;
 */
public abstract class TextualSplittableSubject extends TextualSubject<String> {

    public TextualSplittableSubject(String s) {
        super(s);
    }

    @Override
    public final List<Subject> getChildren() {
        List<Subject> children = new ArrayList<>();

        int begin = 0;
        for (int i = 0; i < getContent().length(); ++i) {
            Character character = new Character(getContent().charAt(i));
            if (shouldSplitAt(i, character)) {
                addSubject(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 children;
    }

    protected final void addSubject(List<Subject> children, int begin, int end) {
        if (end > begin) {
            children.add(newSubject(getContent().substring(begin, end)));
        }
    }

    protected abstract boolean shouldSplitAt(int i, Character character);

    protected abstract Subject newSubject(String content);

    protected abstract void endReached(List<Subject> children, int begin, Character character);

    protected abstract int addDivider(List<Subject> children, int begin, Character character);
}

@@ -613,16 +613,22 @@
         
+final 
 List%3CSub
@@ -663,17 +663,16 @@
 st%3C%3E();%0A
-%0A
         
@@ -686,16 +686,17 @@
 in = 0;%0A
+%0A
         
@@ -757,16 +757,22 @@
         
+final 
 Characte
@@ -789,22 +789,8 @@
 r = 
-new Character(
 getC
@@ -807,18 +807,18 @@
 harAt(i)
-)
 ;
+%0A
 %0A       

Remove unnecessary boxing.