20
20
import java .io .Writer ;
21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
+ import java .util .Objects ;
23
24
import java .util .function .Consumer ;
24
25
import java .util .function .Function ;
25
26
import java .util .logging .Level ;
@@ -34,6 +35,7 @@ public class UnifiedDiffWriter {
34
35
private static final Logger LOG = Logger .getLogger (UnifiedDiffWriter .class .getName ());
35
36
36
37
public static void write (UnifiedDiff diff , Function <String , List <String >> originalLinesProvider , Writer writer , int contextSize ) throws IOException {
38
+ Objects .requireNonNull (originalLinesProvider , "original lines provider needs to be specified" );
37
39
write (diff , originalLinesProvider , line -> {
38
40
try {
39
41
writer .append (line ).append ("\n " );
@@ -138,7 +140,8 @@ private static void processDeltas(Consumer<String> writer,
138
140
}
139
141
140
142
// output the context before the first Delta
141
- for (line = contextStart ; line < curDelta .getSource ().getPosition (); line ++) { //
143
+ for (line = contextStart ; line < curDelta .getSource ().getPosition ()
144
+ && line < origLines .size (); line ++) { //
142
145
buffer .add (" " + origLines .get (line ));
143
146
origTotal ++;
144
147
revTotal ++;
@@ -153,8 +156,8 @@ private static void processDeltas(Consumer<String> writer,
153
156
AbstractDelta <String > nextDelta = deltas .get (deltaIndex );
154
157
int intermediateStart = curDelta .getSource ().getPosition ()
155
158
+ curDelta .getSource ().getLines ().size ();
156
- for (line = intermediateStart ; line < nextDelta .getSource ()
157
- . getPosition (); line ++) {
159
+ for (line = intermediateStart ; line < nextDelta .getSource (). getPosition ()
160
+ && line < origLines . size (); line ++) {
158
161
// output the code between the last Delta and this one
159
162
buffer .add (" " + origLines .get (line ));
160
163
origTotal ++;
0 commit comments