The in-memory file system will store our files. We could write our code to use real files, but this will make it much easier to
The in-memory file system will store our files. We could write our code to use real files, but this will make it much easier to test our code. The file system will store a collection of file entries, where each entry contains the name of the file, and a String containing the file's contents. We can simulate a directory structure by using / in filenames, but the file system itself doesn't know anything about directories - it just associates a filename (whether it contains a / or not) with the file's contents. The classes GitFileSystem and GitFileSystemEntry have been provided for you and most of its code is complete. You will need to fill in the empty methods. The file entries in the file system must be kept sorted by filename, and the TreeSet object will help for us.
Code:
gitfilesystem
import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.SortedSet; import java.util.TreeSet;
/** * An in-memory filesystem. * @author tim * @version 1.0 * */ public class GitFileSystem { /** The files in the filesystem, stored in order by filename. */ private SortedSet files = new TreeSet();
/** * Returns the # of files. * @return the # of files */ public int size() { return files.size(); }
/** * Returns the files. * @return the files */ public Collection getFiles() { return Collections.unmodifiableSortedSet(files); }
/** * Returns the file entry for the given file. * @param filename the file * @return the entry, or null */ public GitFileSystemEntry getFile(String filename) { for (GitFileSystemEntry entry : files) { if (entry.getFilename().equals(filename)) { return entry; } }
return null; }
/** * Returns true if the file exists. * @param filename the filename * @return true if the file exists */ public boolean exists(String filename) { return getFile(filename) != null; }
/** * Deletes a file. * @param filename the filename * @return true if found and deleted */ public boolean remove(String filename) { for (Iterator iter = files.iterator(); iter.hasNext();) { GitFileSystemEntry entry = iter.next(); if (filename.equals(entry.getFilename())) { iter.remove(); return true; } }
return false; }
/** * Reads the contents of the file with the given name. * @param filename the filename * @return the contents of the file, or null if the file isn't present */ public String readFileContents(String filename) { return null;
}
/** * Writes the given contents to a file. The file is created if it doesn't exist. * @param filename the filename * @param contents the contents */ public void writeFileContents(String filename, String contents) { }
/** * Returns the files in the working directory. That is, all files * whose filenames don't start with .git. The files must be in sorted * order by filename. * @return the files in the working directory */ public List getWorkingDirFiles() { return null; } }
gitfilesystementry
public class GitFileSystemEntry implements Comparable { /** The filename. */ private String filename; /** The contents. */ private String contents;
/** * Creates a FileEntry. * @param filename the filename * @param contents the contents */ public GitFileSystemEntry(String filename, String contents) { this.filename = filename; this.contents = contents; }
/** * Returns the filename. * @return the filename */ public String getFilename() { return filename; }
/** * Returns the contents. * @return the contents */ public String getContents() { return contents; }
/** * Sets the file contents. * @param contents the contents */ public void setContents(String contents) { this.contents = contents; }
@Override public int compareTo(GitFileSystemEntry that) { return this.filename.compareTo(that.filename); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To complete the implementation of the GitFileSystem class and GitFileSystemEntry class you need to fill in the empty methods as follows 1 GitFileSyste...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started