]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/UpdateMessageBroker.java
Version 18.1, September 2015
[GpsPrune.git] / tim / prune / UpdateMessageBroker.java
1 package tim.prune;
2
3 /**
4  * Class responsible for distributing update information
5  * to all registered listeners
6  */
7 public abstract class UpdateMessageBroker
8 {
9         private static final int MAXIMUM_NUMBER_SUBSCRIBERS = 8;
10         /** Array of all subscribers */
11         private static DataSubscriber[] _subscribers = new DataSubscriber[MAXIMUM_NUMBER_SUBSCRIBERS];
12         /** Index from which to start looking for an empty slot*/
13         private static int _searchStartIndex = 0;
14         /** Enable/disabled flag */
15         private static boolean _enabled = true;
16
17
18         /**
19          * Add a data subscriber to the list
20          * @param inSub DataSubscriber to add
21          */
22         public static void addSubscriber(DataSubscriber inSub)
23         {
24                 // Loop looking for first null entry
25                 for (int i=_searchStartIndex; i<MAXIMUM_NUMBER_SUBSCRIBERS; i++)
26                 {
27                         if (_subscribers[i] == null)
28                         {
29                                 _subscribers[i] = inSub;
30                                 _searchStartIndex = i+1;
31                                 break;
32                         }
33                 }
34         }
35
36         /**
37          * Remove the given subscriber from the list
38          * @param inSub subscriber to remove
39          */
40         public static void removeSubscriber(DataSubscriber inSub)
41         {
42                 for (int i=0; i<MAXIMUM_NUMBER_SUBSCRIBERS; i++)
43                 {
44                         if (_subscribers[i] == inSub)
45                         {
46                                 _subscribers[i] = null;
47                                 // Could break out of the loop here but we want to make sure we remove all of them
48                         }
49                 }
50                 _searchStartIndex = 0; // for the next add, start from beginning to ensure all gaps are filled
51         }
52
53         /**
54          * Enable or disable the messaging (to allow temporary disabling for multiple operations)
55          * @param inEnabled false to disable, true to enable again
56          */
57         public static void enableMessaging(boolean inEnabled)
58         {
59                 _enabled = inEnabled;
60         }
61
62         /**
63          * Send a message to all subscribers that
64          * the data has been updated
65          */
66         public static void informSubscribers()
67         {
68                 informSubscribers(DataSubscriber.ALL);
69         }
70
71
72         /**
73          * Send message to all subscribers
74          * @param inChange Change that occurred
75          */
76         public static void informSubscribers(byte inChange)
77         {
78                 // TODO: Launch separate thread so that whatever caused the inform can finish
79                 if (!_enabled) return;
80                 for (int i=0; i<_subscribers.length; i++)
81                 {
82                         if (_subscribers[i] != null)
83                         {
84                                 _subscribers[i].dataUpdated(inChange);
85                         }
86                 }
87         }
88
89         /**
90          * Send message to all subscribers
91          * @param inMessage message to display informing of action completed
92          */
93         public static void informSubscribers(String inMessage)
94         {
95                 if (!_enabled) return;
96                 for (int i=0; i<_subscribers.length; i++)
97                 {
98                         if (_subscribers[i] != null)
99                         {
100                                 _subscribers[i].actionCompleted(inMessage);
101                         }
102                 }
103         }
104 }