private LinkedList<Change> compute(String text1, String text2, boolean checkLines) {
        LinkedList<Change> diffs = new LinkedList<>();

        if (text1.length() == 0) {
            // Just add some text (speedup).
            diffs.add(new Change(Operation.INSERT, text2));
            return diffs;
        }

        if (text2.length() == 0) {
            // Just delete some text (speedup).
            diffs.add(new Change(Operation.DELETE, text1));
            return diffs;
        }

        String longtext = text1.length() > text2.length() ? text1 : text2;
        String shorttext = text1.length() > text2.length() ? text2 : text1;
        int i = longtext.indexOf(shorttext);
        if (i != -1) {
            // Shorter text is inside the longer text (speedup).
            Operation op = (text1.length() > text2.length()) ?
                           Operation.DELETE : Operation.INSERT;
            diffs.add(new Change(op, longtext.substring(0, i)));
            diffs.add(new Change(Operation.EQUAL, shorttext));
            diffs.add(new Change(op, longtext.substring(i + shorttext.length())));
            return diffs;
        }

        if (shorttext.length() == 1) {
            // Single character string.
            // After the previous speedup, the character can't be an equality.
            diffs.add(new Change(Operation.DELETE, text1));
            diffs.add(new Change(Operation.INSERT, text2));
            return diffs;
        }

        // Check to see if the problem can be split in two.
        String[] hm = halfMatch(text1, text2);
        if (hm != null) {
            // A half-match was found, sort out the return data.
            String text1_a = hm[0];
            String text1_b = hm[1];
            String text2_a = hm[2];
            String text2_b = hm[3];
            String mid_common = hm[4];
            // Send both pairs off for separate processing.
            LinkedList<Change> diffs_a = main(text1_a, text2_a, checkLines);
            LinkedList<Change> diffs_b = main(text1_b, text2_b, checkLines);
            // Merge the results.
            diffs = diffs_a;
            diffs.add(new Change(Operation.EQUAL, mid_common));
            diffs.addAll(diffs_b);
            return diffs;
        }

        if (checkLines && text1.length() > 100 && text2.length() > 100) {
            return lineMode(text1, text2);
        }

        return bisect(text1, text2);
    }
private LinkedList < Change > compute ( String text1 , String text2 , boolean checkLines ) { LinkedList < Change > diffs = new LinkedList < > ( ) ; if ( text1 . length ( ) == 0 ) { diffs . add ( new Change ( Operation . INSERT , text2 ) ) ; return diffs ; } if ( text2 . length ( ) == 0 ) { diffs . add ( new Change ( Operation . DELETE , text1 ) ) ; return diffs ; } String longtext = text1 . length ( ) > text2 . length ( ) ? text1 : text2 ; String shorttext = text1 . length ( ) > text2 . length ( ) ? text2 : text1 ; int i = longtext . indexOf ( shorttext ) ; if ( i != - 1 ) { Operation op = ( text1 . length ( ) > text2 . length ( ) ) ? Operation . DELETE : Operation . INSERT ; diffs . add ( new Change ( op , longtext . substring ( 0 , i ) ) ) ; diffs . add ( new Change ( Operation . EQUAL , shorttext ) ) ; diffs . add ( new Change ( op , longtext . substring ( i + shorttext . length ( ) ) ) ) ; return diffs ; } if ( shorttext . length ( ) == 1 ) { diffs . add ( new Change ( Operation . DELETE , text1 ) ) ; diffs . add ( new Change ( Operation . INSERT , text2 ) ) ; return diffs ; } String [ ] hm = halfMatch ( text1 , text2 ) ; if ( hm != null ) { String text1_a = hm [ 0 ] ; String text1_b = hm [ 1 ] ; String text2_a = hm [ 2 ] ; String text2_b = hm [ 3 ] ; String mid_common = hm [ 4 ] ; LinkedList < Change > diffs_a = main ( text1_a , text2_a , checkLines ) ; LinkedList < Change > diffs_b = main ( text1_b , text2_b , checkLines ) ; diffs = diffs_a ; diffs . add ( new Change ( Operation . EQUAL , mid_common ) ) ; diffs . addAll ( diffs_b ) ; return diffs ; } if ( checkLines && text1 . length ( ) > 100 && text2 . length ( ) > 100 ) { return lineMode ( text1 , text2 ) ; } return bisect ( text1 , text2 ) ; }
Find the differences between two texts.  Assumes that the texts do not
have any common prefix or suffix.
@param text1 Old string to be diffed.
@param text2 New string to be diffed.
@return Linked List of DiffBase objects.