AsyncServerTask.java 2.41 KB
Newer Older
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
package emu.grasscutter.server.scheduler;

import lombok.Getter;

import javax.annotation.Nullable;

/**
 * A server task that should be run asynchronously.
 */
public final class AsyncServerTask implements Runnable {
    /* The runnable to run. */
    private final Runnable task;
    /* This ID is assigned by the scheduler. */
    @Getter private final int taskId;
    /* The result callback to run. */
    @Nullable private final Runnable callback;

    /* Has the task already been started? */
    private boolean started = false;
    /* Has the task finished execution? */
    private boolean finished = false;
    /* The result produced in the async task. */
    @Nullable private Object result = null;

    /**
     * For tasks without a callback.
     * @param task The task to run.
     */
    public AsyncServerTask(Runnable task, int taskId) {
        this(task, null, taskId);
    }

    /**
     * For tasks with a callback.
     * @param task The task to run.
     * @param callback The task to run after the task is complete.
     */
    public AsyncServerTask(Runnable task, @Nullable Runnable callback, int taskId) {
        this.task = task;
        this.callback = callback;
        this.taskId = taskId;
    }

    /**
     * Returns the state of the task.
     * @return True if the task has been started, false otherwise.
     */
    public boolean hasStarted() {
        return this.started;
    }

    /**
     * Returns the state of the task.
     * @return True if the task has finished execution, false otherwise.
     */
    public boolean isFinished() {
        return this.finished;
    }

    /**
     * Runs the task.
     */
    @Override public void run() {
        // Declare the task as started.
        this.started = true;

        // Run the runnable.
        this.task.run();

        // Declare the task as finished.
        this.finished = true;
    }

    /**
     * Runs the callback.
     */
    public void complete() {
        // Run the callback.
        if(this.callback != null)
            this.callback.run();
    }

    /**
     * Sets the result of the async task.
     * @param result The result of the async task.
     */
    public void setResult(@Nullable Object result) {
        this.result = result;
    }

    /**
     * Returns the set result of the async task.
     * @return The result, or null if it has not been set.
     */
    @Nullable public Object getResult() {
        return this.result;
    }
}