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();
/**
* @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;

@ -16,6 +16,9 @@ import java.util.Map;
import app.Artifacts.MFile;
/***
* Controller class for JavaFX. Contains the application logic.
*/
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,
* <i>output</i> and <i>error</i>
* @see <i>output</i> BufferedReader, corresponds to STDOUT

@ -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);

@ -5,72 +5,76 @@ import app.Artifacts.MFile;
import java.util.Map;
public class QuicksortMd5 {
private Map<Integer, MFile> md5Pool;
private int i;
private int j;
private int p;
public Map<Integer, MFile> quicksortMd5(Map<Integer, MFile> 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<Integer, MFile> md5Pool;
private int i;
private int j;
private int p;
/**
* @param md5Pool
* @return a Map sorted by md5.
*/
public Map<Integer, MFile> quicksortMd5(Map<Integer, MFile> 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;
}
}

@ -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 <b>find srcdir</b> to <b>/tmp/find</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
* [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>
* [3] Add <b>List< String /></b> entries to <b>Map>String,File></b> , where
* <b>String</b> is an <b>int</b> key. <br>
*
* @param srcdir
* @param type file OR directory
* @param srcdir <i>String</i>
* @param type <i>String</i> "file" OR "dir" , pick what type will be loaded.
* @return filepool
*/
public Map<Integer, File> 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 <b>pool</b> .
*
* @param pool Map< Integer, File />
* @return Map< Integer , MFile />
*/
public Map<Integer, MFile> md5Pool(Map<Integer, File> pool) {
Map<Integer, MFile> 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 <b>md5Pool</b>
*/
public Map<Integer, MFile> doubles(Map<Integer, MFile> md5Pool) {
Map<Integer, MFile> doubles = new HashMap<>();
int d = 0;

@ -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 <i>String</i>
* @return Md5 of File at <b>path</b>
*/
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;
}
}

@ -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 <b>text</b> to file at <b>path</b>
* <p>
*/
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 <b>pool</b> to StringBuilder and write to
* <b>/tmp/name</b>
* <p>
*
* @param pool
*/
public void textPool(String name, Map<Integer, File> 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 <b>md5Pool</b> to StringBuilder and write to
* <b>/tmp/name</b>
* <p>
*
* @param md5Pool
*/
public void textMd5Pool(String name, Map<Integer, MFile> 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 <b>text</b> to file at <b>path</b> .
*
* @param path <i>String</i>
* @param text <i>StringBuilder</i>
*/
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 <b>pool</b> to <i>StringBuilder</i>
* and write to /tmp/<b>name</b> .
*
* @param name <i>String</i>
* @param pool <i>Map< Integer, File /></i>
*/
public void textPool(String name, Map<Integer, File> 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 <b>pool</b> to <i>StringBuilder</i>
* and write to /tmp/<b>name</b> .
*
* @param name <i>String</i>
* @param md5Pool <i>Map< Integer, MFile /></i>
*/
public void textMd5Pool(String name, Map<Integer, MFile> 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);
}
}

Loading…
Cancel
Save