Guest User

gagdadagda

a guest
Jun 30th, 2012
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4.  
  5. public class Directory extends Node {
  6.     private List<INode> subElements;
  7.  
  8.     @Override
  9.     public void accept(FSVisitor visitor) {
  10.         visitor.visit(this);
  11.         for (INode node : subElements)
  12.             node.accept(visitor);
  13.     }
  14.    
  15.     public Directory (String name, int id){
  16.         super(name, id);
  17.         this.subElements = new ArrayList<INode>();
  18.     }
  19.    
  20.     public List<INode> getSubs(){
  21.         return subElements;
  22.     }
  23.    
  24.     public void add (INode newNode){
  25.         newNode.setParent(this);
  26.         subElements.add(newNode);
  27.     }
  28.    
  29.    
  30.     public void remove (int id){
  31.         INode curNode = null;
  32.         for (INode searchNode : subElements)
  33.             if (searchNode.getId() == id){
  34.                 curNode = searchNode;
  35.                 break;
  36.             }
  37.         if (curNode != null)
  38.             subElements.remove(curNode);
  39.     }
  40.    
  41.     public String toString(){
  42.         if (parent == null)
  43.             return getName();
  44.         return parent.getName() + getName() + "/";
  45.     }
  46.  
  47. }
  48.  
  49. public class File extends Node implements INode {
  50.  
  51.     protected File(String name, int id) {
  52.         super(name, id);
  53.     }
  54.  
  55.     @Override
  56.     public void accept(FSVisitor visitor) {
  57.         visitor.visit(this);
  58.     }
  59.  
  60.     @Override
  61.     public String toString() {
  62.         return parent.toString() + getName();
  63.     }
  64.  
  65.    
  66.  
  67. }
  68.  
  69. public class FSException extends Exception {
  70.     private String msg;
  71.  
  72.     /**
  73.      *
  74.      */
  75.     private static final long serialVersionUID = 1L;
  76.  
  77.     @Override
  78.     public String getMessage() {
  79.         // TODO Auto-generated method stub
  80.         return msg;
  81.     }
  82.    
  83.     public FSException(String msg){
  84.         this.msg = msg;
  85.     }
  86.  
  87. }
  88. import java.util.List;
  89.  
  90.  
  91. public class FSManager {
  92.     private Directory root;
  93.    
  94.     private enum ElemType{
  95.         FILE, DIRECTORY
  96.     }
  97.    
  98.     private List<Directory> getPossibleDirs(int parentId){
  99.         SearchVisitor searchVisitor = new SearchVisitor(parentId);
  100.         root.accept(searchVisitor);
  101.         return searchVisitor.getResults();
  102.     }
  103.    
  104.     private void addElement(INode node, Directory parent){
  105.         parent.add(node);
  106.     }
  107.    
  108.     private void addElement(INode newElement, int parentId) throws FSException{
  109.         List<Directory> possibleDirs = getPossibleDirs(parentId);
  110.         for (Directory curDir : possibleDirs){
  111.             if (checkUnique(newElement, curDir)){
  112.                 addElement(newElement, curDir);
  113.                 return;
  114.             }
  115.         }
  116.         throw new FSException("No suitable directory");
  117.     }
  118.    
  119.     private boolean checkUnique(INode node, Directory dir){
  120.         for (INode curNode : dir.getSubs()){
  121.             if (curNode.getId() == node.getId() || curNode.getName() == node.getName())
  122.                 return false;
  123.         }
  124.         return true;
  125.     }
  126.    
  127.     private boolean exists(int id, Directory dir, ElemType elemType){
  128.         for (INode curNode : dir.getSubs()){
  129.             if (curNode.getId() == id && ((elemType == ElemType.FILE && curNode instanceof File) || (elemType == ElemType.DIRECTORY && curNode instanceof Directory)))
  130.                 return true;           
  131.         }
  132.         return false;
  133.     }
  134.    
  135.     private void removeElement(int id, int parentId, ElemType fileOrDir) throws FSException{
  136.         List<Directory> possibleDirs = getPossibleDirs(parentId);
  137.         for (Directory curDir : possibleDirs ){
  138.             if (exists(id, curDir, fileOrDir)){
  139.                 curDir.remove(id);
  140.                 return;
  141.             }
  142.         }
  143.         throw new FSException("No suitable directory");
  144.     }
  145.    
  146.     public void addFolder(int id, String name, int parentId) throws FSException{
  147.         Directory newDir = new Directory(name, id);
  148.         addElement(newDir, parentId);
  149.     }
  150.    
  151.     public void addFile(int id, String name, int parentId) throws FSException {
  152.         File newFile = new File(name, id);
  153.         addElement(newFile, parentId);
  154.     }
  155.    
  156.    
  157.     public void deleteFolder (int id, int parentId) throws FSException{
  158.         removeElement(id, parentId, ElemType.DIRECTORY);
  159.     }
  160.    
  161.     public void deleteFile (int id, int parentId) throws FSException{
  162.         removeElement(id, parentId, ElemType.FILE);
  163.     }
  164.    
  165.    
  166.     public void printFS(){
  167.         FSVisitor printer = new FSPrinter();
  168.         root.accept(printer);
  169.     }
  170.    
  171.     public FSManager(){
  172.         root = new Directory("/", 0);
  173.         root.setParent(null);
  174.     }
  175.    
  176.    
  177. }
  178.  
  179. public class FSPrinter implements FSVisitor {
  180.  
  181.     @Override
  182.     public void visit(Directory dir) {
  183.         System.out.println(dir);
  184.     }
  185.  
  186.     @Override
  187.     public void visit(File file) {
  188.         System.out.println(file);
  189.     }
  190.  
  191. }
  192.  
  193. public interface FSVisitor {
  194.     void visit(Directory dir);
  195.     void visit (File file);
  196. }
  197.  
  198. public interface INode {
  199.     int getId();
  200.     String getName();
  201.     void accept(FSVisitor visitor);
  202.     void setParent (Directory dir);
  203. }
  204.  
  205. public abstract class Node implements INode {
  206.     private int id;
  207.     private String name;
  208.     protected Directory parent;
  209.  
  210.     @Override
  211.     public int getId() {
  212.         return id;
  213.     }
  214.  
  215.     @Override
  216.     public String getName() {
  217.         return name;
  218.     }
  219.    
  220.     protected Node(String name, int id){
  221.         this.id = id;
  222.         this.name = name;
  223.     }
  224.    
  225.     public void setParent(Directory p){
  226.         parent = p;
  227.     }  
  228.  
  229. }
  230. import java.util.ArrayList;
  231. import java.util.List;
  232.  
  233.  
  234. public class SearchVisitor implements FSVisitor {
  235.     private int parentId;
  236.     private List<Directory> results;
  237.    
  238.     public SearchVisitor(int parentId) {
  239.         this.parentId = parentId;
  240.         results = new ArrayList<Directory>();
  241.     }
  242.  
  243.     @Override
  244.     public void visit(Directory dir) {
  245.         if (dir.getId() == parentId)
  246.             results.add(dir);
  247.     }
  248.  
  249.     @Override
  250.     public void visit(File file) {
  251.         // TODO Auto-generated method stub
  252.     }
  253.    
  254.     public List<Directory> getResults(){
  255.         return results;
  256.     }
  257.  
  258. }
  259.  
  260. public class Tester {
  261.  
  262.     /**
  263.      * @param args
  264.      */
  265.     public static void main(String[] args) {
  266.         FSManager fs = new FSManager();
  267.        
  268.         try {
  269.             fs.addFolder(1, "usr", 0);
  270.             fs.addFolder(2, "etc", 0);
  271.             fs.addFile(3, "va", 0);
  272.             fs.addFile(1, "usrs", 1);
  273.             fs.addFile(2, "usrs2", 1);
  274.             fs.printFS();
  275.             fs.deleteFile(2, 1);
  276.             fs.deleteFolder(1, 0);
  277.             fs.printFS();
  278.         } catch (FSException e) {
  279.             // TODO Auto-generated catch block
  280.             e.printStackTrace();
  281.         }
  282.  
  283.     }
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment