java programming
You will be given two interfaces and two abstract classes,FileTextReader, FileTextWriter,AbstractFileMonitor, and AbstractDictionary. Your job isto create two classes the first class should be namedFileManager, the second class should be namedDictionary. The FileManager will implement theinterfaces FileTextReader and FileTextWriter andextend the clas AbstractFileMonitor. Your class signaturewould look something like the following:
public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{...
The constructor signature of the FileManager shouldlook like the following:
public FileManager(String filePath){...
The Dictionary will extend the abstract classAbstractDictionary. Your class signature wouldlook something like the following:
public class Dictionary extends AbstractDictionary {...
The constructor signature of the Dictionary should looklike the following:
public Dictionary(String path, FileManager fileManager) throws IOException {...
Please read the programmatic documentation of the two interfacesand abstract classes to gain an understanding of what each functionshould do. This assignment will test your knowledge of reading andwriting to a text file, implementing interfaces, extending abstractclasses, throwing exceptions and working with collectionsspecifically sets.
This project is accompanied by two test classes,ManagerTest, and DictionaryTest. They will testthe functionality of the functions in the interfaces and theabstract methods of classes. Its recommended that youimplement the FileManager before working on theAbstractDictionary.
Abstract Dictionary
package homework;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;
* A class that holds a collection of words read from a textfile. The collection of words is used in the methods of thisclass
* methods provided in this class.
public abstract class AbstractDictionary{
private final FileTextReaderFILE_READER;
private final Set ALL_WORDS;
/**
* Creates an abstract dictionary of words using thetext file at the specified path.
* @param path a path to a text file containing adictionary of words
* @param dictionaryFileReader the FileReader used toread from the text file at the specified path
* @throws IOException thrown if there is a problemreading the file at the path
*/
public AbstractDictionary(String path,FileTextReader dictionaryFileReader) throws IOException {
Objects.requireNonNull(dictionaryFileReader, "The File reader cannot be null");
Objects.requireNonNull(path, "Thepath can not be null");
FILE_READER =dictionaryFileReader;
ALL_WORDS =FILE_READER.getAllLines(path);
}
/**
* Returns a set of all the words contained in thedictionary text file.
* @return a set containing all the words in thedictionary file.
*/
public Set getAllWords(){
return ALL_WORDS;
}
/**
* Counts the number of words in this Dictionary thatstart with the specified prefix and have a length that is equal orgreater
* than the specified size. If size the specified sizeis less than 1 then word size is not taken into account.
* @param prefix the prefix to be found
* @param size the length that the word must equal orbe greater than. If a value less than 1 is specified, all wordsregardless of their
* characters size should be considered. In other wordsif the size parameter is < 1 word size is disregarded in thecalculations.
* @param ignoreCase if true this will ignore casedifferences when matching the strings. If false thisconsiders
* case differences when matching the strings
* @return The number of words that start with thespecified prefix
* @throws IllegalArgumentException if the specifiedstring is null or empty (Meaning contains no characters or onlywhite space or blank)
*/
public abstract int countWordsThatStartWith(Stringprefix, int size, boolean ignoreCase) throwsIllegalArgumentException;
/**
* Tests if this Dictionary contains at least one wordwith a length equal to or greater than the specified size thatstarts with the specified prefix.
* If size the specified size is less than 1 then wordsize is not taken into account.
* @param prefix the prefix to be found
* @param size the length that the word must equal orbe greater than. If a value less than 1 is specified, all wordsregardless of their
* characters size should be considered. In other wordsif the size parameter is < 1 word size is disregarded in thecalculations.
* @param ignoreCase if true this will ignore casedifferences when matching the strings. If false thisconsiders
* case differences when matching the strings
* @return The number of words that start with thespecified prefix
* @throws IllegalArgumentException if the specifiedstring is null or empty (Meaning contains no characters or onlywhite space)
*/
public abstract booleancontainsWordsThatStartWith(String prefix, int size, booleanignoreCase) throws IllegalArgumentException;
* Returns a set of all the words within in thisDictionary that start with the specified prefix and have a lengththat is equal or greater
* than the specified size. If size the specified sizeis less than 1 then word size is not taken into account.
* @param prefix the prefix to be found
* @param size the length that the word must equal orbe greater than. If a value less than 1 is specified, all wordsregardless of their
* characters size should be considered. In other wordsif the size parameter is < 1 word size is disregarded in thecalculations.
* @param ignoreCase if true this will ignore casedifferences when matching the strings. If false thisconsiders
* case differences when matching the strings
* @return A list of all strings that start with thespecified prefix
* @throws IllegalArgumentException if the specifiedstring is null or empty (Meaning contains no characters or onlywhite space)
public abstract SetgetWordsThatStartWith(String prefix, int size, boolean ignoreCase)throws IllegalArgumentException;
}
AbstractFileMonitor
package homework;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
* A class with methods to monitor a text file.
public abstract class AbstractFileMonitor {
private MessageDigest messageDigest;
private int currentCheckSum;
private boolean hasChanged = false;
private long nextUpateTime = 0;
public AbstractFileMonitor(String path){
setFilePath(path);
}
* Updates the variables that correspond to the filebeing monitored
* This function has been throttled such that it willonly update values every 250 ms. In other words successive calls tothis
* function in time intervals that are less than 250 mswill yield no change.
* @throws IOException Thrown if any type of I/Oexception occurs while writing to the file
* @throws IllegalStateException If the {@link#setFile(String)} method in not invoked with a proper file prior toinvocation of this
* @throws NoSuchAlgorithmException If the computer'sOS is missing the SHA-256 hashing algorithm
* method. In other words if no file is currently setto be monitored.
public void update() throws IOException,IllegalStateException, NoSuchAlgorithmException {
if(messageDigest ==null){
messageDigest =MessageDigest.getInstance("SHA-256");
}
if(nextUpateTime >System.currentTimeMillis()) {
hasChanged =false;
return;
}
nextUpateTime =System.currentTimeMillis() + 250;
File file = newFile(getFilePath());
if(!file.exists())return;
try (DigestInputStream dis = new DigestInputStream(newFileInputStream(getFilePath()), messageDigest)) {
while (dis.read() != -1) ;
messageDigest = dis.getMessageDigest();
}
StringBuilder result = new StringBuilder();
for (byte b : messageDigest.digest()) {
result.append(String.format("%02x", b));
}
hasChanged = currentCheckSum !=result.toString().hashCode();
currentCheckSum = result.toString().hashCode();
}
* Tests if the file being monitored has changed sincethe last time {@link #update()} was invoked
public boolean hasChanged(){
return hasChanged;
}
public abstract void setFilePath(String path);
public abstract String getFilePath() throwsIllegalStateException;
}
FileTextReader
package homework;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
public interface FileTextReader{
public String readText(String path) throwsIOException;
* @throws IOException Thrown if any type of I/Oexception occurs while writing to the file
public Set getAllLines(String path) throwsIOException;
* @throws IOException Thrown if any type of I/Oexception occurs while writing to the file
public String getLastLine(String path) throwsIOException;
}
FileTextWriter
package homework;
import java.io.IOException;
public interface FileTextWriter {
* @throws IOException Thrown if any type of I/Oexception occurs while writing to the file
* @throws IllegalArgumentException if the specifiedstring is null or empty (Meaning contains no characters or onlywhite space)
public void writeToFile(String string,String path) throws IOException,IllegalArgumentException;
}