Skip to content

Commit

Permalink
Resolves checkstyle errors for event-* (#1070)
Browse files Browse the repository at this point in the history
* Reduces checkstyle errors in event-aggregator

* Reduces checkstyle errors in event-asynchronous

* Reduces checkstyle errors in event-driven-architecture

* Reduces checkstyle errors in event-queue

* Reduces checkstyle errors in event-sourcing
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 10, 2019
1 parent 7c888e8 commit 5ae2ce6
Show file tree
Hide file tree
Showing 38 changed files with 208 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,22 @@
import java.util.List;

/**
*
* A system with lots of objects can lead to complexities when a client wants to subscribe to
* events. The client has to find and register for each object individually, if each object has
* multiple events then each event requires a separate subscription.
* <p>
* An Event Aggregator acts as a single source of events for many objects. It registers for all the
* events of the many objects allowing clients to register with just the aggregator.
* <p>
* In the example {@link LordBaelish}, {@link LordVarys} and {@link Scout} deliver events to
* {@link KingsHand}. {@link KingsHand}, the event aggregator, then delivers the events to
* {@link KingJoffrey}.
*
* <p>An Event Aggregator acts as a single source of events for many objects. It registers for all
* the events of the many objects allowing clients to register with just the aggregator.
*
* <p>In the example {@link LordBaelish}, {@link LordVarys} and {@link Scout} deliver events to
* {@link KingsHand}. {@link KingsHand}, the event aggregator, then delivers the events to {@link
* KingJoffrey}.
*/
public class App {

/**
* Program entry point
*
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* Event enumeration.
*
*/
public enum Event {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import java.util.List;

/**
*
* EventEmitter is the base class for event producers that can be observed.
*
*/
public abstract class EventEmitter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* Observers of events implement this interface.
*
*/
public interface EventObserver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* KingJoffrey observes events from {@link KingsHand}.
*
*/
public class KingJoffrey implements EventObserver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* KingsHand observes events from multiple sources and delivers them to listeners.
*
*/
public class KingsHand extends EventEmitter implements EventObserver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* LordBaelish produces events.
*
*/
public class LordBaelish extends EventEmitter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* LordVarys produces events.
*
*/
public class LordVarys extends EventEmitter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* Scout produces events.
*
*/
public class Scout extends EventEmitter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.event.aggregator;

/**
*
* Weekday enumeration
*
* Weekday enumeration.
*/
public enum Weekday {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,38 @@

package com.iluwatar.event.asynchronous;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of
* the pattern) may choose to run events in an Asynchronous or Synchronous mode. There can be
* multiple Asynchronous events running at once but only one Synchronous event can run at a time.
* Asynchronous events are synonymous to multi-threads. The key point here is that the threads run
* in the background and the user is free to carry on with other processes. Once an event is
* complete, the appropriate listener/callback method will be called. The listener then proceeds to
* carry out further processing depending on the needs of the user.
*
* This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may
* choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at
* once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key
* point here is that the threads run in the background and the user is free to carry on with other processes. Once an
* event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out
* further processing depending on the needs of the user.
* <p>The {@link EventManager} manages the events/threads that the user creates. Currently, the
* supported event operations are: <code>start</code>, <code>stop</code>, <code>getStatus</code>.
* For Synchronous events, the user is unable to start another (Synchronous) event if one is already
* running at the time. The running event would have to either be stopped or completed before a new
* event can be started.
*
* The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations
* are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to
* start another (Synchronous) event if one is already running at the time. The running event would have to either be
* stopped or completed before a new event can be started.
*
* The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many
* of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:-
* (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without
* interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each
* completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate
* with pending asynchronous operations using the familiar events-and-delegates model.
* <p>The Event-based Asynchronous Pattern makes available the advantages of multithreaded
* applications while hiding many of the complex issues inherent in multithreaded design. Using a
* class that supports this pattern can allow you to:- (1) Perform time-consuming tasks, such as
* downloads and database operations, "in the background," without interrupting your application.
* (2) Execute multiple operations simultaneously, receiving notifications when each completes. (3)
* Wait for resources to become available without stopping ("hanging") your application. (4)
* Communicate with pending asynchronous operations using the familiar events-and-delegates model.
*
* @see EventManager
* @see Event
*
*/
public class App {

Expand All @@ -67,8 +67,7 @@ public class App {
/**
* Program entry point.
*
* @param args
* command line args
* @param args command line args
*/
public static void main(String[] args) {
App app = new App();
Expand All @@ -78,8 +77,9 @@ public static void main(String[] args) {
}

/**
* App can run in interactive mode or not. Interactive mode == Allow user interaction with command line.
* Non-interactive is a quick sequential run through the available {@link EventManager} operations.
* App can run in interactive mode or not. Interactive mode == Allow user interaction with command
* line. Non-interactive is a quick sequential run through the available {@link EventManager}
* operations.
*/
public void setUp() {
Properties prop = new Properties();
Expand Down Expand Up @@ -118,24 +118,24 @@ public void quickRun() {

try {
// Create an Asynchronous event.
int aEventId = eventManager.createAsync(60);
LOGGER.info("Async Event [{}] has been created.", aEventId);
eventManager.start(aEventId);
LOGGER.info("Async Event [{}] has been started.", aEventId);
int asyncEventId = eventManager.createAsync(60);
LOGGER.info("Async Event [{}] has been created.", asyncEventId);
eventManager.start(asyncEventId);
LOGGER.info("Async Event [{}] has been started.", asyncEventId);

// Create a Synchronous event.
int sEventId = eventManager.create(60);
LOGGER.info("Sync Event [{}] has been created.", sEventId);
eventManager.start(sEventId);
LOGGER.info("Sync Event [{}] has been started.", sEventId);
int syncEventId = eventManager.create(60);
LOGGER.info("Sync Event [{}] has been created.", syncEventId);
eventManager.start(syncEventId);
LOGGER.info("Sync Event [{}] has been started.", syncEventId);

eventManager.status(aEventId);
eventManager.status(sEventId);
eventManager.status(asyncEventId);
eventManager.status(syncEventId);

eventManager.cancel(aEventId);
LOGGER.info("Async Event [{}] has been stopped.", aEventId);
eventManager.cancel(sEventId);
LOGGER.info("Sync Event [{}] has been stopped.", sEventId);
eventManager.cancel(asyncEventId);
LOGGER.info("Async Event [{}] has been stopped.", asyncEventId);
eventManager.cancel(syncEventId);
LOGGER.info("Sync Event [{}] has been stopped.", syncEventId);

} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
Expand Down Expand Up @@ -211,16 +211,17 @@ private void processOption1(EventManager eventManager, Scanner s) {
int eventId = eventManager.createAsync(eventTime);
eventManager.start(eventId);
LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
} catch (MaxNumOfEventsAllowedException | LongRunningEventException
| EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
} else if (eventType.equalsIgnoreCase("S")) {
try {
int eventId = eventManager.create(eventTime);
eventManager.start(eventId);
LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
| EventDoesNotExistException e) {
} catch (MaxNumOfEventsAllowedException | InvalidOperationException
| LongRunningEventException | EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* Each Event runs as a separate/individual thread.
*
*/
public class Event implements IEvent, Runnable {

Expand All @@ -43,9 +41,10 @@ public class Event implements IEvent, Runnable {
private ThreadCompleteListener eventListener;

/**
* Constructor.
*
* @param eventId event ID
* @param eventTime event time
* @param eventId event ID
* @param eventTime event time
* @param isSynchronous is of synchronous type
*/
public Event(final int eventId, final int eventTime, final boolean isSynchronous) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.iluwatar.event.asynchronous;

/**
* Custom Exception Class for Non Existent Event
* Custom Exception Class for Non Existent Event.
*/
public class EventDoesNotExistException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,28 @@
import java.util.concurrent.ConcurrentHashMap;

/**
*
* EventManager handles and maintains a pool of event threads. {@link Event} threads are created upon user request. Thre
* are two types of events; Asynchronous and Synchronous. There can be multiple Asynchronous events running at once but
* only one Synchronous event running at a time. Currently supported event operations are: start, stop, and getStatus.
* Once an event is complete, it then notifies EventManager through a listener. The EventManager then takes the event
* out of the pool.
*
* EventManager handles and maintains a pool of event threads. {@link Event} threads are created
* upon user request. Thre are two types of events; Asynchronous and Synchronous. There can be
* multiple Asynchronous events running at once but only one Synchronous event running at a time.
* Currently supported event operations are: start, stop, and getStatus. Once an event is complete,
* it then notifies EventManager through a listener. The EventManager then takes the event out of
* the pool.
*/
public class EventManager implements ThreadCompleteListener {

public static final int MAX_RUNNING_EVENTS = 1000; // Just don't wanna have too many running events. :)
public static final int MAX_RUNNING_EVENTS = 1000;
// Just don't wanna have too many running events. :)
public static final int MIN_ID = 1;
public static final int MAX_ID = MAX_RUNNING_EVENTS;
public static final int MAX_EVENT_TIME = 1800; // in seconds / 30 minutes.
private int currentlyRunningSyncEvent = -1;
private Random rand;
private Map<Integer, Event> eventPool;

private static final String DOES_NOT_EXIST = " does not exist.";

/**
* EventManager constructor.
*
*/
public EventManager() {
rand = new Random(1);
Expand All @@ -65,14 +64,15 @@ public EventManager() {
* @param eventTime Time an event should run for.
* @return eventId
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
* @throws InvalidOperationException No new synchronous events can be created when one is already running.
* @throws LongRunningEventException Long running events are not allowed in the app.
* @throws InvalidOperationException No new synchronous events can be created when one is
* already running.
* @throws LongRunningEventException Long running events are not allowed in the app.
*/
public int create(int eventTime)
throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException {
if (currentlyRunningSyncEvent != -1) {
throw new InvalidOperationException(
"Event [" + currentlyRunningSyncEvent + "] is still running. Please wait until it finishes and try again.");
throw new InvalidOperationException("Event [" + currentlyRunningSyncEvent + "] is still"
+ " running. Please wait until it finishes and try again.");
}

int eventId = createEvent(eventTime, true);
Expand All @@ -87,16 +87,18 @@ public int create(int eventTime)
* @param eventTime Time an event should run for.
* @return eventId
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
* @throws LongRunningEventException Long running events are not allowed in the app.
* @throws LongRunningEventException Long running events are not allowed in the app.
*/
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException,
LongRunningEventException {
return createEvent(eventTime, false);
}

private int createEvent(int eventTime, boolean isSynchronous)
throws MaxNumOfEventsAllowedException, LongRunningEventException {
if (eventPool.size() == MAX_RUNNING_EVENTS) {
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later.");
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment."
+ " Please try again later.");
}

if (eventTime >= MAX_EVENT_TIME) {
Expand Down Expand Up @@ -185,7 +187,8 @@ public void shutdown() {
}

/**
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
* Returns a pseudo-random number between min and max, inclusive. The difference between min and
* max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*/
private int generateId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
package com.iluwatar.event.asynchronous;

/**
* Events that fulfill the start stop and list out current status behaviour
* follow this interface
* Events that fulfill the start stop and list out current status behaviour follow this interface.
*/
public interface IEvent {

Expand Down

0 comments on commit 5ae2ce6

Please sign in to comment.