-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeTracker.java
99 lines (79 loc) · 3.2 KB
/
TimeTracker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Programmer: Dan Hopp
* Date: 27-FEB-2020
* Description: An object with functions to:
See if the target end time has been reached.
Calcualte 30 second intervals from the user's requested run time. (To alter
the interval, change the final variable.)
Give feedback to the user for when an interval has passed
(30, 60, 90, etc...).
*/
package lab3;
import java.util.ArrayList;
public class TimeTracker {
private final int TIME_INTERVAL = 30; //in seconds
private final long SEC_TO_MS_CONVERSION = 1000;
private boolean stopCrawl;
private ArrayList<Integer> messageIntervals;
private int arrayIndex;
private long startTime;
private long stopTime;
private int runTime;
//Don't use the default constructor for this program
public TimeTracker(){
}
/*TimeTracker constructor*/
public TimeTracker(int runTime){
this.runTime = runTime;
//Calculate start and stop times
startTime = System.currentTimeMillis();
this.stopTime = startTime + (long)(this.runTime * SEC_TO_MS_CONVERSION);
//Populate message interval array
messageIntervals = new ArrayList<>();
createTimeIntervals();
arrayIndex = 0;
//Set the webcrawl's toggle
stopCrawl = false;
}
/*Function to see if the stop time has been reached, and to give the user
feedback as the time elapses. Boolean pass-by-value is an option to call the
feedback message function, specifically to prevent spam for when the function
is called from within a rapid loop*/
public boolean isStoptimeReached(boolean callMessageFunction){
//Current time is < the calculated end time from the user's entry ((secondsTOmilliseconds) + current time)
if (System.currentTimeMillis() > stopTime){
stopCrawl = true;
}
if (callMessageFunction == true) {
timeIntervalMessage();
}
return stopCrawl;
}
/*If a time interval has been reached, inform the user*/
private void timeIntervalMessage(){
if (((System.currentTimeMillis() - startTime) / SEC_TO_MS_CONVERSION)
> messageIntervals.get(arrayIndex)){
System.out.println("Crawl time at " + messageIntervals.get(arrayIndex)
+ " seconds...");
//Move to the next interval
arrayIndex = arrayIndex + 1;
}
}
/*Create an array list with the interval's elements, in 30 second blocks*/
private void createTimeIntervals(){
int temp = 0;
int numberOfIntervals = runTime / TIME_INTERVAL;
//If the user's input is equal to or greater than the TIME_INTERVAL
if (numberOfIntervals > 0) {
//Populate array with values
for (int i = 0; i < numberOfIntervals; i++) {
temp = temp + TIME_INTERVAL;
messageIntervals.add(temp);
}
}
//Else just add one item
else {
messageIntervals.add(TIME_INTERVAL);
}
}
}