import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.concurrent.*; import java.util.AbstractCollection; public class Wiki { // private ConcurrentHashMap<String, SynchronizedStack<TXTFile>> repository; private ConcurrentHashMap<String, Stack<TXTFile>> repository; public Wiki() { // repository = new ConcurrentHashMap<String, // SynchronizedStack<TXTFile>>(); repository = new ConcurrentHashMap<String, Stack<TXTFile>>(); } // HashMap to store files private HashMap<String, TXTFile> files; /** * Method used to wait for input and invoke methods according to that input. */ public void Start() { System.out.println("Wiki started"); String input = ""; Scanner in = new Scanner(System.in); // Create the txt_files folder if it does not already exist. File dir = new File("txt_files"); if(! dir.exists()) { dir.mkdir(); } // Enter a loop to parse all input. do{ System.out.println(">"); input = in.nextLine(); // Perform string matching /*if(input.startsWith("commit")) { commit(); continue; }*/ if(input.startsWith("add")) { /* "\\s+" this groups all whitespaces as a delimiter... so if i have the string "Hello[space][tab]World", this should yield the strings "Hello" and "World" and omit the empty space between the space and the tab*/ Add(input.substring(4).split("\\s+")); continue; } if(input.equals("show")) { list(); continue; } // Fall through case System.out.println("Illegal command: " + input); }while(!input.equals("quit")); // Cleanup. System.out.println("Wiki is terminating..."); in.close(); } /** * Method used for listing all wiki files. */ public void list() { File dir = new File("txt_files"); File[] wfiles = dir.listFiles(); // No files in directory. if (wfiles == null) { // return; System.out.println("There no files !"); } // Initialise streams FileInputStream fis = null; BufferedInputStream bis = null; BufferedReader reader = null; // Loop over all files in the directory. for (File f : wfiles) { // System.out.println(f.getName().endsWith(".txt")); System.out.println(f.getName()); } } /** * Method used for outputting the content of wiki files. */ /* * private void list() { File dir = new File("txt_files"); File[] wfiles = * dir.listFiles(); // No files in directory. if(wfiles == null) { return; } * // Initialise streams FileInputStream fis = null; BufferedInputStream bis * = null; BufferedReader reader = null; * * // Loop over all files in the directory. for(File f : wfiles) { * if(f.getName().endsWith(".txt")) { try{ fis = new FileInputStream(f); bis * = new BufferedInputStream(fis); reader = new BufferedReader(new * InputStreamReader(bis)); String entry; * * while ((entry = reader.readLine()) != null) { * System.out.println(f.getName().substring(0, f.getName().indexOf(".")) + * "\t" + entry); } * * }catch(Exception e){ e.printStackTrace(); }finally{ try{ fis.close(); * bis.close(); reader.close(); }catch(IOException e) { * System.out.println("Error closing streams!"); e.printStackTrace(); } } } * } System.out.println(); } */ // ?? load factor = initial-capacity x .75 = number of items (round up) that // HashMap supports /* * commit method is used before adding a file to the repository if accepted * version < Wiki's newest version, an Exception is thrown HashMap contains * all files which will be added to the repository */ /* * public void commit(HashMap<String, TXTFile> files) throws Exception{ * Iterator<String> keySet = files.keySet().iterator(); while * (keySet.hasNext()) { TXTFile file = files.get(keySet.next()); add(file); * } } */ /** * Adds a file to the repository (1). */ public void Add(String[] tokens) { if (tokens.length < 2) { System.out.println("Illegal arguments"); System.out.println("add [file_name] [content]"); System.out.println(); return; } // processing user input, creating a TXTFile String fileName = tokens[0]; String content = tokens[1]; TXTFile file = new TXTFile(fileName, content); // managing file versions Stack<TXTFile> stack = repository.get(file.getFileName()); if (stack == null) // if there are no versions yet { stack = new Stack<TXTFile>(); file.setVersion(1); TXTFile copy = file.clone(); stack.push(copy); repository.put(file.getFileName(), stack); } else { int subVersion = file.getVersion(); int latestVersion = stack.peek().getVersion(); /* * if(subVersion == -1) throw new Exception( * "Your file has no Version yet. You need to checkout or update before submitting files" * ); * * if(subVersion < latestVersion || subVersion > latestVersion) * throw new Exception("Your file \"" +file.getFileName() * +"\" has an outdated or not allowed version"); */ file.setVersion(++latestVersion); stack.push(file.clone()); } } /** * Adds a file to the repository (2). */ /* * public void add(TXTFile file) throws Exception { if * (file.getFileName().length() < 1) throw new Exception("No FileName !"); * * //SynchronizedStack<TXTFile> stack = repository.get(file.getFileName()); * Stack<TXTFile> stack = repository.get(file.getFileName()); * * if(stack == null) { //stack = new SynchronizedStack<TXTFile>(); stack = * new Stack<TXTFile>(); file.setVersion(0); TXTFile copy = * file.clone(file); stack.push(file.clone(file)); * repository.put(file.getFileName(), stack); } * * else { int subVersion = file.getVersion(); int latestVersion = * stack.peek().getVersion(); * * if(subVersion == -1) throw new Exception( * "Your file has no Version yet. You need to checkout or update before submitting files" * ); * * if(subVersion < latestVersion || subVersion > latestVersion) throw new * Exception("Your file \"" +file.getFileName() * +"\" has an outdated or not allowed version"); * * file.setVersion(++latestVersion); stack.push(file.clone(file)); } } */ // removes a file from the repository public void remove(String fileName) throws Exception { if (!repository.containsKey(fileName)) throw new Exception("The file \"" + fileName + "\" doesnt exist in the Repository"); repository.remove(fileName); } // Update method to update a single file to the newest version available public TXTFile update(String filename) throws Exception { if (!repository.containsKey(filename)) throw new Exception("The file \"" + filename + "\" doesnt exist in the Repository"); // SynchronizedStack<TXTFile> stack = repository.get(filename); Stack<TXTFile> stack = repository.get(filename); TXTFile file = stack.peek(); //try { return file.clone(); //} catch (CloneNotSupportedException ex) { // return null; //} } // Reverts a single file to the version given as parameter public TXTFile revert(String filename, int version) throws Exception { if (!repository.containsKey(filename)) throw new Exception("The file \"" + filename + "\" doesnt exist in the Repository"); // SynchronizedStack<JavaCode> stack = repository.get(filename); Stack<TXTFile> stack = repository.get(filename); if (version < 0 || version >= stack.size()) throw new Exception("Please check your version. Your Version: \"" + version + "\""); TXTFile file = stack.get(version); Boolean found = false; if (file.getVersion() != version) { Iterator<TXTFile> iterator = stack.iterator(); while (iterator.hasNext()) if (iterator.next().getVersion() == version) { found = true; break; } if (!found) throw new Exception("The version \"" + version + "\" of the file \"" + filename + "\" doesn't exist in the Repository"); } return file.clone(); } }
public class TXTFile { private String fileName; private int version; private String content; public TXTFile(String fileName, String content){ this.fileName = fileName; this.content = content; } public int getVersion() { return this.version; } public void setVersion(int version) { this.version = version; } public TXTFile(String fn){ this.fileName = fn; } public String getFileName() { return this.fileName; } public void setFileName(String fileName) { fileName = fileName; } @Override public TXTFile clone(){ try{ return (TXTFile) super.clone(); } catch (CloneNotSupportedException e){ throw new AssertionError(); } } /*public TXTFile clone(TXTFile file) throws CloneNotSupportedException{ return (TXTFile)file.clone(); }*/ public String getContent() { return this.content; } public void setContent(String cont) { this.content = cont; } }
public class Main { public static void main(String[] args) { // Start Wiki Wiki wiki = new Wiki(); wiki.Start(); } }
Console output:
Wiki started
>
show
handler copy 2.txt
handler copy.txt
handler.txt
>
add example.txt content
Exception in thread "main" java.lang.AssertionError
at TXTFile.clone(TXTFile.java:37)
at Wiki.Add(Wiki.java:163)
at Wiki.Start(Wiki.java:59)
at Main.main(Main.java:6)
what's wrong ?