Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
In memory file system implementation using Java
package oop.filesystem;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public class Directory extends Node {
private Set<Node> nodes;
public Directory(String path) {
super(path);
nodes = new TreeSet<>();
}
public void add(Node node) {
node.setRoot(this);
nodes.add(node);
}
public Set<Node> getNodes() {
return nodes;
}
@Override
public long getLength() {
long length = 0;
for (Node node : nodes) {
length += node.getLength();
}
return length;
}
public void printTree() {
int indent = 0;
StringBuilder sb = new StringBuilder();
printDirectoryTree(this, indent, sb);
System.out.println(sb.toString());
}
private void printDirectoryTree(Node node, int indent, StringBuilder sb) {
sb.append(getIndentString(indent));
sb.append("+--");
sb.append(node.getName());
sb.append("/");
sb.append("\n");
if (node.isDirectory()) {
Directory directory = (Directory) node;
for (Node file : directory.getNodes()) {
printDirectoryTree(file, indent + 1, sb);
}
}
}
private static String getIndentString(int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append("| ");
}
return sb.toString();
}
}
package oop.filesystem;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public class File extends Node {
private String content; /// for simplicity
public File(String name, String content) {
super(name);
this.content = content;
}
public File(String name) {
super(name);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public long getLength() {
if (content != null) {
return content.getBytes().length;
}
return 0;
}
}
package oop.filesystem;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public class Main {
public static void main(String[] args) {
Directory root = new Directory("root");
File file = new File("profile.jpg");
root.add(file);
Directory movie = new Directory("movie");
root.add(movie);
Directory englishMovie = new Directory("english");
englishMovie.add(new File("IronFist.mp4"));
englishMovie.add(new File("The Shawshank Redemption.mp4"));
englishMovie.add(new File("ZotaPia.mp4"));
File despicableMe = new File("DespicableMe.mp4");
englishMovie.add(despicableMe);
movie.add(englishMovie);
Directory banglaMovie = new Directory("Bangla");
banglaMovie.add(new File("The Clay Bird.mp4"));
banglaMovie.add(new File("Jibon Thekey Neya.mp4"));
movie.add(banglaMovie);
root.printTree();
System.out.println("name: " + movie.getName());
System.out.println("Created: " + movie.getCreated());
}
}
package oop.filesystem;
import java.time.LocalDateTime;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public abstract class Node implements Comparable<Node> {
private Directory root;
private String name;
private LocalDateTime created;
private LocalDateTime lastUpdated;
private LocalDateTime lastAccessed;
public Node(String name) {
this.name = name;
this.created = LocalDateTime.now();
this.lastUpdated = LocalDateTime.now();
this.lastAccessed = LocalDateTime.now();
}
public boolean isDirectory() {
return this instanceof Directory;
}
public String getPath() {
return root != null ? root.getPath() + "/" + name : name;
}
public Node getParent() {
return root;
}
public abstract long getLength();
public String getName() {
return name;
}
@Override
public int compareTo(Node o) {
return this.getName().compareTo(o.getName());
}
public void setRoot(Directory root) {
this.root = root;
}
public LocalDateTime getCreated() {
return created;
}
public LocalDateTime getLastUpdated() {
return lastUpdated;
}
public LocalDateTime getLastAccessed() {
return lastAccessed;
}
@Override
public String toString() {
return "root=" + root +
", \nname='" + name + '\'' +
", \ncreated=" + created +
", \nlastUpdated=" + lastUpdated +
", \nlastAccessed=" + lastAccessed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment