Problem Statement
You have to create a server that processes events, adds and removes listeners, and sends important events to all active listeners.
Given class Server:
Represents a server object that processes events, and adds and removes listeners.
abstract class Server implements Listener, Notifier {
/**
* Constructs a new server with the given data seed. The constructors for
* all subclasses of the Server class call this constructor.
*
* @param dataSeed The data seed.
*/
public Server(int dataSeed);
/**
* If the event is not ImportantEvent, do not process.
* Otherwise, pass the event to all the currently registered listeners by
* using their processEvent method.
*
* @param e An Event to be processed.
*/
abstract public void processEvent(Event e);
/**
* Starts the server. The server now receives some events to process
* and some calls to add and remove listeners. There can be a maximum of 10000 calls to
* methods processEvent, addListener, and removeListener.
*
* There can be a maximum of 50 distinct added listeners at any point in time.
*
* @return An integer result.
*/
public int startServer();
/**
* @see addListener in Listener
*/
public abstract void addListener(Listener l);
/*
* @see removeListener in Listener
*/
public abstract void removeListener(Listener l);
}
Given interface Notifier:
All objects implementing this interface can add and remove listeners. The implmentation need not be thread-safe.
interface Notifier {
/**
* If the listener with the same reference is currently added,
* the method does not process. Otherwise, it adds this listener.
*
* @param l The Listener to be added.
*/
public void addListener(Listener l);
/**
* If the listener with the same reference is not currently added,
* the method does not process. Otherwise, it removes this listener.
*
* @param l The Listener to be removed.
*/
public void removeListener(Listener l);
}
Given class Listener:
All objects implementing this interface can process events. The implementation need not be thread-safe.
interface Listener {
/**
* This method processes an incoming event.
*
* @param e An incoming Event
*/
public void processEvent(Event e);
}
Given class Event, with sub-class ImportantEvent:
Event describes a single event. ImportantEvent describes an important event.
DEFINITION
You must define public class NotifyListeners with public method int startServer(int dataSeed).
startServer must instantiate CustomServer by passing dataSeed to CustomServer(int). Call startServer() to get an integer and return this integer.
CUSTOMSERVER IMPLEMENTATION
You must declare class CustomServer that extends Server. Implement all abstract methods and do not change the implementation for all the other methods.
Definition
- Class:
- NotifyListeners
- Method:
- startServer
- Parameters:
- int
- Returns:
- int
- Method signature:
- int startServer(int dataSeed)
- (be sure your method is public)
Constraints
- dataSeed will be between 0 and 1,000,000,000, inclusive.
Examples
1
Returns: -872114613
After the start, your server will receive the following requests (in the given order): add listener 1 (listener added); process important event (event is passed to listener 1); add listener 1 (nothing happens because the listener is already registered).2
Returns: 1471158495
After the start, your server will receive the following requests (in the given order): process usual event (nothing happens because the event is not important); process important event (still nothing happens because there are no registered listeners); add listener 1 (listener added); process important event (event is passed to listener 1); remove listener 2 (nothing happens because this listener is not registered); process usual event (nothing happens because the event is not important).3
Returns: 1525885969
After the start, your server will receive the following requests (in the given order): process important event (nothing happens); add listener 1 (listener added); remove listener 1 (listener removed); remove listener 2 (nothing happens); add listener 3 (listener added); remove listener 2 (nothing happens); process usual event (nothing happens); remove listener 1 (nothing happens - the listener is already not registered); remove listener 4 (nothing happens); process important event (event is passed to listener 3).4
Returns: 77520079
After the start, your server will receive 100 requests.