|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cz.cuni.amis.pogamut.base.communication.worldview.impl.AbstractWorldView
public abstract class AbstractWorldView
Abstract world view is implementing some of the tedious things every WorldView will surely implement -> maps for holding the references to all world objects either according to their id and type (class). It also implements a map of listeners for events that may be generated in the world.
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'.
Field Summary | |
---|---|
static Token |
COMPONENT_ID
|
protected IComponentControlHelper |
control
|
protected ComponentController<IComponent> |
controller
|
protected IComponentBus |
eventBus
|
protected LogCategory |
log
|
Constructor Summary | |
---|---|
AbstractWorldView(ComponentDependencies dependencies,
IComponentBus bus,
IAgentLogger logger)
|
Method Summary | ||
---|---|---|
void |
addEventListener(java.lang.Class<?> event,
IWorldEventListener<?> listener)
Adds listener to a specific event (Level A listeners). |
|
void |
addObjectListener(java.lang.Class<?> objectClass,
java.lang.Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Adds listener to a specified 'event' that occurs on the specific 'objectClass' (Level C listeners). |
|
void |
addObjectListener(java.lang.Class<?> objectClass,
IWorldObjectEventListener<?,?> listener)
Adds listener to all events that happens on any object of the 'objectClass' (Level B listeners). |
|
void |
addObjectListener(WorldObjectId objectId,
java.lang.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. |
|
java.util.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). |
|
java.util.Map<java.lang.Class,java.util.Map<WorldObjectId,IWorldObject>> |
getAll()
Returns map of all objects that are present in the world view. |
|
|
getAll(java.lang.Class<T> type)
Returns map of all objects of a specific type that are present in the world view. |
|
Token |
getComponentId()
Unique identification of the component. |
|
IComponentBus |
getEventBus()
|
|
LogCategory |
getLog()
|
|
|
getSingle(java.lang.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(java.lang.Class<?> objectClass,
java.lang.Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Tests whether the 'listener' is listening at specified 'objectClass' for specified 'event' (Level C listeners). |
|
boolean |
isListening(java.lang.Class<?> eventClass,
IWorldEventListener<?> listener)
Tests whether the 'listener' is listening to a specific event (Level A listeners). |
|
boolean |
isListening(java.lang.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,
java.lang.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(java.lang.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(java.lang.Class<?> objectClass,
java.lang.Class<?> eventClass,
IWorldObjectEventListener<?,?> listener)
Removes listener from specific 'objectClass' listening for specified 'event' (Level C listeners). |
|
void |
removeObjectListener(java.lang.Class<?> objectClass,
IWorldObjectEventListener<?,?> listener)
Removes listener from specific 'objectClass' listening for specified 'event' (Level B listeners). |
|
void |
removeObjectListener(WorldObjectId objectId,
java.lang.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. |
|
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface cz.cuni.amis.pogamut.base.communication.worldview.IWorldChangeEventInput |
---|
notify, notifyImmediately |
Field Detail |
---|
public static final Token COMPONENT_ID
protected LogCategory log
protected IComponentBus eventBus
protected ComponentController<IComponent> controller
protected IComponentControlHelper control
Constructor Detail |
---|
public AbstractWorldView(ComponentDependencies dependencies, IComponentBus bus, IAgentLogger logger)
Method Detail |
---|
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 Token getComponentId()
IComponent
getComponentId
in interface IComponent
public LogCategory getLog()
public IComponentBus getEventBus()
getEventBus
in interface IWorldView
public void addEventListener(java.lang.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(java.lang.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(java.lang.Class<?> objectClass, java.lang.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, java.lang.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(java.lang.Class<?> eventClass, IWorldEventListener<?> listener)
IWorldView
isListening
in interface IWorldView
eventClass
- which events you want to receivelistener
- that is tested
public boolean isListening(java.lang.Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectClass
- where the listener is testedlistener
- that is tested
public boolean isListening(java.lang.Class<?> objectClass, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectClass
- where the listener is testedeventClass
- where the listener is testedlistener
- that is tested
public boolean isListening(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectId
- where the listener is testedlistener
- that is tested
public boolean isListening(WorldObjectId objectId, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
IWorldView
isListening
in interface IWorldView
objectId
- where the listener is testedeventClass
- what class is testedlistener
- that is tested
public boolean isListening(IWorldEventListener<?> listener)
IWorldView
WARNING: Can be time consuming! (Iterating through all levels of listeners.)
isListening
in interface IWorldView
public void removeEventListener(java.lang.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(java.lang.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(java.lang.Class<?> objectClass, java.lang.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, java.lang.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 java.util.Map<java.lang.Class,java.util.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> java.util.Map<WorldObjectId,T> getAll(java.lang.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(java.lang.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 retrieved
public 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 id
public java.util.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
- public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |