Created
January 25, 2017 15:50
-
-
Save mgenov/c708882412a2fdaea3c3a33a2c1a3c37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Question { | |
private String name; | |
private String status; | |
private String module; | |
public Question(String name, String status, String module) { | |
this.name = name; | |
this.status = status; | |
this.module = module; | |
} | |
public Set<String> searchIndex() { | |
return Sets.newHashSet("name:" + name, "status:" + status, "module:" + module); | |
} | |
@Override | |
public String toString() { | |
return MoreObjects.toStringHelper(this) | |
.add("name", name) | |
.add("status", status) | |
.add("module", module) | |
.toString(); | |
} | |
} | |
@Test | |
public void happyPath() throws Exception { | |
Set<String> filter = Sets.newHashSet("name:none", "status:none","module:A2"); | |
List<Question> questions = Lists.newArrayList( | |
new Question("Q1", "asad", "A1"), | |
new Question("Q1", "something", "A2"), | |
new Question("Q1", "else", "A3"), | |
new Question("Q2", "good", "A2"), | |
new Question("Q2", "bad", "A3") | |
); | |
List<Question> filteredQuestions = questions | |
.stream() | |
.filter(it -> shouldBeFilteredBy(filter, it)) | |
.collect(Collectors.toList()); | |
for (Question each : filteredQuestions) { | |
System.out.println(each); | |
} | |
} | |
private boolean shouldBeFilteredBy(Set<String> filter, Question it) { | |
Set<String> index = it.searchIndex(); | |
for (String each : filter) { | |
if (each.contains("none")) { | |
continue; | |
} | |
if (!index.contains(each)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment