public abstract class AbstractWorldView extends Object implements IWorldView
For raising new IWorldEvent in descendants call protected method raiseEvent(IWorldEvent event), that is preventing recursion.
Note that there is a big advantage in how the listeners are called and how objects are stored.
The event notifying method (raiseEvent()) is respecting the class/interface hierarchy thus informing listeners hooked on all levels of the hierarchy.
The items are stored according the the class/interface hierarchy as well!
Example:You have interface ItemEvent (extends IWorldObjectEvent) and it's implementation WeaponEvent and HealthEvent. Perheps you want to listen for all events on WeaponEvent, so you will create IWorldEventListener<WeaponEvent>. But hey - you may want to listen on both WeaponEvent and HealthEvent (and perheps any ItemEvent there is), that's easy - just create IWorldEventListener<ItemEvent> and you will receive both WeaponEvent and HealthEvent. That's because during event handling we're probing the event class ancestors / interfaces and informing all listeners on all class-hierarchy levels.
Ultimately you may create IWorldEventListener<IWorldEvent> to catch all events the world view produces (be careful because it may cause serious performance hit if you process these events slowly).
Same goes for storing the items under it's class in the 'worldObjects'.
Modifier and Type | Field and Description |
---|---|
static cz.cuni.amis.utils.token.Token |
COMPONENT_ID |
protected IComponentControlHelper |
control |
protected ComponentController<IComponent> |
controller |
protected IComponentBus |
eventBus |
protected LogCategory |
log |
Constructor and Description |
---|
AbstractWorldView(ComponentDependencies dependencies,
IComponentBus bus,
IAgentLogger logger) |
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(Class<?> event,
IWorldEventListener<?> listener)
Adds listener to a specific event (Level A listeners).
|
void |
addObjectListener(Class<?> objectClass,
Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Adds listener to a specified 'event' that occurs on the specific 'objectClass' (Level C listeners).
|
void |
addObjectListener(Class<?> objectClass,
IWorldObjectEventListener<?,?> listener)
Adds listener to all events that happens on any object of the 'objectClass' (Level B listeners).
|
void |
addObjectListener(WorldObjectId objectId,
Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Adds listener to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).
|
void |
addObjectListener(WorldObjectId objectId,
IWorldObjectEventListener<?,?> listener)
Adds listener to all events that happens on object with specific 'objectId' (Level D listeners).
|
protected void |
addWorldObject(IWorldObject worldObject)
Method that adds a new world object to the object maps.
|
protected void |
cleanUp()
Cleans up internal data structures, called from start/stop/kill/reset methods.
|
Map<WorldObjectId,IWorldObject> |
get()
Returns map with objects inserted according to their id.
|
IWorldObject |
get(WorldObjectId objectId)
Returns a world object of the specific id (if exists inside the world view).
|
<T extends IWorldObject> |
get(WorldObjectId id,
Class<T> clazz)
Returns a world object of the specific id and class (if exists inside the world view).
|
Map<Class,Map<WorldObjectId,IWorldObject>> |
getAll()
Returns map of all objects that are present in the world view.
|
<T extends IWorldObject> |
getAll(Class<T> type)
Returns map of all objects of a specific type that are present in the world view.
|
cz.cuni.amis.utils.token.Token |
getComponentId()
Unique identification of the component.
|
IComponentBus |
getEventBus() |
LogCategory |
getLog() |
<T extends IWorldObject> |
getSingle(Class<T> cls)
Returns the only instance of required object if present, if there are
more instances of this object then
IllegalArgumentException will be thrown. |
boolean |
isListening(Class<?> objectClass,
Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Tests whether the 'listener' is listening at specified 'objectClass' for specified 'event' (Level C listeners).
|
boolean |
isListening(Class<?> eventClass,
IWorldEventListener<?> listener)
Tests whether the 'listener' is listening to a specific event (Level A listeners).
|
boolean |
isListening(Class<?> objectClass,
IWorldObjectEventListener<?,?> listener)
Tests whether the 'listener' is listening at specified 'objectClass' (Level B listeners).
|
boolean |
isListening(IWorldEventListener<?> listener)
Checks whether this listener is hooked to the world view (at any listener level).
|
boolean |
isListening(WorldObjectId objectId,
Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Tests whether the 'listener' is listening to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).
|
boolean |
isListening(WorldObjectId objectId,
IWorldObjectEventListener<?,?> listener)
Tests whether the 'listener' is listening at specified 'objectId' (Level D Listeners).
|
protected boolean |
isPaused() |
protected boolean |
isRunning() |
protected void |
kill()
Kills the world view.
|
protected void |
pause()
Pauses the world view.
|
protected void |
prePause()
Pre-pauses the world view.
|
protected void |
preStop()
Pre-stops the world view.
|
protected void |
raiseEvent(IWorldEvent event)
Process new IWorldEvent - notify all the listeners about it.
|
void |
removeEventListener(Class<?> eventClass,
IWorldEventListener<?> listener)
Removes listener from a specific event (Level A listeners).
|
void |
removeListener(IWorldEventListener<?> listener)
Removes listener from every listeners category (from every listener level).
|
void |
removeObjectListener(Class<?> objectClass,
Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Removes listener from specific 'objectClass' listening for specified 'event' (Level C listeners).
|
void |
removeObjectListener(Class<?> objectClass,
IWorldObjectEventListener<?,?> listener)
Removes listener from specific 'objectClass' listening for specified 'event' (Level B listeners).
|
void |
removeObjectListener(WorldObjectId objectId,
Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Removes listener to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).
|
void |
removeObjectListener(WorldObjectId objectId,
IWorldObjectEventListener<?,?> listener)
Removes listener from objects with specified 'objectId' (Level D Listeners).
|
protected void |
removeWorldObject(IWorldObject worldObject)
Removes world object from the world view - this will be called from the descendants
of the AbstractWorldView whenever world object should disappear from the world view
(was destroyed in the world).
|
protected void |
reset()
Resets the world view so it is start()able again.
|
protected void |
resume()
Resumes the world view.
|
protected void |
start(boolean startPaused)
Starts the world view.
|
protected void |
stop()
Stops the world view.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
notify, notifyAfterPropagation, notifyImmediately
public static final cz.cuni.amis.utils.token.Token COMPONENT_ID
protected LogCategory log
protected IComponentBus eventBus
protected ComponentController<IComponent> controller
protected IComponentControlHelper control
public AbstractWorldView(ComponentDependencies dependencies, IComponentBus bus, IAgentLogger logger)
protected void cleanUp()
If you override this method, do not forget to call super.cleanUp().
protected void start(boolean startPaused)
If you override this method, do not forget to call super.start().
protected void prePause()
If you override this method, do not forget to call super.preStop().
protected void pause()
If you override this method, do not forget to call super.start().
protected void resume()
If you override this method, do not forget to call super.start().
protected void preStop()
If you override this method, do not forget to call super.preStop().
protected void stop()
If you override this method, do not forget to call super.stop().
protected void kill()
If you override this method, do not forget to call super.stop().
protected void reset()
If you override this method, do not forget to call super.reset().
protected boolean isRunning()
protected boolean isPaused()
public cz.cuni.amis.utils.token.Token getComponentId()
IComponent
getComponentId
in interface IComponent
public LogCategory getLog()
public IComponentBus getEventBus()
getEventBus
in interface IWorldView
public void addEventListener(Class<?> event, IWorldEventListener<?> listener)
IWorldView
It is the most general type of listener-registration allowing you to sniff any type of events.
Events passed to the listener are filtered only according to the 'event' class.
WARNING: even though the method does not require templated class and listener, you must be sure that 'listener' can accept 'eventClass'.
addEventListener
in interface IWorldView
event
- which event types you want to receivelistener
- where you want to handle these eventspublic void addObjectListener(Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
IWorldView
Events passed to the listener are filtered according to the 'objectClass'.
WARNING: even though the method does not require templated classes and listener, you must be sure that 'listener' accepts all events (IWorldObjectEvent) for objects of 'objectClass'.
addObjectListener
in interface IWorldView
objectClass
- which object class you want to listen atlistener
- where you want to handle these eventspublic void addObjectListener(Class<?> objectClass, Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
Events passed to the listener are filtered according to the 'event' and 'objectClass' of the object the event happened upon.
WARNING: even though the method does not require templated classes and listener, you must be sure that 'listener' accepts 'eventClass' for objects of 'objectClass'.
eventClass can be any implementor of IWorldObjectEvent
. E.g.
WorldObjectAppearedEvent
, WorldObjectDestroyedEvent
, WorldObjectDisappearedEvent
,
WorldObjectFirstEncounteredEvent
or WorldObjectUpdatedEvent
.
addObjectListener
in interface IWorldView
objectClass
- which object class you want to listen ateventClass
- which event class you want to receivelistener
- where you want to handle these eventspublic void addObjectListener(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
IWorldView
Events passed to the listener are filtered according to the 'objectId' of the object.
WARNING: you must ensure that 'listener' can accept the event raised on object of specified 'objectId'.
addObjectListener
in interface IWorldView
objectId
- which object you want to listen atlistener
- where you want to handle eventspublic void addObjectListener(WorldObjectId objectId, Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
Events passed to the listener are filtered according to the 'event' and 'objectId' of the object.
WARNING: even though the method does not require templated classes and listener, you must be sure that 'listener' accepts 'eventClass' for objects of specified 'objectId'.
addObjectListener
in interface IWorldView
objectId
- which object you want to listen ateventClass
- which event classes you want to receivelistener
- where you want to handle these eventspublic boolean isListening(Class<?> eventClass, IWorldEventListener<?> listener)
IWorldView
isListening
in interface IWorldView
eventClass
- which events you want to receivelistener
- that is testedpublic boolean isListening(Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectClass
- where the listener is testedlistener
- that is testedpublic boolean isListening(Class<?> objectClass, Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectClass
- where the listener is testedeventClass
- where the listener is testedlistener
- that is testedpublic boolean isListening(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectId
- where the listener is testedlistener
- that is testedpublic boolean isListening(WorldObjectId objectId, Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectId
- where the listener is testedeventClass
- what class is testedlistener
- that is testedpublic boolean isListening(IWorldEventListener<?> listener)
IWorldView
WARNING: Can be time consuming! (Iterating through all levels of listeners.)
isListening
in interface IWorldView
public void removeEventListener(Class<?> eventClass, IWorldEventListener<?> listener)
IWorldView
removeEventListener
in interface IWorldView
eventClass
- which events class you want to remove the listener fromlistener
- you want to removepublic void removeObjectListener(Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
IWorldView
removeObjectListener
in interface IWorldView
objectClass
- class of objects you want the listener to remove fromlistener
- you want to removepublic void removeObjectListener(Class<?> objectClass, Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
removeObjectListener
in interface IWorldView
objectClass
- class of objects you want the listener to remove fromeventClass
- which events class you want to remove the listener fromlistener
- you want to removepublic void removeObjectListener(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
IWorldView
removeObjectListener
in interface IWorldView
objectId
- id of object you want the listener to remove fromlistener
- you want to removepublic void removeObjectListener(WorldObjectId objectId, Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
removeObjectListener
in interface IWorldView
objectId
- id of object you want the listener to remove fromeventClass
- event class you want to stop receiving in the listenerlistener
- you want to removepublic void removeListener(IWorldEventListener<?> listener)
IWorldView
WARNING: Can be time consuming! (Iterating through all levels of listeners.)
removeListener
in interface IWorldView
listener
- you want to remove from all listener levelspublic Map<Class,Map<WorldObjectId,IWorldObject>> getAll()
IWorldView
WARNING: If you will do iteration over the map, you must synchronize on it.
getAll
in interface IWorldView
public <T extends IWorldObject> Map<WorldObjectId,T> getAll(Class<T> type)
IWorldView
WARNING: If you will do iteration over the map, you must synchronize on it.
getAll
in interface IWorldView
public <T extends IWorldObject> T getSingle(Class<T> cls)
IWorldView
IllegalArgumentException
will be thrown.
Should be used to obtain "utility" world objects such us "informations about the agent" (that is unique for the agent and as the world view should be used by only one agent...) or some "world statistics object".
getSingle
in interface IWorldView
cls
- Class of object to be retrievedpublic IWorldObject get(WorldObjectId objectId)
IWorldView
Note that there is no way to tell the correct type of returned object - you have to cast it to a correct class yourself.
get
in interface IWorldView
objectId
- objects's idpublic <T extends IWorldObject> T get(WorldObjectId id, Class<T> clazz)
IWorldView
get
in interface IWorldView
id
- objects's idclazz
- the class of the object to be returnedpublic Map<WorldObjectId,IWorldObject> get()
IWorldView
WARNING: If you will do iteration over the map, you must synchronize on it.
Note that this map contains various objects, therefore you will somehow guess the correct class of the object from its id.
get
in interface IWorldView
protected void addWorldObject(IWorldObject worldObject)
Synchronized!
worldObject
- protected void removeWorldObject(IWorldObject worldObject)
Synchronized!
worldObject
- protected void raiseEvent(IWorldEvent event)
Use in the descendants to process new IWorldChangeEvent.
Does not catch any exceptions!
Synchronized!
event
- Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.