Skip to content

Commit

Permalink
Finisher can't be null anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Mar 22, 2019
1 parent 8b50795 commit aa12b5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
49 changes: 21 additions & 28 deletions stream/src/main/java/com/annimon/stream/Collectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -875,13 +875,12 @@ public void accept(U u) {
*/
@NotNull
public static <T, A, IR, OR> Collector<T, A, OR> collectingAndThen(
@NotNull Collector<T, A, IR> c, Function<IR, OR> finisher) {
Function<A, IR> downstreamFinisher = c.finisher();
if (downstreamFinisher == null) {
downstreamFinisher = castIdentity();
}
@NotNull Collector<T, A, IR> c,
@NotNull Function<IR, OR> finisher) {
Objects.requireNonNull(c);
Objects.requireNonNull(finisher);
return new CollectorsImpl<T, A, OR>(c.supplier(), c.accumulator(),
Function.Util.andThen(downstreamFinisher, finisher));
Function.Util.andThen(c.finisher(), finisher));
}

/**
Expand Down Expand Up @@ -944,24 +943,21 @@ public static <T, A, IR, OR> Collector<T, A, OR> collectingAndThen(

@SuppressWarnings("unchecked")
final Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
Function<Map<K, A>, M> finisher = null;
if (downstreamFinisher != null) {
finisher = new Function<Map<K, A>, M>() {
@NotNull
@Override
public M apply(@NotNull Map<K, A> map) {
// Update values of a map by a finisher function
for (Map.Entry<K, A> entry : map.entrySet()) {
A value = entry.getValue();
value = downstreamFinisher.apply(value);
entry.setValue(value);
}
@SuppressWarnings("unchecked")
M castedMap = (M) map;
return castedMap;
Function<Map<K, A>, M> finisher = new Function<Map<K, A>, M>() {
@NotNull
@Override
public M apply(@NotNull Map<K, A> map) {
// Update values of a map by a finisher function
for (Map.Entry<K, A> entry : map.entrySet()) {
A value = entry.getValue();
value = downstreamFinisher.apply(value);
entry.setValue(value);
}
};
}
@SuppressWarnings("unchecked")
M castedMap = (M) map;
return castedMap;
}
};

@SuppressWarnings("unchecked")
Supplier<Map<K, A>> castedMapFactory = (Supplier<Map<K, A>>) mapFactory;
Expand Down Expand Up @@ -1042,10 +1038,7 @@ public void accept(@NotNull Tuple2<A> container, T t) {
@NotNull
@Override
public Map<Boolean, D> apply(@NotNull Tuple2<A> container) {
final Function<A, D> downstreamFinisher = downstream.finisher();
final Function<A, D> finisher = downstreamFinisher == null
? Collectors.<A, D>castIdentity()
: downstreamFinisher;
final Function<A, D> finisher = downstream.finisher();
Map<Boolean, D> result = new HashMap<Boolean, D>(2);
result.put(Boolean.TRUE, finisher.apply(container.a));
result.put(Boolean.FALSE, finisher.apply(container.b));
Expand Down Expand Up @@ -1218,7 +1211,7 @@ private static final class CollectorsImpl<T, A, R> implements Collector<T, A, R>
private final Function<A, R> finisher;

public CollectorsImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator) {
this(supplier, accumulator, null);
this(supplier, accumulator, Collectors.<A, R>castIdentity());
}

public CollectorsImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, Function<A, R> finisher) {
Expand Down
4 changes: 1 addition & 3 deletions stream/src/main/java/com/annimon/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1825,9 +1825,7 @@ public <R, A> R collect(@NotNull Collector<? super T, A, R> collector) {
final T value = iterator.next();
collector.accumulator().accept(container, value);
}
if (collector.finisher() != null)
return collector.finisher().apply(container);
return Collectors.<A, R>castIdentity().apply(container);
return collector.finisher().apply(container);
}

/**
Expand Down

0 comments on commit aa12b5d

Please sign in to comment.