3232 */
3333public class EventManager implements ThreadCompleteListener {
3434
35- private int minID = 1 ;
36- private int maxID = Integer .MAX_VALUE - 1 ; // Be cautious of overflows.
35+ private int minId = 1 ;
36+ private int maxId = Integer .MAX_VALUE - 1 ; // Be cautious of overflows.
3737 private int maxRunningEvents = 1000 ; // no particular reason. Just don't wanna have too many running events. :)
3838 private int maxEventTime = 1800 ; // in seconds / 30 minutes.
3939 private int currentlyRunningSyncEvent = -1 ;
4040 private Random rand ;
4141 private Map <Integer , Event > eventPool ;
4242
43+ /**
44+ * EventManager constructor.
45+ *
46+ */
4347 public EventManager () {
4448 rand = new Random (1 );
4549 eventPool = new ConcurrentHashMap <Integer , Event >(maxRunningEvents );
4650
4751 }
4852
49- // Create a Synchronous event.
53+ /**
54+ * Create a Synchronous event.
55+ *
56+ * @param eventTime Time an event should run for.
57+ * @return eventId
58+ * @throws MaxNumOfEventsAllowedException When too many events are running at a time.
59+ * @throws InvalidOperationException No new synchronous events can be created when one is already running.
60+ * @throws LongRunningEventException Long running events are not allowed in the app.
61+ */
5062 public int createSyncEvent (int eventTime )
5163 throws MaxNumOfEventsAllowedException , InvalidOperationException , LongRunningEventException {
52- int eventID = createEvent (eventTime );
64+ int eventId = createEvent (eventTime );
5365 if (currentlyRunningSyncEvent != -1 ) {
5466 throw new InvalidOperationException (
5567 "Event [" + currentlyRunningSyncEvent + "] is still running. Please wait until it finishes and try again." );
5668 }
57- currentlyRunningSyncEvent = eventID ;
69+ currentlyRunningSyncEvent = eventId ;
5870
59- return eventID ;
71+ return eventId ;
6072 }
6173
62- // Create an Asynchronous event.
74+ /**
75+ * Create an Asynchronous event.
76+ *
77+ * @param eventTime Time an event should run for.
78+ * @return eventId
79+ * @throws MaxNumOfEventsAllowedException When too many events are running at a time.
80+ * @throws LongRunningEventException Long running events are not allowed in the app.
81+ */
6382 public int createAsyncEvent (int eventTime ) throws MaxNumOfEventsAllowedException , LongRunningEventException {
6483 return createEvent (eventTime );
6584 }
@@ -74,44 +93,65 @@ private int createEvent(int eventTime) throws MaxNumOfEventsAllowedException, Lo
7493 "Maximum event time allowed is " + maxEventTime + " seconds. Please try again." );
7594 }
7695
77- int newEventID = generateID ();
96+ int newEventId = generateId ();
7897
79- Event newEvent = new Event (newEventID , eventTime );
98+ Event newEvent = new Event (newEventId , eventTime );
8099 newEvent .addListener (this );
81- eventPool .put (newEventID , newEvent );
100+ eventPool .put (newEventId , newEvent );
82101
83- return newEventID ;
102+ return newEventId ;
84103 }
85104
86- public void startEvent (int eventID ) throws EventDoesNotExistException {
87- if (!eventPool .containsKey (eventID )) {
88- throw new EventDoesNotExistException (eventID + " does not exist." );
105+ /**
106+ * Starts event.
107+ *
108+ * @param eventId The event that needs to be started.
109+ * @throws EventDoesNotExistException If event does not exist in our eventPool.
110+ */
111+ public void startEvent (int eventId ) throws EventDoesNotExistException {
112+ if (!eventPool .containsKey (eventId )) {
113+ throw new EventDoesNotExistException (eventId + " does not exist." );
89114 }
90115
91- eventPool .get (eventID ).start ();
116+ eventPool .get (eventId ).start ();
92117 }
93118
94- public void stopEvent (int eventID ) throws EventDoesNotExistException {
95- if (!eventPool .containsKey (eventID )) {
96- throw new EventDoesNotExistException (eventID + " does not exist." );
119+ /**
120+ * Stops event.
121+ *
122+ * @param eventId The event that needs to be stopped.
123+ * @throws EventDoesNotExistException If event does not exist in our eventPool.
124+ */
125+ public void stopEvent (int eventId ) throws EventDoesNotExistException {
126+ if (!eventPool .containsKey (eventId )) {
127+ throw new EventDoesNotExistException (eventId + " does not exist." );
97128 }
98129
99- if (eventID == currentlyRunningSyncEvent ) {
130+ if (eventId == currentlyRunningSyncEvent ) {
100131 currentlyRunningSyncEvent = -1 ;
101132 }
102133
103- eventPool .get (eventID ).stop ();
104- eventPool .remove (eventID );
134+ eventPool .get (eventId ).stop ();
135+ eventPool .remove (eventId );
105136 }
106137
107- public void getStatus (int eventID ) throws EventDoesNotExistException {
108- if (!eventPool .containsKey (eventID )) {
109- throw new EventDoesNotExistException (eventID + " does not exist." );
138+ /**
139+ * Get status of a running event.
140+ *
141+ * @param eventId The event to inquire status of.
142+ * @throws EventDoesNotExistException If event does not exist in our eventPool.
143+ */
144+ public void getStatus (int eventId ) throws EventDoesNotExistException {
145+ if (!eventPool .containsKey (eventId )) {
146+ throw new EventDoesNotExistException (eventId + " does not exist." );
110147 }
111148
112- eventPool .get (eventID ).status ();
149+ eventPool .get (eventId ).status ();
113150 }
114151
152+ /**
153+ * Gets status of all running events.
154+ */
115155 @ SuppressWarnings ("rawtypes" )
116156 public void getStatusOfAllEvents () {
117157 Iterator it = eventPool .entrySet ().iterator ();
@@ -125,12 +165,12 @@ public void getStatusOfAllEvents() {
125165 * Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
126166 * <code>Integer.MAX_VALUE - 1</code>.
127167 */
128- private int generateID () {
168+ private int generateId () {
129169 // nextInt is normally exclusive of the top value,
130170 // so add 1 to make it inclusive
131- int randomNum = rand .nextInt ((maxID - minID ) + 1 ) + minID ;
171+ int randomNum = rand .nextInt ((maxId - minId ) + 1 ) + minId ;
132172 while (eventPool .containsKey (randomNum )) {
133- randomNum = rand .nextInt ((maxID - minID ) + 1 ) + minID ;
173+ randomNum = rand .nextInt ((maxId - minId ) + 1 ) + minId ;
134174 }
135175
136176 return randomNum ;
@@ -140,9 +180,9 @@ private int generateID() {
140180 * Callback from an {@link Event} (once it is complete). The Event is then removed from the pool.
141181 */
142182 @ Override
143- public void notifyOfThreadComplete (int eventID ) {
144- eventPool .get (eventID ).status ();
145- eventPool .remove (eventID );
183+ public void notifyOfThreadComplete (int eventId ) {
184+ eventPool .get (eventId ).status ();
185+ eventPool .remove (eventId );
146186 }
147187
148188}
0 commit comments