aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/util/Scheduler.java
blob: aa45f1b26113d2ab06f84ea8da0f69112fda9ba1 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.xtremelabs.robolectric.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class Scheduler {
    private List<PostedRunnable> postedRunnables = new ArrayList<PostedRunnable>();
    private long currentTime = 0;
    private boolean paused = false;

    public long getCurrentTime() {
        return currentTime;
    }

    public void pause() {
        paused = true;
    }

    public void unPause() {
        paused = false;
        advanceToLastPostedRunnable();
    }

    public boolean isPaused() {
        return paused;
    }

    public void postDelayed(Runnable runnable, long delayMillis) {
        if (paused || delayMillis > 0) {
            postedRunnables.add(new PostedRunnable(runnable, currentTime + delayMillis));
            Collections.sort(postedRunnables);
        } else {
            runnable.run();
        }
    }

    public void post(Runnable runnable) {
        postDelayed(runnable, 0);
    }

    public void postAtFrontOfQueue(Runnable runnable) {
        if (paused) {
            postedRunnables.add(0, new PostedRunnable(runnable, currentTime));
        } else {
            runnable.run();
        }
    }

    public void remove(Runnable runnable) {
        ListIterator<PostedRunnable> iterator = postedRunnables.listIterator();
        while (iterator.hasNext()) {
            PostedRunnable next = iterator.next();
            if (next.runnable == runnable) {
                iterator.remove();
            }
        }
    }

    public boolean advanceToLastPostedRunnable() {
        if (enqueuedTaskCount() < 1) {
            return false;
        }

        return advanceTo(postedRunnables.get(postedRunnables.size() - 1).scheduledTime);
    }

    public boolean advanceToNextPostedRunnable() {
        if (enqueuedTaskCount() < 1) {
            return false;
        }

        return advanceTo(postedRunnables.get(0).scheduledTime);
    }

    public boolean advanceBy(long intervalMs) {
        long endingTime = currentTime + intervalMs;
        return advanceTo(endingTime);
    }

    public boolean advanceTo(long endingTime) {
        if (endingTime - currentTime < 0 || enqueuedTaskCount() < 1) {
            return false;
        }

        int runCount = 0;
        while (nextTaskIsScheduledBefore(endingTime)) {
            runOneTask();
            ++runCount;
        }
        currentTime = endingTime;

        return runCount > 0;
    }

    public boolean runOneTask() {
        if (enqueuedTaskCount() < 1) {
            return false;
        }

        PostedRunnable postedRunnable = postedRunnables.remove(0);
        currentTime = postedRunnable.scheduledTime;
        postedRunnable.run();
        return true;
    }

    public boolean runTasks(int howMany) {
        if (enqueuedTaskCount() < howMany) {
            return false;
        }

        while (howMany > 0) {
            PostedRunnable postedRunnable = postedRunnables.remove(0);
            currentTime = postedRunnable.scheduledTime;
            postedRunnable.run();
            howMany--;
        }
        return true;
    }

    public int enqueuedTaskCount() {
        return postedRunnables.size();
    }

    public boolean areAnyRunnable() {
        return nextTaskIsScheduledBefore(currentTime);
    }

    public void reset() {
        postedRunnables.clear();
        paused = false;
    }

    public int size() {
        return postedRunnables.size();
    }

    class PostedRunnable implements Comparable<PostedRunnable> {
        Runnable runnable;
        long scheduledTime;

        PostedRunnable(Runnable runnable, long scheduledTime) {
            this.runnable = runnable;
            this.scheduledTime = scheduledTime;
        }

        @Override
        public int compareTo(PostedRunnable postedRunnable) {
            return (int) (scheduledTime - postedRunnable.scheduledTime);
        }

        public void run() {
            runnable.run();
        }
    }

    private boolean nextTaskIsScheduledBefore(long endingTime) {
        return enqueuedTaskCount() > 0 && postedRunnables.get(0).scheduledTime <= endingTime;
    }
}