Advertisement
Guest User

Task

a guest
Aug 28th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package com.runescape.task;
  2.  
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.ScheduledExecutorService;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. /**
  8.  * Represents a {@link Task} for the application.
  9.  *
  10.  * @author Braeden Dempsey
  11.  */
  12. public abstract class Task {
  13.  
  14.     /**
  15.      * Constructs a default {@link Task}.
  16.      * @param delay the interval between a cycle.
  17.      * @param unit the {@link java.util.concurrent.TimeUnit} of the {@link com.runescape.task.Task}.
  18.      */
  19.     public Task(final int delay, final TimeUnit unit) {
  20.         this.delay = delay;
  21.         this.unit = unit;
  22.     }
  23.  
  24.     /**
  25.      * Interval between cycles.
  26.      */
  27.     private final int delay;
  28.  
  29.     /**
  30.      * {@link TimeUnit} of the {@link Task}.
  31.      */
  32.     private final TimeUnit unit;
  33.  
  34.     /**
  35.      * {@link TaskState} of the {@link Task}.
  36.      */
  37.     private TaskState state;
  38.  
  39.     /**
  40.      * Scheduler of the {@link Task}.
  41.      */
  42.     private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
  43.  
  44.     /**
  45.      * Executes the {@link Task}.
  46.      */
  47.     protected abstract void execute();
  48.  
  49.     /**
  50.      * Pauses the {@link Task}.
  51.      */
  52.     public void pause() {
  53.         state = TaskState.PAUSED;
  54.     }
  55.  
  56.     /**
  57.      * Registers a {@link Task}.
  58.      */
  59.     public void initialize() {
  60.         if (!state.equals(TaskState.ACTIVE) || !service.isShutdown())
  61.             service.scheduleAtFixedRate(new TaskManager(), 0, delay, unit);
  62.         state = TaskState.ACTIVE;
  63.     }
  64.  
  65.     /**
  66.      * Remove a {@link Task}.
  67.      */
  68.     public void kill() {
  69.         state = TaskState.DEAD;
  70.         service.shutdown();
  71.     }
  72.  
  73.     /**
  74.      * Checks if the {@link TaskState} equals <code>PAUSED</code>.
  75.      * @return the <code>state</code>.
  76.      */
  77.     public boolean isPaused() {
  78.         return state.equals(TaskState.PAUSED);
  79.     }
  80.  
  81.     /**
  82.      * Checks if the {@link TaskState} equals <code>ACTIVE</code>.
  83.      * @return the <code>state</code>.
  84.      */
  85.     public boolean isActive() {
  86.         return state.equals(TaskState.ACTIVE);
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement