Guest User

Untitled

a guest
Jun 11th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. package org.sxam.core.SXAMEventSource;
  2.  
  3. import java.util.Map;
  4. import java.util.concurrent.ConcurrentHashMap;
  5.  
  6. import org.sxam.core.enums.SXAMEvent;
  7. import org.sxam.osrs.bot.structure.enums.LOG_LEVEL;
  8. import org.sxam.osrs.utils.Logger;
  9.  
  10. /**
  11.  * @author      sxam
  12.  * @version     2.00
  13.  * @since       02/05/2021
  14.  * A Observer pattern (publish/subscribe) implemented for pushing out SXAMEvents
  15.  *              to willing subscribers, used for spawning PLATFORM and UI events thus far.
  16.  *
  17.  *              For the use of anonymous in-line method support for lazy programmers, we
  18.  *              implemented the  methods that will be
  19.  *              automatically removed from the event queue during SXAMPlatform 'pause' tier
  20.  *              events.
  21.  *
  22.  *              Since I can appreciate that some programmers would perfer a simplier syntax when writing
  23.  *              their scripts, this option provides a means to clean up your anonymous Observers (ex:
  24.  *              
  25.  *               SXAMEventSourceSingleton.Instance.subscribe(SXAMEvent.UI_CHAT_MESSAGE_EVENT,
  26.  *                   (update) -> {
  27.  *                       SXAMEventChatMessage chatMessageUpdate = (SXAMEventChatMessage) update;
  28.  *                       // some work based on the message
  29.  *                   }, true
  30.  *               );
  31.  *              )
  32.  *              This saves time when writing Observers, rather than a fully extended class definition,
  33.  *              we can provide easier to reproduce and write method without the hassel of
  34.  *              pausing and cleaning.
  35.  *
  36.  *
  37.  * @see         org.sxam.osrs.ui.core.structure.SXAMUI.java                     For example fireEvents
  38.  * @see         org.ItemDisplayItem.sxam.osrs.ui.core.ItemDisplayHelper.java    For example subscribe/unsubscribe
  39.  * @see         org.sxam.core.SXAMEventSource.SXAMRenderSourceSingleton.java    For the UI render event.
  40.  */
  41. public class SXAMEventSourceSingleton {
  42.     // access this API via SXAMEventSourceSingleton.Instance
  43.     public static SXAMEventSourceSingleton Instance = new SXAMEventSourceSingleton();
  44.  
  45.     private SXAMEventSourceSingleton() {};
  46.  
  47.     public interface Observer {
  48.         void update(Object event);
  49.     }
  50.  
  51.     private final Map<Observer, SXAMEvent> _OBSERVERS = new ConcurrentHashMap<Observer, SXAMEvent>(64);
  52.     private final Map<Observer, SXAMEvent> _AUTO_PAUSE = new ConcurrentHashMap<Observer, SXAMEvent>(64);
  53.  
  54.     /**
  55.      * Typical subscribe method, use this if you have a formal definition (full class) extension
  56.      * of an Observer, you'll need to hook in to the platform specific 'pause' routine that your
  57.      * events are closed by, then manually unsubscribe your class objects.
  58.      *
  59.      * @see unsubscribe
  60.      * @param event         The SXAMEvent that your observer will be updated upon.
  61.      * @param observer      The formally defined extended Observer object which contains
  62.      *                      a single method: 'update' which consumes an update object relvant
  63.      *                      to the supplied SXAMEvent.
  64.      */
  65.     public void subscribe(SXAMEvent event, Observer observer) {
  66.         Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: An Observer subscribed to the [%s] event", event.getEventTypeName()));
  67.         this._OBSERVERS.put(observer, event);
  68.     }
  69.  
  70.     /**
  71.      * Supply the SXAMEvent and an anonymous Observer to consume it's updates,
  72.      * withPausing should be set to true for allowing us to automatically control the cleanup
  73.      * of this Observer.
  74.      *
  75.      * @param event         The SXAMEvent that your observer will be updated upon.
  76.      * @param observer      The formally defined extended Observer object which contains
  77.      *                      a single method: 'update' which consumes an update object relvant
  78.      *                      to the supplied SXAMEvent.
  79.      * @param withPausing   Supply true for allowing SXAM to automatically pause and destroy
  80.      *                      your anonymous Observer whenever the platform 'pause'
  81.      *                      or 'stop' events respectively occurs.
  82.      */
  83.     public void subscribe(SXAMEvent event, Observer observer, boolean withPausing) {
  84.         Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: An Observer subscribed via automatic pause to the [%s] event", event.getEventTypeName()));
  85.         this._OBSERVERS.put(observer, event);
  86.         if (withPausing) { this._AUTO_PAUSE.put(observer, event); }
  87.     }
  88.  
  89.     /**
  90.      *
  91.      * @param event
  92.      * @param observer
  93.      */
  94.     public void unsubscribe(SXAMEvent event, Observer observer) {
  95.         Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: An Observer unsubscribed from the [%s] Event", event.getEventTypeName()));
  96.         this._OBSERVERS.remove(observer, event);
  97.     }
  98.  
  99.     /**
  100.      *  
  101.      * @param newEventType
  102.      * @param newEventObject
  103.      */
  104.     public void publish(SXAMEvent newEventType, Object newEventObject) {
  105.         this._OBSERVERS.forEach((obs, evType) -> {
  106.             if (evType.getEventTypeName().equals(newEventType.getEventTypeName())) {
  107.                 obs.update(newEventObject);
  108.             }
  109.         });
  110.  
  111.         Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: Published event [%s] to Observers. EventObject class: [%s]", newEventType.getEventTypeName(), newEventObject.getClass().getName()));
  112.     }
  113.  
  114.     /**
  115.      * Via the use of subscribe(observer, eventType, boolean) method, observers will be
  116.      * enumerated and removed from the Observer Event list on platform wide 'pause' events,
  117.      * this assists in allowing programmers to be lazy.
  118.      *
  119.      * @param continueExecution True to add the Observers back into the Observer Event list.
  120.      *                          False to remove the Observers from the Observer Event list.
  121.      */
  122.     public void pause(boolean continueExecution) {
  123.         if (continueExecution) {
  124.             this._AUTO_PAUSE.forEach((obs, evType) -> {
  125.                 this._OBSERVERS.put(obs, evType);
  126.             });
  127.         } else {
  128.             this._AUTO_PAUSE.forEach((obs, evType) -> {
  129.                 this._OBSERVERS.remove(obs, evType);
  130.             });
  131.         }
  132.     }
  133.  
  134.     /**
  135.      * When the platform fires a 'stop' event, the EventSource will clear the observers in
  136.      * the Observer Event list & the Observer Pausing list.
  137.      */
  138.     public void clean() {
  139.         this.pause(false);
  140.         this._AUTO_PAUSE.clear();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment