4 * Class responsible for distributing update information
5 * to all registered listeners
7 public abstract class UpdateMessageBroker
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;
19 * Add a data subscriber to the list
20 * @param inSub DataSubscriber to add
22 public static void addSubscriber(DataSubscriber inSub)
24 // Loop looking for first null entry
25 for (int i=_searchStartIndex; i<MAXIMUM_NUMBER_SUBSCRIBERS; i++)
27 if (_subscribers[i] == null)
29 _subscribers[i] = inSub;
30 _searchStartIndex = i+1;
37 * Remove the given subscriber from the list
38 * @param inSub subscriber to remove
40 public static void removeSubscriber(DataSubscriber inSub)
42 for (int i=0; i<MAXIMUM_NUMBER_SUBSCRIBERS; i++)
44 if (_subscribers[i] == inSub)
46 _subscribers[i] = null;
47 // Could break out of the loop here but we want to make sure we remove all of them
50 _searchStartIndex = 0; // for the next add, start from beginning to ensure all gaps are filled
54 * Enable or disable the messaging (to allow temporary disabling for multiple operations)
55 * @param inEnabled false to disable, true to enable again
57 public static void enableMessaging(boolean inEnabled)
63 * Send a message to all subscribers that
64 * the data has been updated
66 public static void informSubscribers()
68 informSubscribers(DataSubscriber.ALL);
73 * Send message to all subscribers
74 * @param inChange Change that occurred
76 public static void informSubscribers(byte inChange)
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++)
82 if (_subscribers[i] != null)
84 _subscribers[i].dataUpdated(inChange);
90 * Send message to all subscribers
91 * @param inMessage message to display informing of action completed
93 public static void informSubscribers(String inMessage)
95 if (!_enabled) return;
96 for (int i=0; i<_subscribers.length; i++)
98 if (_subscribers[i] != null)
100 _subscribers[i].actionCompleted(inMessage);