Created
July 4, 2014 05:48
-
-
Save bobbyjam99-zz/6ddfdc10756c15137552 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.io.BufferedReader; | |
import java.io.Reader; | |
import java.util.Arrays; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import java.util.stream.Stream; | |
public class GrepUtil { | |
public static Stream<String> grep(Reader reader, String... words) { | |
AtomicInteger index = new AtomicInteger(); | |
return new BufferedReader(reader).lines() | |
.map(s -> new GrepFile(index.incrementAndGet(), s)) | |
.filter(o -> Arrays.asList(words).stream().anyMatch(s -> o.getWord().contains(s))) | |
.map(o -> o.toString()); | |
} | |
static class GrepFile { | |
private int index; | |
private String word; | |
public GrepFile(int index, String word) { | |
this.index = index; | |
this.word = word; | |
} | |
public String getWord() { | |
return word; | |
} | |
public String toString() { | |
return index + ": " + word; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment