Revert "Simplify WordReader"

This reverts commit c029880e071b4c90a00f646202bf3ef9a923958a.
This commit is contained in:
Charles Gould 2017-10-16 21:59:34 -04:00
parent 07ff8bb03e
commit e1dea14aa1
2 changed files with 27 additions and 9 deletions

View File

@ -1,21 +1,38 @@
package lingo.common;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WordReader {
public static List<String> readFileToList(String filename) throws Exception {
final URI resource = WordReader.class.getResource(filename).toURI();
return Files.readAllLines(Paths.get(resource));
private static void readFileToCollection(String filename, Collection<String> c) throws IOException {
try (final InputStream stream = WordReader.class.getResourceAsStream(filename);
final InputStreamReader streamReader = new InputStreamReader(stream);
final BufferedReader bufferedReader = new BufferedReader(streamReader)) {
String line = null;
while ((line = bufferedReader.readLine()) != null) {
c.add(line);
}
}
}
public static Set<String> readFileToSet(String filename) throws Exception {
return new HashSet<>(readFileToList(filename));
public static List<String> readFileToList(String filename) throws IOException {
final List<String> list = new ArrayList<>();
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;
}
}

View File

@ -1,5 +1,6 @@
package lingo.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -16,7 +17,7 @@ public class WordRepository {
private final List<String> words;
public WordRepository() throws Exception {
public WordRepository() throws IOException {
guesses = WordReader.readFileToSet("/guesses.txt");
words = WordReader.readFileToList("/words.txt");
}