Skip to content

Instantly share code, notes, and snippets.

@jiaguofang
Created January 21, 2015 08:05
Show Gist options
  • Save jiaguofang/694fb4a9230b0ed31fde to your computer and use it in GitHub Desktop.
Save jiaguofang/694fb4a9230b0ed31fde to your computer and use it in GitHub Desktop.
Simulation of a file system
package filesystem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
public class SimpleFileSystem {
private Directory rootDir;
private Directory currDir;
public SimpleFileSystem() {
rootDir = new Directory("", null);
currDir = rootDir;
}
public boolean mkdir(String dirName) {
return currDir.addDir(new Directory(dirName, currDir));
}
public void gotoUpperDir() {
if (!currDir.isRootDir())
currDir = currDir.getUpperDir();
}
public void gotoRootDir() {
currDir = rootDir;
}
public boolean cd(String dirName) {
Directory dir = currDir.getDir(dirName);
if (dir != null) {
currDir = dir;
return true;
} else
return false;
}
public void cd() {
currDir = rootDir;
}
public String pwd() {
return currDir.getAbsolutePath();
}
public boolean touch(String fileName) {
return currDir.addFile(new File(fileName));
}
public List<String> ls() {
return currDir.getEntityNames();
}
public static void main(String[] args) throws IOException {
SimpleFileSystem fs = new SimpleFileSystem();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print(fs.pwd());
System.out.print(":");
String input = br.readLine();
input = input.trim();
if (input.startsWith("mkdir")) {
String param = input.substring(5).trim();
if (param.equals(""))
System.out.println("<need more parameter>");
else if (!fs.mkdir(param))
System.out.println("<directory already exists>");
;
} else if (input.startsWith("cd")) {
String param = input.substring(2).trim();
if (param.equals("")) {
fs.gotoRootDir();
} else if (param.equals("..")) {
fs.gotoUpperDir();
} else if (!fs.cd(param)) {
System.out.println("<directory not exist>");
}
} else if (input.startsWith("touch")) {
String param = input.substring(5).trim();
if (param.equals(""))
System.out.println("<need more parameter>");
else if (!fs.touch(param))
System.out.println("<file already exists>");
} else if (input.startsWith("ls")) {
List<String> entityNames = fs.ls();
if (entityNames.isEmpty())
System.out.println("");
for (String s : entityNames)
System.out.println(s);
} else if (input.startsWith("pwd"))
System.out.println(fs.pwd());
else if (input.startsWith("quit") || input.startsWith("exit"))
break;
else
System.out.println("<unrecognized command>");
;
}
}
abstract class Entity implements Comparable<Entity> {
String name;
Entity(String name) {
this.name = name;
}
String getDisplayName() {
return name;
}
public int compareTo(Entity entity) {
return name.compareTo(entity.name);
}
}
class Directory extends Entity {
Directory upperDir;
SortedSet<Entity> entities;
Directory(String name, Directory parent) {
super(name);
this.upperDir = parent;
entities = new TreeSet<Entity>();
}
boolean addDir(Entity entity) {
return addEntity(entity);
}
boolean addFile(Entity entity) {
return addEntity(entity);
}
boolean addEntity(Entity entity) {
return entities.add(entity);
}
Directory getDir(String entityName) {
if (entityName.equals("..")) {
if (upperDir != null)
return upperDir;
else
return this;
}
for (Entity entity : entities)
if (entity instanceof Directory
&& entity.name.equals(entityName))
return (Directory) entity;
return null;
}
boolean isRootDir() {
return upperDir == null;
}
Directory getUpperDir() {
return upperDir;
}
List<String> getEntityNames() {
List<String> entityNames = new LinkedList<String>();
for (Entity entity : entities)
entityNames.add(entity.getDisplayName());
return entityNames;
}
String getAbsolutePath() {
List<String> dirNames = new LinkedList<String>();
Directory dir = this;
while (dir != null) {
dirNames.add(dir.getDisplayName());
dir = dir.upperDir;
}
Collections.reverse(dirNames);
StringBuilder sb = new StringBuilder();
for (String dirName : dirNames)
sb.append(dirName);
return sb.toString();
}
String getDisplayName() {
return name + "/";
}
@Override
public int compareTo(Entity entity) {
if (entity instanceof Directory)
return name.compareTo(entity.name);
return 1;
}
}
class File extends Entity {
File(String name) {
super(name);
}
@Override
public int compareTo(Entity entity) {
if (entity instanceof File)
return name.compareTo(entity.name);
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment