From d43e79babd7ec2d4109363510afd2e4963fac29f Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Sun, 21 Jul 2019 15:16:02 +0200 Subject: [PATCH] Format and minor docs. --- src/app/Artifacts.java | 10 ++- src/app/Controller.java | 3 + src/app/Execute.java | 3 +- src/app/Main.java | 3 +- src/app/QuicksortMd5.java | 138 ++++++++++++++++++++------------------ src/app/Routines.java | 32 +++++---- src/app/Tools.java | 35 +++++----- src/app/Write.java | 91 +++++++++++++------------ 8 files changed, 166 insertions(+), 149 deletions(-) diff --git a/src/app/Artifacts.java b/src/app/Artifacts.java index af17b83..5d5e863 100644 --- a/src/app/Artifacts.java +++ b/src/app/Artifacts.java @@ -6,16 +6,20 @@ public class Artifacts { Tools tools = new Tools(); - + /** + * @param file + * @return + */ public MFile getMFile(File file) { MFile mfile = new MFile(); mfile.file = file; mfile.md5 = tools.getMd5(file.getPath()); return mfile; - - } + /** + * + */ public class MFile { public File file; public String md5; diff --git a/src/app/Controller.java b/src/app/Controller.java index 9ce0a8c..701a422 100644 --- a/src/app/Controller.java +++ b/src/app/Controller.java @@ -16,6 +16,9 @@ import java.util.Map; import app.Artifacts.MFile; +/*** + * Controller class for JavaFX. Contains the application logic. + */ public class Controller { diff --git a/src/app/Execute.java b/src/app/Execute.java index d2ceb92..7f3e6c7 100644 --- a/src/app/Execute.java +++ b/src/app/Execute.java @@ -7,8 +7,7 @@ public class Execute { /** - * - * @param cmd an array representing a shell command + * @param cmd an array representing a shell command * @return TwoBr class, containing two BufferedReaders, * output and error * @see output BufferedReader, corresponds to STDOUT diff --git a/src/app/Main.java b/src/app/Main.java index 6fd3d9a..866edbb 100644 --- a/src/app/Main.java +++ b/src/app/Main.java @@ -13,10 +13,9 @@ import javafx.stage.Stage; public class Main extends Application { - static String foo; @Override - public void start(Stage primaryStage) throws Exception{ + public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("layout.fxml")); Scene scene = new Scene(root, 300, 275); diff --git a/src/app/QuicksortMd5.java b/src/app/QuicksortMd5.java index 2c60145..dd338ab 100644 --- a/src/app/QuicksortMd5.java +++ b/src/app/QuicksortMd5.java @@ -5,72 +5,76 @@ import app.Artifacts.MFile; import java.util.Map; public class QuicksortMd5 { - private Map md5Pool; - private int i; - private int j; - private int p; - - public Map quicksortMd5(Map md5Pool) { - this.md5Pool = md5Pool; - - quicksort(0, md5Pool.size() - 1); - return this.md5Pool; - } - - private void quicksort(int low, int high) { - i = low; - j = high; - p = low + (high - low) / 2; - - while (i < j) { - - // a < b a.compareto(b) = -1 - // a = b a.compareto(b) = 0 - // a > b a.compareto(b) = 1 - - while (i < p) { - if (imd5().compareTo(pmd5()) <= 0) { - i++; - } else { - swap(i, p); - j = high; - } - } - - while (p < j) { - if (jmd5().compareTo(pmd5()) >= 0) { - j--; - } else { - swap(p, j); - i = low; - } - } - } - - if (high - p > 2) { - quicksort(p, high); - } - if (p - low > 2) { - quicksort(low, p); - } - } - - private void swap(int a, int b) { - MFile temp = md5Pool.get(a); - md5Pool.put(a, md5Pool.get(b)); - md5Pool.put(b, temp); - } - - private String imd5() { - return md5Pool.get(i).md5; - } - - private String jmd5() { - return md5Pool.get(j).md5; - } - - private String pmd5() { - return md5Pool.get(p).md5; - } + private Map md5Pool; + private int i; + private int j; + private int p; + + /** + * @param md5Pool + * @return a Map sorted by md5. + */ + public Map quicksortMd5(Map md5Pool) { + this.md5Pool = md5Pool; + + quicksort(0, md5Pool.size() - 1); + return this.md5Pool; + } + + private void quicksort(int low, int high) { + i = low; + j = high; + p = low + (high - low) / 2; + + while (i < j) { + + // a < b a.compareto(b) = -1 + // a = b a.compareto(b) = 0 + // a > b a.compareto(b) = 1 + + while (i < p) { + if (imd5().compareTo(pmd5()) <= 0) { + i++; + } else { + swap(i, p); + j = high; + } + } + + while (p < j) { + if (jmd5().compareTo(pmd5()) >= 0) { + j--; + } else { + swap(p, j); + i = low; + } + } + } + + if (high - p > 2) { + quicksort(p, high); + } + if (p - low > 2) { + quicksort(low, p); + } + } + + private void swap(int a, int b) { + MFile temp = md5Pool.get(a); + md5Pool.put(a, md5Pool.get(b)); + md5Pool.put(b, temp); + } + + private String imd5() { + return md5Pool.get(i).md5; + } + + private String jmd5() { + return md5Pool.get(j).md5; + } + + private String pmd5() { + return md5Pool.get(p).md5; + } } diff --git a/src/app/Routines.java b/src/app/Routines.java index 32f6182..21c6614 100644 --- a/src/app/Routines.java +++ b/src/app/Routines.java @@ -12,28 +12,24 @@ import app.Artifacts.MFile; public class Routines { - - Execute x = null; - - Tools tools = new Tools(); - Write write = new Write(); + Execute x; + Tools tools; public Routines() { this.x = new Execute(); - this.tools = new Tools(); } /** - * [1] Write output of find srcdir to /tmp/find
- * [2] Read /tmp/find into List>String>
- * [3] Add List>String> entries to Map>String,File> , where + * [1] Write output of find srcdir to /tmp/find .
+ * [2] Read /tmp/find into List< String /> .
+ * [3] Add List< String /> entries to Map>String,File> , where * String is an int key.
* - * @param srcdir - * @param type file OR directory + * @param srcdir String + * @param type String "file" OR "dir" , pick what type will be loaded. * @return filepool */ public Map loadPool(String srcdir, String type) { @@ -51,7 +47,7 @@ public class Routines { int j = 0; for (int i = 0; i < lines.size(); i++) { File file = new File(lines.get(i)); - if (type == "directory" && file.isDirectory() || type == "file" && file.isFile()) { + if (type == "dir" && file.isDirectory() || type == "file" && file.isFile()) { filepool.put(j, file); j++; } @@ -59,7 +55,12 @@ public class Routines { return filepool; } - + /** + * Calculate md5 for each file in pool . + * + * @param pool Map< Integer, File /> + * @return Map< Integer , MFile /> + */ public Map md5Pool(Map pool) { Map md5Pool = new HashMap<>(); for (int i = 0; i < pool.size(); i++) { @@ -69,7 +70,10 @@ public class Routines { return md5Pool; } - + /** + * @param md5Pool Map< Integer, MFile /> , a map containing files and their md5. + * @return Map< Integer , MFile /> of duplicates contained in md5Pool + */ public Map doubles(Map md5Pool) { Map doubles = new HashMap<>(); int d = 0; diff --git a/src/app/Tools.java b/src/app/Tools.java index 313b618..d9b076d 100644 --- a/src/app/Tools.java +++ b/src/app/Tools.java @@ -7,23 +7,24 @@ public class Tools { Execute x; - public Tools() { - x = new Execute(); - } + public Tools() { + x = new Execute(); + } - /** - * @return Md5 of File at @param path - */ - public String getMd5(String path) { - // output of md5sum: "md5 filepath" - BufferedReader md5reader = x.execute(new String[] { "md5sum", path }).output; - String md5 = null; - try { - md5 = md5reader.readLine().split(" ")[0]; - } catch (IOException e) { - e.printStackTrace(); - } - return md5; - } + /** + * @param path String + * @return Md5 of File at path + */ + public String getMd5(String path) { + // output of md5sum is "md5 filepath" + BufferedReader md5reader = x.execute(new String[]{"md5sum", path}).output; + String md5 = null; + try { + md5 = md5reader.readLine().split(" ")[0]; + } catch (IOException e) { + e.printStackTrace(); + } + return md5; + } } diff --git a/src/app/Write.java b/src/app/Write.java index b5c8e4d..1ac81cc 100644 --- a/src/app/Write.java +++ b/src/app/Write.java @@ -4,53 +4,56 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Map; + import app.Artifacts.MFile; public class Write { /** - * writes text to file at path - *

- */ - public void textFile(String path, StringBuilder text) { - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path))); - bw.write(text.toString()); - bw.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - - /** - * append all elements of pool to StringBuilder and write to - * /tmp/name - *

- * - * @param pool - */ - public void textPool(String name, Map pool) { - StringBuilder text = new StringBuilder(); - for (int i = 0; i < pool.size(); i++) { - text.append(i + " " + pool.get(i) + "\n"); - } - textFile("/tmp/" + name, text); - } - - - /** - * append all elements of md5Pool to StringBuilder and write to - * /tmp/name - *

- * - * @param md5Pool - */ - public void textMd5Pool(String name, Map md5Pool) { - StringBuilder text = new StringBuilder(); - for (int i = 0; i < md5Pool.size(); i++) { - text.append(i + " " + md5Pool.get(i).md5 + " " + md5Pool.get(i).file + "\n"); - } - textFile("/tmp/" + name, text); - } + * Write text to file at path . + * + * @param path String + * @param text StringBuilder + */ + public void textFile(String path, StringBuilder text) { + try { + BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path))); + bw.write(text.toString()); + bw.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * Append all elements of pool to StringBuilder + * and write to /tmp/name . + * + * @param name String + * @param pool Map< Integer, File /> + */ + public void textPool(String name, Map pool) { + StringBuilder text = new StringBuilder(); + for (int i = 0; i < pool.size(); i++) { + text.append(i + " " + pool.get(i) + "\n"); + } + textFile("/tmp/" + name, text); + } + + + /** + * Append all elements of pool to StringBuilder + * and write to /tmp/name . + * + * @param name String + * @param md5Pool Map< Integer, MFile /> + */ + public void textMd5Pool(String name, Map md5Pool) { + StringBuilder text = new StringBuilder(); + for (int i = 0; i < md5Pool.size(); i++) { + text.append(i + " " + md5Pool.get(i).md5 + " " + md5Pool.get(i).file + "\n"); + } + textFile("/tmp/" + name, text); + } }