Advertisement
debetimi

packagemanager.java

Mar 30th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.05 KB | None | 0 0
  1. package sfdctest;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.TreeMap;
  8. import java.util.HashSet;
  9. import java.util.Map;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.util.Scanner;
  13.  
  14. public class PackageManager {
  15.    
  16.     private Map<String, Package> system;
  17.     private Map<String, Package> installed;
  18.     public static enum Actions {
  19.         DEPEND,
  20.         INSTALL,
  21.         REMOVE,
  22.         LIST,
  23.         END
  24.     }
  25.  
  26.     private class Package{
  27.         private String name;
  28.         private Set<Package> dependencies;
  29.         private Set<Package> dependents;
  30.         private boolean isActive;
  31.         private boolean firstClass;
  32.    
  33.         public Package(String name){
  34.             this.name  = name;
  35.             this.dependencies = new HashSet<Package>();
  36.             this.dependents = new HashSet<Package>();
  37.             this.isActive = false;
  38.             this.firstClass = false;
  39.         }
  40.        
  41.         public boolean addDependency(Package p){
  42.             if (this.isActive){
  43.                 System.out.println("\tCannot add dependency to installed package. Uninstall first");
  44.                 return false;
  45.             }
  46.             if (!this.hasCircularDependencyOn(p)){
  47.                 this.dependencies.add(p);
  48.                 //p.addDependent(this);
  49.                 return true;
  50.             } else {
  51.                 System.out.println("\t" + this.getName() + " depends on " + p.getName() + ". Ignoring command.");
  52.                 return false;
  53.             }
  54.         }  
  55.        
  56.         public boolean hasCircularDependencyOn(Package p){
  57.             LinkedList<Package> remaining = new LinkedList<Package>();
  58.             remaining.add(p);
  59.             List<Package> visited = new ArrayList<Package>();
  60.             while(!remaining.isEmpty()){
  61.                 Package dependent = remaining.removeFirst();
  62.                 if (dependent.equals(this)){
  63.                     return true;
  64.                 } else {
  65.                     visited.add(dependent);
  66.                     for (Package c: dependent.dependencies()){
  67.                         if (!visited.contains(c)){
  68.                             remaining.addLast(c);
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.             return false;
  74.         }
  75.        
  76.         public void addDependent(Package p){
  77.             this.dependents.add(p);
  78.         }
  79.        
  80.        
  81.         public void removeDependent(Package p){
  82.             this.dependents.remove(p);
  83.             if (this.dependents.size() == 0 && !this.firstClass){
  84.                 uninstall();
  85.             }
  86.         }
  87.        
  88.         @Override
  89.         public String toString(){
  90.             return this.name;
  91.         }
  92.        
  93.         public String getName(){
  94.             return this.name;
  95.         }
  96.        
  97.         public Set<Package> dependencies(){
  98.             return this.dependencies;
  99.         }
  100.        
  101.         //installs as a primary package meaning it uses itself and is
  102.         //not simply a dependency
  103.         public void installPrimary(){
  104.             if (!this.install()){
  105.                 System.out.println("\t" + this.getName() + " already installed.");
  106.             };
  107.             this.firstClass = true;
  108.         }
  109.        
  110.         public boolean install(){
  111.             if (this.isActive){
  112.                 return false;
  113.             }
  114.             for (Package dependency: this.dependencies){
  115.                 if (!installed.containsKey(dependency.getName())){
  116.                     dependency.install();
  117.                 }
  118.                 //Let this package know its is being used by us
  119.                 dependency.addDependent(this);
  120.             }
  121.             installed.put(this.name, this);
  122.             this.isActive = true;
  123.             System.out.println("\tInstalling " + this.name);
  124.             return true;
  125.         }
  126.        
  127.        
  128.         public void uninstallPrimary(){
  129.             if(!this.uninstall()){
  130.                 System.out.println("\t"+ this.name + " is still needed");
  131.             }
  132.             this.firstClass = false;
  133.         }
  134.        
  135.         public boolean uninstall(){
  136.             if (this.dependents.size() > 0){
  137.                 return false;
  138.             }
  139.            
  140.             installed.remove(this.getName());
  141.            
  142.             this.isActive = false;
  143.            
  144.             for(Package using: this.dependencies){
  145.                 using.removeDependent(this);
  146.             }
  147.            
  148.             System.out.println("\tRemoving " + this.getName());
  149.             return true;
  150.         }
  151.     }
  152.    
  153.     public PackageManager(){
  154.         this.system = new HashMap<String, Package>();
  155.         this.installed = new TreeMap<String, Package>();
  156.     }
  157.    
  158.    
  159.     public Package createPackage(String package_name){
  160.         this.system.put(package_name, new Package(package_name));
  161.         return getPackage(package_name);
  162.     }
  163.    
  164.     public Package getPackage(String package_name){
  165.         return this.system.get(package_name);
  166.     }
  167.    
  168.     public void createDependencies(String[] packages){
  169.         for (int i = 1; i < packages.length; i++){
  170.             if(!this.system.containsKey(packages[i])){
  171.                 createPackage(packages[i]);
  172.             }
  173.         }
  174.         Package primary = this.system.get(packages[1]);
  175.         for (int i = 2; i < packages.length; i++){
  176.             primary.addDependency(getPackage(packages[i]));
  177.         }
  178.     }
  179.     public void installPackage(String package_name){
  180.         Package p;
  181.         if (!this.system.containsKey(package_name)){
  182.             p = createPackage(package_name);
  183.         } else {
  184.             p = getPackage(package_name);
  185.         }
  186.         p.installPrimary();
  187.     }
  188.    
  189.     public void listPackages(){
  190.         for(String name: this.installed.keySet()){
  191.             System.out.println("\t"+name);
  192.         }
  193.     }
  194.    
  195.    
  196.     public Map<String, Package> getInstalledPackages(){
  197.         return this.installed;
  198.     }
  199.    
  200.     public Map<String, Package> getSystemPackages(){
  201.         return this.system;
  202.     }
  203.     public void uninstallPackage(String package_name){
  204.         if (this.installed.containsKey(package_name)){
  205.             Package p = getPackage(package_name);
  206.             p.uninstallPrimary();
  207.         } else {
  208.             System.out.println("\t" + package_name + " is not installed");
  209.         }
  210.  
  211.     }
  212.        
  213.     public static void main(String[] args){
  214.        
  215.         PackageManager aptGet = new PackageManager();
  216.         File instructions = new File("/Users/timi/src/study/Study/src/sftestprep/datax.txt");
  217.         Scanner scanner = null;
  218.         try {
  219.             scanner = new Scanner(instructions);
  220.             while (scanner.hasNextLine()){
  221.                 String line = scanner.nextLine();
  222.                 //Echo the line
  223.                 System.out.println(line);
  224.                 String[] tokens = line.split("\\s+");
  225.                 String command = tokens[0];
  226.                 switch(PackageManager.Actions.valueOf(command)) {
  227.                    
  228.                     case DEPEND:
  229.                         aptGet.createDependencies(tokens);
  230.                         break;
  231.                     case INSTALL:
  232.                         aptGet.installPackage(tokens[1]);
  233.                         break;
  234.                     case REMOVE:
  235.                         aptGet.uninstallPackage(tokens[1]);
  236.                         break;
  237.                     case LIST:
  238.                         aptGet.listPackages();
  239.                         break;
  240.                     case END:
  241.                         //We want to exit the while loop on this
  242.                         return;
  243.                     default:
  244.                         break;
  245.                 }
  246.             }
  247.         } catch(FileNotFoundException e) {
  248.             e.printStackTrace();
  249.         } finally {
  250.             scanner.close();
  251.         }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement