Skip to content

Commit dbd605e

Browse files
author
WSSIA
committed
Changes based on latest code review
1 parent f215951 commit dbd605e

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ public void runInteractiveMode() {
138138

139139
Scanner s = new Scanner(System.in);
140140
int option = -1;
141-
while (option != 5) {
141+
while (option != 4) {
142142
System.out.println("Hello. Would you like to boil some eggs?");
143-
System.out.println(
144-
"(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW IS MY EGG? \n(4) HOW ARE MY EGGS? \n(5) EXIT");
145-
System.out.print("Choose [1,2,3,4,5]: ");
143+
System.out.println("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT");
144+
System.out.print("Choose [1,2,3,4]: ");
146145
option = s.nextInt();
147146

148147
if (option == 1) {
@@ -181,16 +180,22 @@ public void runInteractiveMode() {
181180
System.out.println(e.getMessage());
182181
}
183182
} else if (option == 3) {
184-
System.out.print("Which egg?: ");
185-
int eventId = s.nextInt();
186-
try {
187-
eventManager.status(eventId);
188-
} catch (EventDoesNotExistException e) {
189-
System.out.println(e.getMessage());
183+
s.nextLine();
184+
System.out.print("Just one egg (O) OR all of them (A) ?: ");
185+
String eggChoice = s.nextLine();
186+
187+
if (eggChoice.equalsIgnoreCase("O")) {
188+
System.out.print("Which egg?: ");
189+
int eventId = s.nextInt();
190+
try {
191+
eventManager.status(eventId);
192+
} catch (EventDoesNotExistException e) {
193+
System.out.println(e.getMessage());
194+
}
195+
} else if (eggChoice.equalsIgnoreCase("A")) {
196+
eventManager.statusOfAllEvents();
190197
}
191198
} else if (option == 4) {
192-
eventManager.statusOfAllEvents();
193-
} else if (option == 5) {
194199
eventManager.shutdown();
195200
}
196201
}

event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/Event.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,25 @@ public class Event implements IEvent, Runnable {
2525

2626
private int eventId;
2727
private int eventTime;
28+
private boolean isSynchronous;
2829
private Thread thread;
2930
private boolean isComplete = false;
3031
private ThreadCompleteListener eventListener;
3132

32-
public Event(final int eventId, final int eventTime) {
33+
/**
34+
*
35+
* @param eventId event ID
36+
* @param eventTime event time
37+
* @param isSynchronous is of synchronous type
38+
*/
39+
public Event(final int eventId, final int eventTime, final boolean isSynchronous) {
3340
this.eventId = eventId;
3441
this.eventTime = eventTime;
42+
this.isSynchronous = isSynchronous;
43+
}
44+
45+
public boolean isSynchronous() {
46+
return isSynchronous;
3547
}
3648

3749
@Override
@@ -42,6 +54,9 @@ public void start() {
4254

4355
@Override
4456
public void stop() {
57+
if (null == thread) {
58+
return;
59+
}
4560
thread.interrupt();
4661
}
4762

event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ public EventManager() {
6161
*/
6262
public int create(int eventTime)
6363
throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException {
64-
int eventId = createEvent(eventTime);
6564
if (currentlyRunningSyncEvent != -1) {
6665
throw new InvalidOperationException(
6766
"Event [" + currentlyRunningSyncEvent + "] is still running. Please wait until it finishes and try again.");
6867
}
68+
69+
int eventId = createEvent(eventTime, true);
6970
currentlyRunningSyncEvent = eventId;
7071

7172
return eventId;
@@ -80,10 +81,11 @@ public int create(int eventTime)
8081
* @throws LongRunningEventException Long running events are not allowed in the app.
8182
*/
8283
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
83-
return createEvent(eventTime);
84+
return createEvent(eventTime, false);
8485
}
8586

86-
private int createEvent(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
87+
private int createEvent(int eventTime, boolean isSynchronous)
88+
throws MaxNumOfEventsAllowedException, LongRunningEventException {
8789
if (eventPool.size() == MAX_RUNNING_EVENTS) {
8890
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later.");
8991
}
@@ -95,7 +97,7 @@ private int createEvent(int eventTime) throws MaxNumOfEventsAllowedException, Lo
9597

9698
int newEventId = generateId();
9799

98-
Event newEvent = new Event(newEventId, eventTime);
100+
Event newEvent = new Event(newEventId, eventTime, isSynchronous);
99101
newEvent.addListener(this);
100102
eventPool.put(newEventId, newEvent);
101103

@@ -194,6 +196,9 @@ private int generateId() {
194196
@Override
195197
public void completedEventHandler(int eventId) {
196198
eventPool.get(eventId).status();
199+
if (eventPool.get(eventId).isSynchronous()) {
200+
currentlyRunningSyncEvent = -1;
201+
}
197202
eventPool.remove(eventId);
198203
}
199204

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
INTERACTIVE_MODE=NO
1+
INTERACTIVE_MODE=YES

0 commit comments

Comments
 (0)