Skip to content

Commit

Permalink
Use stream in matcher (#3696)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez authored and Siedlerchr committed Feb 6, 2018
1 parent 24ed12a commit bd3ef32
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
19 changes: 4 additions & 15 deletions src/main/java/org/jabref/model/search/matchers/AndMatcher.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package org.jabref.model.search.matchers;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.search.SearchMatcher;

/**
* Subclass of MatcherSet that ANDs or ORs between its rules, returning 0 or
* 1.
* A set of matchers that returns true if all matcher match the given entry.
*/
public class AndMatcher extends MatcherSet {

@Override
public boolean isMatch(BibEntry bibEntry) {
int score = 0;

// We let each rule add a maximum of 1 to the score.
for (SearchMatcher rule : matchers) {
if (rule.isMatch(bibEntry)) {
score++;
}
}

// Then an AND rule demands that score == number of rules
return score == matchers.size();
public boolean isMatch(BibEntry entry) {
return matchers.stream()
.allMatch(rule -> rule.isMatch(entry));
}
}
19 changes: 4 additions & 15 deletions src/main/java/org/jabref/model/search/matchers/OrMatcher.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package org.jabref.model.search.matchers;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.search.SearchMatcher;

/**
* Subclass of MatcherSet that ANDs or ORs between its rules, returning 0 or
* 1.
* A set of matchers that returns true if any matcher matches the given entry.
*/
public class OrMatcher extends MatcherSet {

@Override
public boolean isMatch(BibEntry bibEntry) {
int score = 0;

// We let each rule add a maximum of 1 to the score.
for (SearchMatcher rule : matchers) {
if (rule.isMatch(bibEntry)) {
score++;
}
}

// OR rule demands score > 0.
return score > 0;
public boolean isMatch(BibEntry entry) {
return matchers.stream()
.anyMatch(rule -> rule.isMatch(entry));
}
}

0 comments on commit bd3ef32

Please sign in to comment.