diff options
author | Senmori <thesenmori@gmail.com> | 2018-03-07 13:41:24 -0500 |
---|---|---|
committer | md_5 <git@md-5.net> | 2018-03-26 15:34:33 +1100 |
commit | 9f49ad60e7650d6e0b5ba11a6b9d465e64c1acc5 (patch) | |
tree | 985087b0582d59c0b91798146b9a8d00333bad7a /src/main/java | |
parent | aa255689c1e00b77b066440bbfd2d84b641bfd58 (diff) | |
download | craftbukkit-9f49ad60e7650d6e0b5ba11a6b9d465e64c1acc5.tar craftbukkit-9f49ad60e7650d6e0b5ba11a6b9d465e64c1acc5.tar.gz craftbukkit-9f49ad60e7650d6e0b5ba11a6b9d465e64c1acc5.tar.lz craftbukkit-9f49ad60e7650d6e0b5ba11a6b9d465e64c1acc5.tar.xz craftbukkit-9f49ad60e7650d6e0b5ba11a6b9d465e64c1acc5.zip |
Remove magic values in scheduler package
Diffstat (limited to 'src/main/java')
4 files changed, 55 insertions, 50 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java index 336519c6..fb50a4a7 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java @@ -27,7 +27,7 @@ class CraftAsyncTask extends CraftTask { public void run() { final Thread thread = Thread.currentThread(); synchronized(workers) { - if (getPeriod() == -2) { + if (getPeriod() == CraftTask.CANCEL) { // Never continue running after cancelled. // Checking this with the lock is important! return; @@ -99,7 +99,7 @@ class CraftAsyncTask extends CraftTask { boolean cancel0() { synchronized (workers) { // Synchronizing here prevents race condition for a completing task - setPeriod(-2l); + setPeriod(CraftTask.CANCEL); if (workers.isEmpty()) { runners.remove(getTaskId()); } diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftFuture.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftFuture.java index 191a1c13..bb990e51 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftFuture.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftFuture.java @@ -16,21 +16,21 @@ class CraftFuture<T> extends CraftTask implements Future<T> { private Exception exception = null; CraftFuture(final Callable<T> callable, final Plugin plugin, final int id) { - super(plugin, null, id, -1l); + super(plugin, null, id, CraftTask.NO_REPEATING); this.callable = callable; } public synchronized boolean cancel(final boolean mayInterruptIfRunning) { - if (getPeriod() != -1l) { + if (getPeriod() != CraftTask.NO_REPEATING) { return false; } - setPeriod(-2l); + setPeriod(CraftTask.CANCEL); return true; } public boolean isDone() { final long period = this.getPeriod(); - return period != -1l && period != -3l; + return period != CraftTask.NO_REPEATING && period != CraftTask.PROCESS_FOR_FUTURE; } public T get() throws CancellationException, InterruptedException, ExecutionException { @@ -44,13 +44,13 @@ class CraftFuture<T> extends CraftTask implements Future<T> { public synchronized T get(long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { timeout = unit.toMillis(timeout); long period = this.getPeriod(); - long timestamp = timeout > 0 ? System.currentTimeMillis() : 0l; + long timestamp = timeout > 0 ? System.currentTimeMillis() : 0L; while (true) { - if (period == -1l || period == -3l) { + if (period == CraftTask.NO_REPEATING || period == CraftTask.PROCESS_FOR_FUTURE) { this.wait(timeout); period = this.getPeriod(); - if (period == -1l || period == -3l) { - if (timeout == 0l) { + if (period == CraftTask.NO_REPEATING || period == CraftTask.PROCESS_FOR_FUTURE) { + if (timeout == 0L) { continue; } timeout += timestamp - (timestamp = System.currentTimeMillis()); @@ -60,26 +60,26 @@ class CraftFuture<T> extends CraftTask implements Future<T> { throw new TimeoutException(); } } - if (period == -2l) { + if (period == CraftTask.CANCEL) { throw new CancellationException(); } - if (period == -4l) { + if (period == CraftTask.DONE_FOR_FUTURE) { if (exception == null) { return value; } throw new ExecutionException(exception); } - throw new IllegalStateException("Expected " + -1l + " to " + -4l + ", got " + period); + throw new IllegalStateException("Expected " + CraftTask.NO_REPEATING + " to " + CraftTask.DONE_FOR_FUTURE + ", got " + period); } } @Override public void run() { synchronized (this) { - if (getPeriod() == -2l) { + if (getPeriod() == CraftTask.CANCEL) { return; } - setPeriod(-3l); + setPeriod(CraftTask.PROCESS_FOR_FUTURE); } try { value = callable.call(); @@ -87,17 +87,17 @@ class CraftFuture<T> extends CraftTask implements Future<T> { exception = e; } finally { synchronized (this) { - setPeriod(-4l); + setPeriod(CraftTask.DONE_FOR_FUTURE); this.notifyAll(); } } } synchronized boolean cancel0() { - if (getPeriod() != -1l) { + if (getPeriod() != CraftTask.NO_REPEATING) { return false; } - setPeriod(-2l); + setPeriod(CraftTask.CANCEL); notifyAll(); return true; } diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java index c5d751e9..96eff377 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java @@ -85,37 +85,37 @@ public class CraftScheduler implements BukkitScheduler { } public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task) { - return this.scheduleSyncDelayedTask(plugin, task, 0l); + return this.scheduleSyncDelayedTask(plugin, task, 0L); } public BukkitTask runTask(Plugin plugin, Runnable runnable) { - return runTaskLater(plugin, runnable, 0l); + return runTaskLater(plugin, runnable, 0L); } @Deprecated public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task) { - return this.scheduleAsyncDelayedTask(plugin, task, 0l); + return this.scheduleAsyncDelayedTask(plugin, task, 0L); } public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) { - return runTaskLaterAsynchronously(plugin, runnable, 0l); + return runTaskLaterAsynchronously(plugin, runnable, 0L); } public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { - return this.scheduleSyncRepeatingTask(plugin, task, delay, -1l); + return this.scheduleSyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING); } public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long delay) { - return runTaskTimer(plugin, runnable, delay, -1l); + return runTaskTimer(plugin, runnable, delay, CraftTask.NO_REPEATING); } @Deprecated public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { - return this.scheduleAsyncRepeatingTask(plugin, task, delay, -1l); + return this.scheduleAsyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING); } public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) { - return runTaskTimerAsynchronously(plugin, runnable, delay, -1l); + return runTaskTimerAsynchronously(plugin, runnable, delay, CraftTask.NO_REPEATING); } public int scheduleSyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) { @@ -124,13 +124,13 @@ public class CraftScheduler implements BukkitScheduler { public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) { validate(plugin, runnable); - if (delay < 0l) { + if (delay < 0L) { delay = 0; } - if (period == 0l) { - period = 1l; - } else if (period < -1l) { - period = -1l; + if (period == CraftTask.ERROR) { + period = 1L; + } else if (period < CraftTask.NO_REPEATING) { + period = CraftTask.NO_REPEATING; } return handle(new CraftTask(plugin, runnable, nextId(), period), delay); } @@ -142,13 +142,13 @@ public class CraftScheduler implements BukkitScheduler { public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) { validate(plugin, runnable); - if (delay < 0l) { + if (delay < 0L) { delay = 0; } - if (period == 0l) { - period = 1l; - } else if (period < -1l) { - period = -1l; + if (period == CraftTask.ERROR) { + period = 1L; + } else if (period < CraftTask.NO_REPEATING) { + period = CraftTask.NO_REPEATING; } return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay); } @@ -156,7 +156,7 @@ public class CraftScheduler implements BukkitScheduler { public <T> Future<T> callSyncMethod(final Plugin plugin, final Callable<T> task) { validate(plugin, task); final CraftFuture<T> future = new CraftFuture<T>(task, plugin, nextId()); - handle(future, 0l); + handle(future, 0L); return future; } @@ -190,7 +190,7 @@ public class CraftScheduler implements BukkitScheduler { } return false; }}); - handle(task, 0l); + handle(task, 0L); for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { if (taskPending == task) { return; @@ -223,7 +223,7 @@ public class CraftScheduler implements BukkitScheduler { } } }); - handle(task, 0l); + handle(task, 0L); for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { if (taskPending == task) { break; @@ -255,7 +255,7 @@ public class CraftScheduler implements BukkitScheduler { CraftScheduler.this.temp.clear(); } }); - handle(task, 0l); + handle(task, 0L); for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { if (taskPending == task) { break; @@ -284,11 +284,11 @@ public class CraftScheduler implements BukkitScheduler { } for (CraftTask task = head.getNext(); task != null; task = task.getNext()) { if (task.getTaskId() == taskId) { - return task.getPeriod() >= -1l; // The task will run + return task.getPeriod() >= CraftTask.NO_REPEATING; // The task will run } } CraftTask task = runners.get(taskId); - return task != null && task.getPeriod() >= -1l; + return task != null && task.getPeriod() >= CraftTask.NO_REPEATING; } public List<BukkitWorker> getActiveWorkers() { @@ -318,13 +318,13 @@ public class CraftScheduler implements BukkitScheduler { final ArrayList<BukkitTask> pending = new ArrayList<BukkitTask>(); for (CraftTask task : runners.values()) { - if (task.getPeriod() >= -1l) { + if (task.getPeriod() >= CraftTask.NO_REPEATING) { pending.add(task); } } for (final CraftTask task : truePending) { - if (task.getPeriod() >= -1l && !pending.contains(task)) { + if (task.getPeriod() >= CraftTask.NO_REPEATING && !pending.contains(task)) { pending.add(task); } } @@ -340,7 +340,7 @@ public class CraftScheduler implements BukkitScheduler { parsePending(); while (isReady(currentTick)) { final CraftTask task = pending.remove(); - if (task.getPeriod() < -1l) { + if (task.getPeriod() < CraftTask.NO_REPEATING) { if (task.isSync()) { runners.remove(task.getTaskId(), task); } @@ -413,7 +413,7 @@ public class CraftScheduler implements BukkitScheduler { for (; task != null; task = (lastTask = task).getNext()) { if (task.getTaskId() == -1) { task.run(); - } else if (task.getPeriod() >= -1l) { + } else if (task.getPeriod() >= CraftTask.NO_REPEATING) { pending.add(task); runners.put(task.getTaskId(), task); } diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java index 9cb666bb..a3ec2301 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java @@ -8,6 +8,11 @@ import org.bukkit.scheduler.BukkitTask; class CraftTask implements BukkitTask, Runnable { private volatile CraftTask next = null; + public static final int ERROR = 0; + public static final int NO_REPEATING = -1; + public static final int CANCEL = -2; + public static final int PROCESS_FOR_FUTURE = -3; + public static final int DONE_FOR_FUTURE = -4; /** * -1 means no repeating <br> * -2 means cancel <br> @@ -23,11 +28,11 @@ class CraftTask implements BukkitTask, Runnable { private final int id; CraftTask() { - this(null, null, -1, -1); + this(null, null, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING); } CraftTask(final Runnable task) { - this(null, task, -1, -1); + this(null, task, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING); } CraftTask(final Plugin plugin, final Runnable task, final int id, final long period) { @@ -83,7 +88,7 @@ class CraftTask implements BukkitTask, Runnable { @Override public boolean isCancelled() { - return (period == -2l); + return (period == CraftTask.CANCEL); } public void cancel() { @@ -96,7 +101,7 @@ class CraftTask implements BukkitTask, Runnable { * @return false if it is a craft future task that has already begun execution, true otherwise */ boolean cancel0() { - setPeriod(-2l); + setPeriod(CraftTask.CANCEL); return true; } } |