Skip to content

Commit c11355e

Browse files
committed
optimize Acc#combine
1 parent 4735ebd commit c11355e

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

src/main/java/io/jbock/util/Eithers.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/io/jbock/util/Right.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.jbock.util;
22

3-
import java.util.Objects;
43
import java.util.Optional;
54
import java.util.function.Consumer;
65
import java.util.function.Function;
@@ -114,6 +113,6 @@ public boolean equals(Object obj) {
114113

115114
@Override
116115
public int hashCode() {
117-
return Objects.hash(value);
116+
return value.hashCode();
118117
}
119118
}

0 commit comments

Comments
 (0)