Skip to content
Permalink
Browse files

Step 7: Filtering by Issue Type

  • Loading branch information...
tlinkowski committed Jan 31, 2019
1 parent 274dedb commit 1b1329ba998d226166f98d49fcf9f789ad5a961e
Showing with 15 additions and 4 deletions.
  1. +10 −3 IssueCoverage.java
  2. +5 −1 Usage.java
@@ -1,19 +1,26 @@
import java.util.function.Predicate;
import java.util.stream.Collector;

class IssueCoverage {
static Collector<IssueWiseText<?>, ?, Double> collector() {
static Collector<IssueWiseText<?>, ?, Double> collector(Predicate<? super Issue> issueFilter) {
return Collector.of(
Accumulator::new, Accumulator::accumulate, Accumulator::combine, Accumulator::finish,
() -> new Accumulator(issueFilter), Accumulator::accumulate, Accumulator::combine, Accumulator::finish,
Collector.Characteristics.UNORDERED
);
}

private static class Accumulator {
private final Predicate<? super Issue> issueFilter;

int totalIssueLength = 0;
int totalTextLength = 0;

Accumulator(Predicate<? super Issue> issueFilter) {
this.issueFilter = issueFilter;
}

void accumulate(IssueWiseText<?> issueWiseText) {
totalIssueLength += issueWiseText.issues().stream().mapToInt(Issue::length).sum();
totalIssueLength += issueWiseText.issues().stream().filter(issueFilter).mapToInt(Issue::length).sum();
totalTextLength += issueWiseText.text().length();
}

@@ -2,7 +2,11 @@

class Usage {
static double issueCoverage(Stream<? extends IssueWiseText<?>> textStream) {
return textStream.collect(IssueCoverage.collector());
return textStream.collect(IssueCoverage.collector(issue -> true));
}

static double issueCoverage(Stream<? extends IssueWiseText<?>> textStream, IssueType issueType) {
return textStream.collect(IssueCoverage.collector(issue -> issue.type() == issueType));
}

static Stream<IssueWiseText<Issue>> testCaseStream() {

0 comments on commit 1b1329b

Please sign in to comment.
You can’t perform that action at this time.