RuneHive-Game
Loading...
Searching...
No Matches
Task.java
Go to the documentation of this file.
1package com.runehive.game.task;
2
3import java.util.Optional;
4import java.util.UUID;
5
6/**
7 * A game representing a cyclic unit of work.
8 *
9 * @author lare96 <http://github.org/lare96>
10 */
11public abstract class Task {
12
13 /** If execution happens instantly upon being scheduled. */
14 private final boolean instant;
15
16 /** The cyclic delay. */
17 private int delay;
18
19 /** If registration has taken place. */
20 private boolean running;
21
22 /** A counter that determines when execution should take place. */
23 private int counter;
24
25 /** An optional attachment. */
26 private Optional<Object> key = Optional.empty();
27
28 /** The creation stack trace because jire likes to guess */
29 private StackTraceElement[] creationStackTrace;
30
31 /** The time the task started task.run(), if null the task has not ran yet **/
32 private Optional<Long> runStartTime = Optional.empty();
33
34 /** Unique task UUID which helps us identify if any tasks will lock up **/
35 private final UUID uuid;
36
37 /** The time the task was created **/
38 private final long taskCreationTime;
39
40 /** Creates a new {@link Task}. */
41 public Task(boolean instant, int delay) {
42 if (delay <= 0) {
43 instant = true;
44 delay = 1;
45 }
46 this.instant = instant;
47 this.delay = delay;
48 this.creationStackTrace = Thread.currentThread().getStackTrace();
49 this.uuid = UUID.randomUUID();
50 this.taskCreationTime = System.currentTimeMillis();
51 }
52
53 public String getTaskId() {
54 return uuid.toString();
55 }
56
57 /** Creates a new {@link Task} that doesn't execute instantly. */
58 public Task(int delay) {
59 this(false, delay);
60 }
61
62 public void setExecutionTime() {
63 runStartTime = Optional.of(System.currentTimeMillis());
64 }
65
66 public long getTaskCreationTime() {
67 return taskCreationTime;
68 }
69
70 public Optional<Long> getRunStartTime() {
71 return runStartTime;
72 }
73
75 if (runStartTime.isPresent()) {
76 return System.currentTimeMillis() - runStartTime.get();
77 }
78 return null;
79 }
80
81 public String getCreationStackTraceStr() {
82 StringBuilder sb = new StringBuilder();
83 for (StackTraceElement ste : creationStackTrace) {
84 sb.append(ste.toString()).append("\n");
85 }
86 return sb.toString();
87 }
88
89 final synchronized void process() {
90 if (++counter >= delay && running) {
92 counter = 0;
93 }
94 }
95
96 /***
97 * Executes the abstract execute method ensuring any methods are called before and after
98 */
99 protected void baseExecute() {
101 execute();
102 }
103
104 /** A function representing the unit of work that will be carried out. */
105 protected abstract void execute();
106
107 /** Determines if the task can be ran. */
108 public boolean canRun() {
109 return true;
110 }
111
112 /** Cancels all subsequent executions. Does nothing if already cancelled. */
113 public synchronized final void cancel() {
114 if (running) {
115 onCancel(false);
116 running = false;
117 }
118 }
119
120 /** Cancels all subsequent executions. Does nothing if already cancelled. */
121 public synchronized final void cancel(boolean logout) {
122 if (running) {
123 onCancel(logout);
124 running = false;
125 }
126 }
127
128 /** A function executed when iterated over. */
129 void onLoop() { }
130
131 /** A function executed on registration. */
132 protected void onSchedule() { }
133
134 /** A function executed on registration. */
135 protected boolean canSchedule() {
136 return true;
137 }
138
139 /** A function executed on cancellation. */
140 protected void onCancel(boolean logout) { }
141
142 /** A function executed on thrown exceptions. */
143 void onException(Exception e) {
144
145 }
146
147 /** Attaches a new key. */
148 public Task attach(Object newKey) {
149 key = Optional.ofNullable(newKey);
150 return this;
151 }
152
153 /** @return {@code true} if execution happens instantly upon being scheduled. */
154 public boolean isInstant() {
155 return instant;
156 }
157
158 /** @return The cyclic delay. */
159 public int getDelay() {
160 return delay;
161 }
162
163 /** Sets the cyclic delay. */
164 public void setDelay(int delay) {
165 this.delay = delay;
166 }
167
168 /** @return {@code true} if registration has taken place. */
169 public boolean isRunning() {
170 return running;
171 }
172
173 /** @return An optional attachment. */
174 public Optional<Object> getAttachment() {
175 return key;
176 }
177
178 synchronized void setRunning(boolean running) {
179 this.running = running;
180 }
181}
final long taskCreationTime
The time the task was created.
Definition Task.java:38
StackTraceElement[] creationStackTrace
The creation stack trace because jire likes to guess.
Definition Task.java:29
final synchronized void process()
Definition Task.java:89
synchronized final void cancel()
Cancels all subsequent executions.
Definition Task.java:113
int counter
A counter that determines when execution should take place.
Definition Task.java:23
synchronized void setRunning(boolean running)
Definition Task.java:178
String getCreationStackTraceStr()
Definition Task.java:81
boolean running
If registration has taken place.
Definition Task.java:20
boolean canRun()
Determines if the task can be ran.
Definition Task.java:108
Optional< Long > getRunStartTime()
Definition Task.java:70
Long getElapsedTimeFromRunStartTime()
Definition Task.java:74
abstract void execute()
A function representing the unit of work that will be carried out.
Task attach(Object newKey)
Attaches a new key.
Definition Task.java:148
Optional< Long > runStartTime
The time the task started task.run(), if null the task has not ran yet.
Definition Task.java:32
Task(boolean instant, int delay)
Creates a new Task.
Definition Task.java:41
void setDelay(int delay)
Sets the cyclic delay.
Definition Task.java:164
void onSchedule()
A function executed on registration.
Definition Task.java:132
Optional< Object > getAttachment()
Definition Task.java:174
boolean canSchedule()
A function executed on registration.
Definition Task.java:135
synchronized final void cancel(boolean logout)
Cancels all subsequent executions.
Definition Task.java:121
void onException(Exception e)
A function executed on thrown exceptions.
Definition Task.java:143
final boolean instant
If execution happens instantly upon being scheduled.
Definition Task.java:14
final UUID uuid
Unique task UUID which helps us identify if any tasks will lock up.
Definition Task.java:35
void onLoop()
A function executed when iterated over.
Definition Task.java:129
void onCancel(boolean logout)
A function executed on cancellation.
Definition Task.java:140
Task(int delay)
Creates a new Task that doesn't execute instantly.
Definition Task.java:58
int delay
The cyclic delay.
Definition Task.java:17
Optional< Object > key
An optional attachment.
Definition Task.java:26