things: Refactor DataStorageManager to be more idiomatic.
This commit is contained in:
@@ -32,7 +32,7 @@ class WordFrequencyController extends TFExercise {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
for (String word : this.storageManager.calculateWords()) {
|
for (String word : this.storageManager.getWords()) {
|
||||||
if (!this.stopWordManager.isStopWord(word)) {
|
if (!this.stopWordManager.isStopWord(word)) {
|
||||||
this.wordFreqManager.incrementCount(word);
|
this.wordFreqManager.incrementCount(word);
|
||||||
}
|
}
|
||||||
@@ -52,27 +52,28 @@ class WordFrequencyController extends TFExercise {
|
|||||||
|
|
||||||
/** Models the contents of the file. */
|
/** Models the contents of the file. */
|
||||||
class DataStorageManager extends TFExercise {
|
class DataStorageManager extends TFExercise {
|
||||||
private String data;
|
private List<String> words;
|
||||||
|
|
||||||
public DataStorageManager(String pathToFile) throws IOException {
|
public DataStorageManager(String pathToFile) throws IOException {
|
||||||
byte[] dataBytes;
|
this.words = new ArrayList<String>();
|
||||||
RandomAccessFile f = new RandomAccessFile(new File(pathToFile), "r");
|
|
||||||
|
Scanner f = new Scanner(new File(pathToFile), "UTF-8");
|
||||||
try {
|
try {
|
||||||
dataBytes = new byte[(int) f.length()];
|
f.useDelimiter("[\\W_]+");
|
||||||
f.readFully(dataBytes);
|
while (f.hasNext()) {
|
||||||
|
this.words.add(f.next().toLowerCase());
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.data = new String(dataBytes, "UTF-8").replaceAll("[\\W_]+", " ").toLowerCase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] calculateWords() {
|
public List<String> getWords() {
|
||||||
return this.data.split("\\s+");
|
return this.words;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getInfo() {
|
public String getInfo() {
|
||||||
return super.getInfo() + ": My major data structure is a " + this.data.getClass().getName();
|
return super.getInfo() + ": My major data structure is a " + this.words.getClass().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user