Skip to content

Commit cc8c953

Browse files
committed
shortcut functions
1 parent c32dba7 commit cc8c953

File tree

4 files changed

+85
-60
lines changed

4 files changed

+85
-60
lines changed

src/main/java/fr/formiko/utils/FLUFiles.java

+79-55
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ private FLUFiles() {} // hide constructor
3434
// GET SET ----------------------------------------------------------------------
3535
public static FLUProgression getProgression() { return progression; }
3636
public static void setProgression(FLUProgression progression) { FLUFiles.progression = progression; }
37+
private static void setDownloadingValue(double value) {
38+
if (progression != null) {
39+
progression.setDownloadingValue(value);
40+
}
41+
}
42+
private static void setDownloadingMessage(String message) {
43+
if (progression != null) {
44+
progression.setDownloadingMessage(message);
45+
}
46+
}
47+
48+
// TODO add progressions to all functions that might be long opperation:
49+
// delete, copy, move, readFile, readFileAsList, readFileFromWeb, writeFile, appendToFile, listFiles, zip, unzip, download,
50+
// downloadAndUnzip, downloadAndRead, countEntryOfZipFile, getSize
3751

3852
public static boolean isAValidePath(String path) { return internal.isAValidePath(path); }
3953
public static boolean createFile(String path) { return internal.createFile(path); }
@@ -77,9 +91,19 @@ private FLUFilesInternal() {} // hide constructor
7791
private boolean createFile(String path) {
7892
if (isAValidePath(path)) {
7993
try {
94+
// int actionToDo = 3;
95+
// int currentAction = 0;
96+
// setDownloadingValue(currentAction++ / (double) actionToDo);
97+
// setDownloadingMessage("Creating file object for " + path);
8098
File file = new File(path);
99+
// setDownloadingValue(currentAction++ / (double) actionToDo);
100+
// setDownloadingMessage("Creating parents");
81101
createParents(file);
82-
return file.createNewFile();
102+
// setDownloadingValue(currentAction++ / (double) actionToDo);
103+
// setDownloadingMessage("Creating file");
104+
boolean r = file.createNewFile();
105+
// setDownloadingValue(currentAction++ / (double) actionToDo);
106+
return r;
83107
} catch (IOException e) {
84108
return false;
85109
}
@@ -349,64 +373,64 @@ private boolean download(String url, String destination) {
349373
return false;
350374
}
351375
}
352-
}
353-
354-
class FLUProgressionThread extends Thread {
355-
private File fileOut;
356-
private long fileToDowloadSize;
357-
private boolean running;
358-
private String downloadName;
359-
private FLUProgression progressionInstance;
360-
/**
361-
* {@summary Main constructor.}<br>
362-
*
363-
* @param fileOut file that we are curently filling by the downloading file
364-
* @param fileToDowloadSize size that we should reach when download will end
365-
*/
366-
public FLUProgressionThread(File fileOut, long fileToDowloadSize, String downloadName, FLUProgression progressionInstance) {
367-
this.fileOut = fileOut;
368-
this.fileToDowloadSize = fileToDowloadSize;
369-
this.downloadName = downloadName;
370-
this.progressionInstance = progressionInstance;
371-
running = true;
372-
}
376+
class FLUProgressionThread extends Thread {
377+
private File fileOut;
378+
private long fileToDowloadSize;
379+
private boolean running;
380+
private String downloadName;
381+
private FLUProgression progressionInstance;
382+
/**
383+
* {@summary Main constructor.}<br>
384+
*
385+
* @param fileOut file that we are curently filling by the downloading file
386+
* @param fileToDowloadSize size that we should reach when download will end
387+
*/
388+
public FLUProgressionThread(File fileOut, long fileToDowloadSize, String downloadName, FLUProgression progressionInstance) {
389+
this.fileOut = fileOut;
390+
this.fileToDowloadSize = fileToDowloadSize;
391+
this.downloadName = downloadName;
392+
this.progressionInstance = progressionInstance;
393+
running = true;
394+
}
373395

374-
public void stopRuning() { running = false; }
375-
/**
376-
* {@summary Main function that print every second %age of download done.}<br>
377-
*/
378-
public void run() {
379-
long fileOutSize = 0;
380-
long lastFileOutSize = 0;
381-
long timeStart = System.currentTimeMillis();
382-
long timeFromLastBitDownload = timeStart;
383-
while (fileOutSize < fileToDowloadSize && running) {
384-
fileOutSize = fileOut.length();
385-
double progression = ((double) fileOutSize) / (double) fileToDowloadSize;
386-
int percent = (int) (100 * progression);
387-
long curentTime = System.currentTimeMillis();
388-
long timeElapsed = curentTime - timeStart;
389-
long timeLeft = (long) ((double) ((timeElapsed / progression) - timeElapsed));
390-
String sTimeLeft = FLUTime.msToTime(timeLeft) + " left";
391-
String message = "Downloading " + downloadName + " - " + percent + "% - ";
392-
if (fileOutSize != lastFileOutSize) {// update watcher of working download
393-
timeFromLastBitDownload = curentTime;
394-
}
395-
if (timeFromLastBitDownload + 10000 < curentTime) {
396-
message += (((curentTime - timeFromLastBitDownload) / 1000) + "s untill a new bit haven't been download");
397-
if (timeFromLastBitDownload + 60000 < curentTime) {
398-
stopRuning();
399-
// TODO stop the IO opperation.
396+
public void stopRuning() { running = false; }
397+
/**
398+
* {@summary Main function that print every second %age of download done.}<br>
399+
*/
400+
public void run() {
401+
long fileOutSize = 0;
402+
long lastFileOutSize = 0;
403+
long timeStart = System.currentTimeMillis();
404+
long timeFromLastBitDownload = timeStart;
405+
while (fileOutSize < fileToDowloadSize && running) {
406+
fileOutSize = fileOut.length();
407+
double progression = ((double) fileOutSize) / (double) fileToDowloadSize;
408+
int percent = (int) (100 * progression);
409+
long curentTime = System.currentTimeMillis();
410+
long timeElapsed = curentTime - timeStart;
411+
long timeLeft = (long) ((double) ((timeElapsed / progression) - timeElapsed));
412+
String sTimeLeft = FLUTime.msToTime(timeLeft) + " left";
413+
String message = "Downloading " + downloadName + " - " + percent + "% - ";
414+
if (fileOutSize != lastFileOutSize) {// update watcher of working download
415+
timeFromLastBitDownload = curentTime;
400416
}
401-
} else {
402-
message += sTimeLeft;
403-
}
404-
progressionInstance.setDownloadingValue(percent);
405-
progressionInstance.setDownloadingMessage(message);
417+
if (timeFromLastBitDownload + 10000 < curentTime) {
418+
message += (((curentTime - timeFromLastBitDownload) / 1000) + "s untill a new bit haven't been download");
419+
if (timeFromLastBitDownload + 60000 < curentTime) {
420+
stopRuning();
421+
// TODO stop the IO opperation.
422+
}
423+
} else {
424+
message += sTimeLeft;
425+
}
426+
progressionInstance.setDownloadingValue(percent);
427+
progressionInstance.setDownloadingMessage(message);
406428

407-
lastFileOutSize = fileOutSize;
408-
FLUTime.sleep(50);
429+
lastFileOutSize = fileOutSize;
430+
FLUTime.sleep(50);
431+
}
409432
}
410433
}
411434
}
435+
412436
}

src/main/java/fr/formiko/utils/progressions/FLUProgression.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ default void setDownloadingMessage(String message) {}
1818
*
1919
* @param state the state as a %age
2020
*/
21-
default void setDownloadingValue(int state) {}
21+
default void setDownloadingValue(double state) {}
2222
/***
2323
* {@summary Initialize the game launcher.}
2424
*/

src/main/java/fr/formiko/utils/progressions/FLUProgressionCLI.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class FLUProgressionCLI implements FLUProgression {
99
@Override
1010
public void iniLauncher() { FLUFiles.setProgression(this); }
1111
@Override
12-
public void setDownloadingMessage(String message) { System.out.println("Dowload: " + message); }
12+
public void setDownloadingMessage(String message) { System.out.println(message); }
1313
@Override
14-
public void setDownloadingValue(int value) { System.out.println(value + "% done"); }
14+
public void setDownloadingValue(double value) { System.out.println(((int) (value * 100)) + "% done"); }
1515
}

src/test/java/fr/formiko/utils/FLUFilesTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.formiko.utils;
22

3+
import fr.formiko.utils.progressions.FLUProgressionCLI;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertNull;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -334,7 +335,7 @@ private static Stream<Arguments> testDownloadSource() {
334335
}
335336

336337
public static void main(String[] args) {
337-
new FLUFilesTest().testUnzip(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "",
338-
TEST_PATH_TEMPORARY + "existingDir/");
338+
FLUFiles.setProgression(new FLUProgressionCLI());
339+
FLUFiles.createFile(TEST_PATH_TEMPORARY + "/testCreateFiles1.txt");
339340
}
340341
}

0 commit comments

Comments
 (0)