1
1
package tasks ;
2
2
3
+ import tasks .break_task .BreakTask ;
3
4
import tasks .task_executor .TaskChangeListener ;
4
5
import tasks .task_executor .TaskExecutor ;
5
6
import util .Copyable ;
6
7
7
8
import java .time .LocalDateTime ;
8
- import java .util .List ;
9
+ import java .util .*;
10
+ import java .util .function .Predicate ;
9
11
import java .util .stream .Collectors ;
10
12
11
13
public class LoopTask extends Task {
12
14
13
15
public static final int INFINITE_ITERATIONS = -1 ;
14
16
15
- private int taskCount ;
17
+ private final int taskCount ;
18
+ private final boolean randomize ;
16
19
17
20
private int numIterations ;
18
21
private long startTimeMS = -1 , durationMS = -1 ;
@@ -26,27 +29,26 @@ public class LoopTask extends Task {
26
29
27
30
private List <Task > loopTasks ;
28
31
29
- private LoopTask () {
32
+ private LoopTask (final int taskCount , final boolean randomize ) {
30
33
super (TaskType .LOOP );
34
+ this .taskCount = taskCount ;
35
+ this .randomize = randomize ;
31
36
}
32
37
33
- public static LoopTask forIterations (final int taskCount , final int numIterations ) {
34
- LoopTask loopTask = new LoopTask ();
35
- loopTask .taskCount = taskCount ;
38
+ public static LoopTask forIterations (final int taskCount , final boolean randomize , final int numIterations ) {
39
+ LoopTask loopTask = new LoopTask (taskCount , randomize );
36
40
loopTask .numIterations = numIterations ;
37
41
return loopTask ;
38
42
}
39
43
40
- public static LoopTask forDuration (final int taskCount , final long durationMS ) {
41
- LoopTask loopTask = new LoopTask ();
42
- loopTask .taskCount = taskCount ;
44
+ public static LoopTask forDuration (final int taskCount , final boolean randomize , final long durationMS ) {
45
+ LoopTask loopTask = new LoopTask (taskCount , randomize );
43
46
loopTask .durationMS = durationMS ;
44
47
return loopTask ;
45
48
}
46
49
47
- public static LoopTask untilDateTime (final int taskCount , final LocalDateTime endDateTime ) {
48
- LoopTask loopTask = new LoopTask ();
49
- loopTask .taskCount = taskCount ;
50
+ public static LoopTask untilDateTime (final int taskCount , final boolean randomize , final LocalDateTime endDateTime ) {
51
+ LoopTask loopTask = new LoopTask (taskCount , randomize );
50
52
loopTask .endDateTime = endDateTime ;
51
53
return loopTask ;
52
54
}
@@ -57,6 +59,12 @@ public void setup(final List<Task> allTasks,
57
59
this .startTaskIndex = this .endTaskIndex - taskCount ;
58
60
59
61
loopTasks = getLoopTasks (allTasks );
62
+
63
+ if (randomize ) {
64
+ randomizeTasks (loopTasks );
65
+ log ("Randomized tasks" );
66
+ }
67
+
60
68
taskExecutor = new TaskExecutor (loopTasks );
61
69
taskExecutor .addTaskChangeListeners (taskChangeListeners );
62
70
}
@@ -108,6 +116,36 @@ private List<Task> copyTasks(final List<Task> tasks) {
108
116
.collect (Collectors .toList ());
109
117
}
110
118
119
+ private void randomizeTasks (final List <Task > tasks ) {
120
+ // Randomly shuffle any tasks that are not break tasks
121
+ randomizeTasks (tasks , task -> !(task instanceof BreakTask ));
122
+
123
+ // Then randomly shuffle all break tasks
124
+ randomizeTasks (tasks , task -> task instanceof BreakTask );
125
+ }
126
+
127
+ /**
128
+ * Randomizes the order of tasks in a list of tasks
129
+ * Only tasks that match the taskPredicate will be re-ordered
130
+ * @param tasks List if tasks to be randomized
131
+ * @param taskPredicate A task predicate to determine candidates for re-ordering
132
+ */
133
+ private void randomizeTasks (final List <Task > tasks , final Predicate <Task > taskPredicate ) {
134
+ List <Task > breakTasks = tasks .stream ()
135
+ .filter (taskPredicate )
136
+ .collect (Collectors .toList ());
137
+
138
+ Collections .shuffle (breakTasks );
139
+
140
+ Queue <Task > randomBreakTaskQueue = new LinkedList <>(breakTasks );
141
+
142
+ for (int i = 0 ; i < tasks .size (); i ++) {
143
+ if (taskPredicate .test (tasks .get (i ))) {
144
+ tasks .set (i , randomBreakTaskQueue .poll ());
145
+ }
146
+ }
147
+ }
148
+
111
149
@ Override
112
150
public boolean canExit () {
113
151
return true ;
@@ -116,11 +154,11 @@ public boolean canExit() {
116
154
@ Override
117
155
public Task copy () {
118
156
if (endDateTime != null ) {
119
- return LoopTask .untilDateTime (taskCount , endDateTime );
157
+ return LoopTask .untilDateTime (taskCount , randomize , endDateTime );
120
158
} else if (durationMS != -1 ) {
121
- return LoopTask .forDuration (taskCount , durationMS );
159
+ return LoopTask .forDuration (taskCount , randomize , durationMS );
122
160
}
123
- return LoopTask .forIterations (taskCount , numIterations );
161
+ return LoopTask .forIterations (taskCount , randomize , numIterations );
124
162
}
125
163
126
164
@ Override
0 commit comments