Skip to content

Commit

Permalink
Introduce Loader#collect(Collector<? super T, A, R> collector) (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
hisener authored and Stephan202 committed Jan 3, 2020
1 parent 31546cf commit f9fd5e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/main/java/tech/picnic/jolo/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jooq.Record;
Expand Down Expand Up @@ -65,9 +66,14 @@ public Stream<T> stream() {
return get().stream();
}

/** Collects and then returns all objects loaded by this loader. */
public <R, A> R collect(Collector<? super T, A, R> collector) {
return stream().collect(collector);
}

/** Returns all objects loaded by this loader. */
public List<T> getList() {
return stream().collect(Collectors.toList());
return collect(Collectors.toList());
}

/**
Expand All @@ -76,7 +82,7 @@ public List<T> getList() {
* correct result.
*/
public Set<T> getSet() {
return stream().collect(Collectors.toSet());
return collect(Collectors.toSet());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/tech/picnic/jolo/LoaderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.jolo;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static tech.picnic.jolo.TestUtil.createRecord;
Expand Down Expand Up @@ -245,7 +246,7 @@ public void testZeroOrOneToManyRecursiveReference() {
expectedBar1.setOtherBar(Optional.of(expectedBar2));
expectedBar2.setOtherBar(Optional.of(expectedBar2));

assertEquals(ImmutableList.of(expectedBar1, expectedBar2), l.getList());
assertEquals(ImmutableList.of(expectedBar1, expectedBar2), l.collect(toImmutableList()));
}

@Test
Expand Down

0 comments on commit f9fd5e1

Please sign in to comment.