Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package com.nital.bundle;
  2.  
  3. /**
  4.  * A specialized version of an {@link Activator} which enables
  5.  * implementation of checkpoints which are defined through an {@link Enum}.
  6.  *
  7.  * <p><b>Usage</b>
  8.  * <p>If a developer created a new bundle which utilized a {@link StringBuilder}
  9.  * to create a new {@link String} (<b>happy</b>) in <tt>3</tt> stages on activation
  10.  * we could use a ReplayingActivator to separate those stages easily:
  11.  * <pre><code>public class Activator extends ReplayingActivator<Activator.Stage> {
  12.  *  
  13.  *  enum Stage {
  14.  *      STAGE_1,
  15.  *      STAGE_2,
  16.  *      STAGE_3;
  17.  *  }
  18.  *
  19.  *  &#064;Override
  20.  *  public void start(Stage stage) {
  21.  *      StringBuilder builder = new StringBuilder();
  22.  *      switch (stage) {
  23.  *      case STAGE_1:
  24.  *          builder.append("ha");
  25.  *          checkpoint(Stage.STAGE_2);
  26.  *          break;
  27.  *      case STAGE_2:
  28.  *          builder.append("p");
  29.  *          checkpoint(Stage.STAGE_3);
  30.  *          break;
  31.  *      case STAGE_3:
  32.  *          builder.append("py");
  33.  *          System.out.println(builder.toString());
  34.  *          break;
  35.  *      }
  36.  *  }
  37.  *
  38.  *  &#064;Override
  39.  *  public void stop(Stage stage) {
  40.  *      ...
  41.  *  }
  42.  *
  43.  * }</code></pre>
  44.  * </p>
  45.  *
  46.  * <b>Advantages</b>
  47.  * <ul><li>The activator implementation can be easily scaled to fit within
  48.  * a singular class.</li>
  49.  * <li>The utilization of checkpoints allows collegaue developers to follow
  50.  * the activation and deactivation of the bundle component thoroughly.</li></ul>
  51.  *
  52.  * @param T A ReplayingActivator implementation requires a generic (namely <b>T</b>),
  53.  * which must extend an {@link Enum}. This is used to define the checkpoint <i>stages</i>.
  54.  * @author Thomas Nappo
  55.  */
  56. public abstract class ReplayingActivator<T extends Enum<?>> implements Activator {
  57.  
  58.     /**
  59.      * Current replaying state of the activator.
  60.      */
  61.     private T state;
  62.  
  63.     /**
  64.      * Gets the current replaying state of the activator.
  65.      * @return The activator's {@link #state}.
  66.      */
  67.     public T getState() {
  68.         return state;
  69.     }
  70.  
  71.     /**
  72.      * Creates a new instance with no initial state.
  73.      * By using this constructor {@link #state} will be set
  74.      * to <code>null</code>.
  75.      */
  76.     public ReplayingActivator() {
  77.         this(null);
  78.     }
  79.  
  80.     /**
  81.      * Creates a new instance for an initial state.
  82.      * @param state The initial state.
  83.      */
  84.     public ReplayingActivator(T state) {
  85.         this.state = state;
  86.     }
  87.  
  88.     /**
  89.      * Stores the internal activation state.
  90.      * @param stage The new state.
  91.      */
  92.     protected void checkpoint(T state) {
  93.         this.state = state;
  94.     }
  95.  
  96.     /**
  97.      * Called when the encapsulating bundle is activated.
  98.      * @param state The internal activation state.
  99.      */
  100.     public abstract void start(T state);
  101.  
  102.     /**
  103.      * Called when the encapsulating bundle is deactivated.
  104.      * @param state The internal activation state.
  105.      */
  106.     public abstract void stop(T state);
  107.  
  108.     @Override
  109.     public void start() {
  110.         start(state);
  111.     }
  112.  
  113.     @Override
  114.     public void stop() {
  115.         stop(state);
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement