Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class Directory extends Node {
- private List<INode> subElements;
- @Override
- public void accept(FSVisitor visitor) {
- visitor.visit(this);
- for (INode node : subElements)
- node.accept(visitor);
- }
- public Directory (String name, int id){
- super(name, id);
- this.subElements = new ArrayList<INode>();
- }
- public List<INode> getSubs(){
- return subElements;
- }
- public void add (INode newNode){
- newNode.setParent(this);
- subElements.add(newNode);
- }
- public void remove (int id){
- INode curNode = null;
- for (INode searchNode : subElements)
- if (searchNode.getId() == id){
- curNode = searchNode;
- break;
- }
- if (curNode != null)
- subElements.remove(curNode);
- }
- public String toString(){
- if (parent == null)
- return getName();
- return parent.getName() + getName() + "/";
- }
- }
- public class File extends Node implements INode {
- protected File(String name, int id) {
- super(name, id);
- }
- @Override
- public void accept(FSVisitor visitor) {
- visitor.visit(this);
- }
- @Override
- public String toString() {
- return parent.toString() + getName();
- }
- }
- public class FSException extends Exception {
- private String msg;
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- @Override
- public String getMessage() {
- // TODO Auto-generated method stub
- return msg;
- }
- public FSException(String msg){
- this.msg = msg;
- }
- }
- import java.util.List;
- public class FSManager {
- private Directory root;
- private enum ElemType{
- FILE, DIRECTORY
- }
- private List<Directory> getPossibleDirs(int parentId){
- SearchVisitor searchVisitor = new SearchVisitor(parentId);
- root.accept(searchVisitor);
- return searchVisitor.getResults();
- }
- private void addElement(INode node, Directory parent){
- parent.add(node);
- }
- private void addElement(INode newElement, int parentId) throws FSException{
- List<Directory> possibleDirs = getPossibleDirs(parentId);
- for (Directory curDir : possibleDirs){
- if (checkUnique(newElement, curDir)){
- addElement(newElement, curDir);
- return;
- }
- }
- throw new FSException("No suitable directory");
- }
- private boolean checkUnique(INode node, Directory dir){
- for (INode curNode : dir.getSubs()){
- if (curNode.getId() == node.getId() || curNode.getName() == node.getName())
- return false;
- }
- return true;
- }
- private boolean exists(int id, Directory dir, ElemType elemType){
- for (INode curNode : dir.getSubs()){
- if (curNode.getId() == id && ((elemType == ElemType.FILE && curNode instanceof File) || (elemType == ElemType.DIRECTORY && curNode instanceof Directory)))
- return true;
- }
- return false;
- }
- private void removeElement(int id, int parentId, ElemType fileOrDir) throws FSException{
- List<Directory> possibleDirs = getPossibleDirs(parentId);
- for (Directory curDir : possibleDirs ){
- if (exists(id, curDir, fileOrDir)){
- curDir.remove(id);
- return;
- }
- }
- throw new FSException("No suitable directory");
- }
- public void addFolder(int id, String name, int parentId) throws FSException{
- Directory newDir = new Directory(name, id);
- addElement(newDir, parentId);
- }
- public void addFile(int id, String name, int parentId) throws FSException {
- File newFile = new File(name, id);
- addElement(newFile, parentId);
- }
- public void deleteFolder (int id, int parentId) throws FSException{
- removeElement(id, parentId, ElemType.DIRECTORY);
- }
- public void deleteFile (int id, int parentId) throws FSException{
- removeElement(id, parentId, ElemType.FILE);
- }
- public void printFS(){
- FSVisitor printer = new FSPrinter();
- root.accept(printer);
- }
- public FSManager(){
- root = new Directory("/", 0);
- root.setParent(null);
- }
- }
- public class FSPrinter implements FSVisitor {
- @Override
- public void visit(Directory dir) {
- System.out.println(dir);
- }
- @Override
- public void visit(File file) {
- System.out.println(file);
- }
- }
- public interface FSVisitor {
- void visit(Directory dir);
- void visit (File file);
- }
- public interface INode {
- int getId();
- String getName();
- void accept(FSVisitor visitor);
- void setParent (Directory dir);
- }
- public abstract class Node implements INode {
- private int id;
- private String name;
- protected Directory parent;
- @Override
- public int getId() {
- return id;
- }
- @Override
- public String getName() {
- return name;
- }
- protected Node(String name, int id){
- this.id = id;
- this.name = name;
- }
- public void setParent(Directory p){
- parent = p;
- }
- }
- import java.util.ArrayList;
- import java.util.List;
- public class SearchVisitor implements FSVisitor {
- private int parentId;
- private List<Directory> results;
- public SearchVisitor(int parentId) {
- this.parentId = parentId;
- results = new ArrayList<Directory>();
- }
- @Override
- public void visit(Directory dir) {
- if (dir.getId() == parentId)
- results.add(dir);
- }
- @Override
- public void visit(File file) {
- // TODO Auto-generated method stub
- }
- public List<Directory> getResults(){
- return results;
- }
- }
- public class Tester {
- /**
- * @param args
- */
- public static void main(String[] args) {
- FSManager fs = new FSManager();
- try {
- fs.addFolder(1, "usr", 0);
- fs.addFolder(2, "etc", 0);
- fs.addFile(3, "va", 0);
- fs.addFile(1, "usrs", 1);
- fs.addFile(2, "usrs2", 1);
- fs.printFS();
- fs.deleteFile(2, 1);
- fs.deleteFolder(1, 0);
- fs.printFS();
- } catch (FSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment