Skip to content

Commit

Permalink
Add MoreGatherers.zipWithIterable() (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Oct 10, 2024
1 parent 0955bec commit 8b87a87
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ Java's Stream API is a powerful tool for processing collections of data. However
Whenever possible, the library follows Project Reactor's naming conventions.

Provided `Gatherers`:
- `MoreGatherers.sample(int)`
- `MoreGatherers.last(int)`
- `MoreGatherers.zip(Collection<T2>)`
- `MoreGatherers.sample(int)`
- `MoreGatherers.zip(Iterator<T2>)`
- `MoreGatherers.zip(Stream<T2>)`
- `MoreGatherers.zip(Collection<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zip(Iterator<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zip(Stream<T2>)`
- `MoreGatherers.zip(Stream<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zipWithIterable(Iterable<T2>)`
- `MoreGatherers.zipWithIterable(Iterable<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.distinctBy(Function<T, R>)`
- `MoreGatherers.distinctUntilChanged()`
- `MoreGatherers.distinctUntilChanged(Function<T, R>)`
- `MoreGatherers.distinctBy(Function<T, R>)`
6 changes: 2 additions & 4 deletions src/main/java/com/pivovarit/gatherers/MoreGatherers.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.pivovarit.gatherers;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -106,12 +104,12 @@ class State {
return zip(other.iterator(), mapper);
}

public static <T1, T2> Gatherer<T1, ?, Map.Entry<T1, T2>> zip(Collection<T2> other) {
public static <T1, T2> Gatherer<T1, ?, Map.Entry<T1, T2>> zipWithIterable(Iterable<T2> other) {
Objects.requireNonNull(other, "other can't be null");
return zip(other.iterator());
}

public static <T1, T2, R> Gatherer<T1, ?, R> zip(Collection<T2> other, BiFunction<? super T1, ? super T2, ? extends R> mapper) {
public static <T1, T2, R> Gatherer<T1, ?, R> zipWithIterable(Iterable<T2> other, BiFunction<? super T1, ? super T2, ? extends R> mapper) {
Objects.requireNonNull(other, "other can't be null");
Objects.requireNonNull(mapper, "mapper can't be null");
return zip(other.iterator(), mapper);
Expand Down
40 changes: 0 additions & 40 deletions src/test/java/com/pivovarit/gatherers/ZipCollectionMapperTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.pivovarit.gatherers;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Stream;

import static com.pivovarit.gatherers.MoreGatherers.zipWithIterable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ZipWithIterableMapperTest {

@Test
void shouldZipEmpty() {
assertThat(Stream.<Integer>empty().gather(zipWithIterable(List.of(1, 2, 3), Integer::sum))).isEmpty();
}

@Test
void shouldZipWithEmpty() {
assertThat(Stream.of(1, 2, 3).gather(zipWithIterable(List.of(), Integer::sum))).isEmpty();
}

@Test
void shouldZip() {
assertThat(Stream.of(1, 2, 3).gather(zipWithIterable(List.of("a", "b", "c", "d"), (i, s) -> i + s)))
.containsExactly("1a", "2b", "3c");
}

@Test
void shouldRejectNullCollection() {
assertThatThrownBy(() -> zipWithIterable(null, (i, _) -> i)).isInstanceOf(NullPointerException.class);
}

@Test
void shouldRejectNullMapper() {
assertThatThrownBy(() -> zipWithIterable(List.of(1), null)).isInstanceOf(NullPointerException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import static com.pivovarit.gatherers.MoreGatherers.zip;
import static com.pivovarit.gatherers.MoreGatherers.zipWithIterable;
import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ZipCollectionTest {
class ZipWithIterableTest {

@Test
void shouldZipEmpty() {
assertThat(Stream.empty().gather(zip(List.of(1, 2, 3)))).isEmpty();
assertThat(Stream.empty().gather(zipWithIterable(List.of(1, 2, 3)))).isEmpty();
}

@Test
void shouldZipWithEmpty() {
assertThat(Stream.of(1, 2, 3).gather(zip(List.of()))).isEmpty();
assertThat(Stream.of(1, 2, 3).gather(zipWithIterable(List.of()))).isEmpty();
}

@Test
void shouldZip() {
assertThat(Stream.of(1, 2, 3).gather(zip(List.of("a", "b", "c", "d"))))
assertThat(Stream.of(1, 2, 3).gather(zipWithIterable(List.of("a", "b", "c", "d"))))
.containsExactly(
entry(1, "a"),
entry(2, "b"),
Expand All @@ -35,6 +34,6 @@ void shouldZip() {

@Test
void shouldRejectNullCollection() {
assertThatThrownBy(() -> zip((Collection<Object>) null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> zipWithIterable(null)).isInstanceOf(NullPointerException.class);
}
}

0 comments on commit 8b87a87

Please sign in to comment.