Simplify WordReader
This commit is contained in:
parent
a548002598
commit
7f4ee4da65
@ -1,38 +1,21 @@
|
|||||||
package lingo.common;
|
package lingo.common;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.net.URI;
|
||||||
import java.io.IOException;
|
import java.nio.file.Files;
|
||||||
import java.io.InputStream;
|
import java.nio.file.Paths;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class WordReader {
|
public class WordReader {
|
||||||
|
|
||||||
private static void readFileToCollection(String filename, Collection<String> c) throws IOException {
|
public static List<String> readFileToList(String filename) throws Exception {
|
||||||
try (final InputStream stream = WordReader.class.getResourceAsStream(filename);
|
final URI resource = WordReader.class.getResource(filename).toURI();
|
||||||
final InputStreamReader streamReader = new InputStreamReader(stream);
|
return Files.readAllLines(Paths.get(resource));
|
||||||
final BufferedReader bufferedReader = new BufferedReader(streamReader)) {
|
|
||||||
String line = null;
|
|
||||||
while ((line = bufferedReader.readLine()) != null) {
|
|
||||||
c.add(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> readFileToList(String filename) throws IOException {
|
public static Set<String> readFileToSet(String filename) throws Exception {
|
||||||
final List<String> list = new ArrayList<>();
|
return new HashSet<>(readFileToList(filename));
|
||||||
readFileToCollection(filename, list);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Set<String> readFileToSet(String filename) throws IOException {
|
|
||||||
final Set<String> list = new HashSet<>();
|
|
||||||
readFileToCollection(filename, list);
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package lingo.server;
|
package lingo.server;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -17,7 +16,7 @@ public class WordRepository {
|
|||||||
|
|
||||||
private final List<String> words;
|
private final List<String> words;
|
||||||
|
|
||||||
public WordRepository() throws IOException {
|
public WordRepository() throws Exception {
|
||||||
guesses = WordReader.readFileToSet("/guesses.txt");
|
guesses = WordReader.readFileToSet("/guesses.txt");
|
||||||
words = WordReader.readFileToList("/words.txt");
|
words = WordReader.readFileToList("/words.txt");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user