@@ -72,11 +72,14 @@ public Set<Characteristics> characteristics() {
7272
7373 // visible for testing
7474 static abstract class Acc <L , C , R > {
75- private List <R > right ;
75+ private ArrayList <R > right ;
7676
77- final List <R > right () {
78- return right == null ? List .of () : right ;
79- }
77+ abstract void combineLeft (C otherLeft );
78+
79+ abstract boolean isLeft ();
80+
81+ /** Returns {@code null} iff {@link #isLeft()} */
82+ abstract C left ();
8083
8184 final void addRight (R value ) {
8285 if (isLeft ()) {
@@ -100,23 +103,17 @@ final Acc<L, C, R> combine(Acc<L, C, R> other) {
100103 return this ;
101104 }
102105 if (right == null ) {
103- right = new ArrayList <>();
106+ right = other .right ;
107+ } else {
108+ right .addAll (other .right );
104109 }
105- right .addAll (other .right );
106110 return this ;
107111 }
108112
109- abstract void combineLeft (C otherLeft );
110-
111- abstract boolean isLeft ();
112-
113- abstract C left ();
114-
115- Either <C , List <R >> finish () {
116- if (isLeft ()) {
117- return Either .left (left ());
118- }
119- return Either .right (right ());
113+ final Either <C , List <R >> finish () {
114+ return isLeft ()
115+ ? Either .left (left ())
116+ : Either .right (right == null ? List .of () : right );
120117 }
121118 }
122119
@@ -149,7 +146,9 @@ private static class FullAcc<L, R> extends Acc<L, List<L>, R> {
149146
150147 @ Override
151148 void combineLeft (List <L > otherLeft ) {
152- if (otherLeft != null && !otherLeft .isEmpty ()) {
149+ if (left == null ) {
150+ left = otherLeft ;
151+ } else if (otherLeft != null && !otherLeft .isEmpty ()) {
153152 left .addAll (otherLeft );
154153 }
155154 }
0 commit comments