Skip to content

Commit

Permalink
Created class to periodically extract some word from the dictionary. …
Browse files Browse the repository at this point in the history
…Thread-safe implementation on the current extracted word.
  • Loading branch information
FabriDeCastelli committed Aug 13, 2023
1 parent 93b1f39 commit 1aacee1
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main/java/server/service/WordExtractionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package server.service;

import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* Word extraction service.
* Periodically extract a new word from the dictionary.
*/
public class WordExtractionService extends Thread {

public static volatile String currentWord;
private static final List<String> dictionary;
private static final List<String> extractedWords;

static {
extractedWords = new ArrayList<>();
dictionary = new ArrayList<>();
try (final RandomAccessFile file =
new RandomAccessFile("src/main/java/server/conf/words.txt", "r")) {
String line = file.readLine();
while (line != null) {
dictionary.add(line);
line = file.readLine();
}
} catch (Exception e) {
System.out.println("Error reading words from file.");
}
}


/**
* Constructor for WordExtractionService.
*/
public WordExtractionService() {
}


/**
* Runs the service.
*/
@Override
public void run() {
this.extractWord();
}


/**
* Randomly extracts a new word from the dictionary.
*/
private void extractWord() {

final Random random = new Random();
final List<String> notExtractedWords =
dictionary.stream().filter(word -> !extractedWords.contains(word)).toList();
final String word = dictionary.get(random.nextInt(notExtractedWords.size()));
extractedWords.add(word);
currentWord = word;

}


}

0 comments on commit 1aacee1

Please sign in to comment.