Format and minor docs.

master
Ivan Olexyn 6 years ago
parent 7b018185c8
commit d43e79babd

@ -6,16 +6,20 @@ public class Artifacts {
Tools tools = new Tools(); Tools tools = new Tools();
/**
* @param file
* @return
*/
public MFile getMFile(File file) { public MFile getMFile(File file) {
MFile mfile = new MFile(); MFile mfile = new MFile();
mfile.file = file; mfile.file = file;
mfile.md5 = tools.getMd5(file.getPath()); mfile.md5 = tools.getMd5(file.getPath());
return mfile; return mfile;
} }
/**
*
*/
public class MFile { public class MFile {
public File file; public File file;
public String md5; public String md5;

@ -16,6 +16,9 @@ import java.util.Map;
import app.Artifacts.MFile; import app.Artifacts.MFile;
/***
* Controller class for JavaFX. Contains the application logic.
*/
public class Controller { public class Controller {

@ -7,8 +7,7 @@ public class Execute {
/** /**
* * @param cmd an array representing a shell command
* @param cmd an array representing a shell command
* @return <i>TwoBr</i> class, containing two BufferedReaders, * @return <i>TwoBr</i> class, containing two BufferedReaders,
* <i>output</i> and <i>error</i> * <i>output</i> and <i>error</i>
* @see <i>output</i> BufferedReader, corresponds to STDOUT * @see <i>output</i> BufferedReader, corresponds to STDOUT

@ -13,10 +13,9 @@ import javafx.stage.Stage;
public class Main extends Application { public class Main extends Application {
static String foo;
@Override @Override
public void start(Stage primaryStage) throws Exception{ public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("layout.fxml")); Parent root = FXMLLoader.load(getClass().getResource("layout.fxml"));
Scene scene = new Scene(root, 300, 275); Scene scene = new Scene(root, 300, 275);

@ -5,72 +5,76 @@ import app.Artifacts.MFile;
import java.util.Map; import java.util.Map;
public class QuicksortMd5 { public class QuicksortMd5 {
private Map<Integer, MFile> md5Pool; private Map<Integer, MFile> md5Pool;
private int i; private int i;
private int j; private int j;
private int p; private int p;
public Map<Integer, MFile> quicksortMd5(Map<Integer, MFile> md5Pool) { /**
this.md5Pool = md5Pool; * @param md5Pool
* @return a Map sorted by md5.
quicksort(0, md5Pool.size() - 1); */
return this.md5Pool; public Map<Integer, MFile> quicksortMd5(Map<Integer, MFile> md5Pool) {
} this.md5Pool = md5Pool;
private void quicksort(int low, int high) { quicksort(0, md5Pool.size() - 1);
i = low; return this.md5Pool;
j = high; }
p = low + (high - low) / 2;
private void quicksort(int low, int high) {
while (i < j) { i = low;
j = high;
// a < b a.compareto(b) = -1 p = low + (high - low) / 2;
// a = b a.compareto(b) = 0
// a > b a.compareto(b) = 1 while (i < j) {
while (i < p) { // a < b a.compareto(b) = -1
if (imd5().compareTo(pmd5()) <= 0) { // a = b a.compareto(b) = 0
i++; // a > b a.compareto(b) = 1
} else {
swap(i, p); while (i < p) {
j = high; if (imd5().compareTo(pmd5()) <= 0) {
} i++;
} } else {
swap(i, p);
while (p < j) { j = high;
if (jmd5().compareTo(pmd5()) >= 0) { }
j--; }
} else {
swap(p, j); while (p < j) {
i = low; 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); if (high - p > 2) {
} quicksort(p, high);
} }
if (p - low > 2) {
private void swap(int a, int b) { quicksort(low, p);
MFile temp = md5Pool.get(a); }
md5Pool.put(a, md5Pool.get(b)); }
md5Pool.put(b, temp);
} private void swap(int a, int b) {
MFile temp = md5Pool.get(a);
private String imd5() { md5Pool.put(a, md5Pool.get(b));
return md5Pool.get(i).md5; md5Pool.put(b, temp);
} }
private String jmd5() { private String imd5() {
return md5Pool.get(j).md5; return md5Pool.get(i).md5;
} }
private String pmd5() { private String jmd5() {
return md5Pool.get(p).md5; return md5Pool.get(j).md5;
} }
private String pmd5() {
return md5Pool.get(p).md5;
}
} }

@ -12,28 +12,24 @@ import app.Artifacts.MFile;
public class Routines { public class Routines {
Execute x;
Execute x = null; Tools tools;
Tools tools = new Tools();
Write write = new Write();
public Routines() { public Routines() {
this.x = new Execute(); this.x = new Execute();
this.tools = new Tools(); this.tools = new Tools();
} }
/** /**
* [1] Write output of <b>find srcdir</b> to <b>/tmp/find</b> <br> * [1] Write output of <b>find srcdir</b> to <b>/tmp/find</b> .<br>
* [2] Read <b>/tmp/find</b> into <b>List>String></b> <br> * [2] Read <b>/tmp/find</b> into <b>List< String /></b> .<br>
* [3] Add <b>List>String></b> entries to <b>Map>String,File></b> , where * [3] Add <b>List< String /></b> entries to <b>Map>String,File></b> , where
* <b>String</b> is an <b>int</b> key. <br> * <b>String</b> is an <b>int</b> key. <br>
* *
* @param srcdir * @param srcdir <i>String</i>
* @param type file OR directory * @param type <i>String</i> "file" OR "dir" , pick what type will be loaded.
* @return filepool * @return filepool
*/ */
public Map<Integer, File> loadPool(String srcdir, String type) { public Map<Integer, File> loadPool(String srcdir, String type) {
@ -51,7 +47,7 @@ public class Routines {
int j = 0; int j = 0;
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
File file = new File(lines.get(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); filepool.put(j, file);
j++; j++;
} }
@ -59,7 +55,12 @@ public class Routines {
return filepool; return filepool;
} }
/**
* Calculate md5 for each file in <b>pool</b> .
*
* @param pool Map< Integer, File />
* @return Map< Integer , MFile />
*/
public Map<Integer, MFile> md5Pool(Map<Integer, File> pool) { public Map<Integer, MFile> md5Pool(Map<Integer, File> pool) {
Map<Integer, MFile> md5Pool = new HashMap<>(); Map<Integer, MFile> md5Pool = new HashMap<>();
for (int i = 0; i < pool.size(); i++) { for (int i = 0; i < pool.size(); i++) {
@ -69,7 +70,10 @@ public class Routines {
return md5Pool; return md5Pool;
} }
/**
* @param md5Pool Map< Integer, MFile /> , a map containing files and their md5.
* @return Map< Integer , MFile /> of duplicates contained in <b>md5Pool</b>
*/
public Map<Integer, MFile> doubles(Map<Integer, MFile> md5Pool) { public Map<Integer, MFile> doubles(Map<Integer, MFile> md5Pool) {
Map<Integer, MFile> doubles = new HashMap<>(); Map<Integer, MFile> doubles = new HashMap<>();
int d = 0; int d = 0;

@ -7,23 +7,24 @@ public class Tools {
Execute x; Execute x;
public Tools() { public Tools() {
x = new Execute(); x = new Execute();
} }
/** /**
* @return Md5 of File at @param path * @param path <i>String</i>
*/ * @return Md5 of File at <b>path</b>
public String getMd5(String path) { */
// output of md5sum: "md5 filepath" public String getMd5(String path) {
BufferedReader md5reader = x.execute(new String[] { "md5sum", path }).output; // output of md5sum is "md5 filepath"
String md5 = null; BufferedReader md5reader = x.execute(new String[]{"md5sum", path}).output;
try { String md5 = null;
md5 = md5reader.readLine().split(" ")[0]; try {
} catch (IOException e) { md5 = md5reader.readLine().split(" ")[0];
e.printStackTrace(); } catch (IOException e) {
} e.printStackTrace();
return md5; }
} return md5;
}
} }

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

Loading…
Cancel
Save