Package repast.simphony.context
Class AbstractContext<T>
- java.lang.Object
-
- java.util.AbstractCollection<T>
-
- repast.simphony.context.AbstractContext<T>
-
- All Implemented Interfaces:
Iterable<T>
,Collection<T>
,Context<T>
,ContextListener
,RepastElement
- Direct Known Subclasses:
AbstractSpatialContext
,SmallDefaultContext
public abstract class AbstractContext<T> extends AbstractCollection<T> implements Context<T>, ContextListener
Abstract base class implementation of theContext
interface.
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<Object,Context<? extends T>>
subContexts
-
Fields inherited from interface repast.simphony.context.Context
SYN_CONTEXT_PREFIX
-
-
Constructor Summary
Constructors Constructor Description AbstractContext()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
add(T o)
void
addContextListener(ContextListener<T> listener)
protected abstract boolean
addInternal(T o)
void
addProjection(Projection<? super T> projection)
Adds the specified Projection to this Context.void
addSubContext(Context<? extends T> context)
void
addValueLayer(ValueLayer valueLayer)
Adds the specified ValueLayer to this Context.void
clear()
boolean
contains(Object o)
protected abstract boolean
containsInternal(Object o)
void
eventOccured(ContextEvent ev)
Listener method for context events.Context
findContext(Object id)
This searches the specified context and its descendants for the context with the specified id.Context
findParent(Object o)
Retrieves the context of the specified object starting with the specified Context.protected void
fireAddContextEvent(T o)
protected void
fireRemoveEvent(Object o)
protected void
fireSubContextAdded(Context<? extends T> context)
protected void
fireSubContextRemoved(Context<? extends T> context)
Iterable<T>
getAgentLayer(Class<T> agentType)
Iterable<Class>
getAgentTypes()
Collection<ContextListener<T>>
getContextListeners()
Object
getId()
<X extends Projection<?>>
XgetProjection(Class<X> projection, String name)
Gets the named projection.Projection<?>
getProjection(String name)
Gets the named projection.Collection<Projection<?>>
getProjections()
Gets an iterable over all the projections contained by this Context.<X extends Projection<?>>
Iterable<X>getProjections(Class<X> clazz)
Gets all the projections in this Context of the specified type.Context<? extends T>
getSubContext(Object id)
Iterable<Context<? extends T>>
getSubContexts()
Object
getTypeID()
Gets an id that indentifies the user-defined type of this context.ValueLayer
getValueLayer(String name)
Gets the named ValueLayer.Collection<ValueLayer>
getValueLayers()
Gets an iterable over the ValueLayer-s contained by this Context.protected boolean
handleRemove(Object o)
boolean
hasSubContext()
Method to check if subcontext(s) are present in the contextIterator<T>
iterator()
By default the iterator returns in the contents in this context in the order entered then it returns the contents of the subcontexts in the order the subcontexts were added.protected abstract Iterator<T>
iteratorInternal()
Iterable<T>
query(org.apache.commons.collections15.Predicate query)
boolean
remove(Object o)
void
removeContextListener(ContextListener<T> listener)
protected abstract boolean
removeInternal(Object o)
Projection<? super T>
removeProjection(String projectionName)
Removes the named projection from this Context.void
removeSubContext(Context<? extends T> context)
ValueLayer
removeValueLayer(String name)
Removes the named ValueLayer from this Context.void
setId(Object id)
void
setTypeID(Object id)
Sets an id that indentifies the user-defined type of this context.int
size()
protected abstract int
sizeInternal()
-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Methods inherited from interface repast.simphony.context.Context
getObjects, getObjectsAsStream, getRandomObject, getRandomObjects, getRandomObjectsAsStream
-
-
-
-
Method Detail
-
getId
public Object getId()
- Specified by:
getId
in interfaceRepastElement
-
setId
public void setId(Object id)
- Specified by:
setId
in interfaceRepastElement
-
getTypeID
public Object getTypeID()
Gets an id that indentifies the user-defined type of this context. A context type typically refers to the role the context plays in a particular model. For example, the type of the context may be "School" and the agents in that context would be "Pupils".
-
setTypeID
public void setTypeID(Object id)
Sets an id that indentifies the user-defined type of this context. A context type typically refers to the role the context plays in a particular model. For example, the type of the context may be "School" and the agents in that context would be "Pupils".
-
addContextListener
public void addContextListener(ContextListener<T> listener)
- Specified by:
addContextListener
in interfaceContext<T>
-
getContextListeners
public Collection<ContextListener<T>> getContextListeners()
- Specified by:
getContextListeners
in interfaceContext<T>
-
removeContextListener
public void removeContextListener(ContextListener<T> listener)
- Specified by:
removeContextListener
in interfaceContext<T>
-
add
public final boolean add(T o)
- Specified by:
add
in interfaceCollection<T>
- Overrides:
add
in classAbstractCollection<T>
-
fireAddContextEvent
protected void fireAddContextEvent(T o)
- Parameters:
o
-
-
addInternal
protected abstract boolean addInternal(T o)
-
containsInternal
protected abstract boolean containsInternal(Object o)
-
contains
public boolean contains(Object o)
- Specified by:
contains
in interfaceCollection<T>
- Overrides:
contains
in classAbstractCollection<T>
-
iterator
public Iterator<T> iterator()
By default the iterator returns in the contents in this context in the order entered then it returns the contents of the subcontexts in the order the subcontexts were added.- Specified by:
iterator
in interfaceCollection<T>
- Specified by:
iterator
in interfaceIterable<T>
- Specified by:
iterator
in classAbstractCollection<T>
-
eventOccured
public void eventOccured(ContextEvent ev)
Listener method for context events. This is primiarly used to receive ContextEvents from a subcontext and pass them on to this context's own listeners.- Specified by:
eventOccured
in interfaceContextListener<T>
- Parameters:
ev
-
-
addSubContext
public void addSubContext(Context<? extends T> context)
- Specified by:
addSubContext
in interfaceContext<T>
-
hasSubContext
public boolean hasSubContext()
Description copied from interface:Context
Method to check if subcontext(s) are present in the context- Specified by:
hasSubContext
in interfaceContext<T>
- Returns:
- true if subcontext(s) are present and false if not
-
getSubContexts
public Iterable<Context<? extends T>> getSubContexts()
- Specified by:
getSubContexts
in interfaceContext<T>
-
removeSubContext
public void removeSubContext(Context<? extends T> context)
- Specified by:
removeSubContext
in interfaceContext<T>
-
remove
public final boolean remove(Object o)
- Specified by:
remove
in interfaceCollection<T>
- Overrides:
remove
in classAbstractCollection<T>
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection<T>
- Overrides:
clear
in classAbstractCollection<T>
-
handleRemove
protected boolean handleRemove(Object o)
-
fireRemoveEvent
protected void fireRemoveEvent(Object o)
-
removeInternal
protected abstract boolean removeInternal(Object o)
-
size
public int size()
- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in classAbstractCollection<T>
-
sizeInternal
protected abstract int sizeInternal()
-
getSubContext
public Context<? extends T> getSubContext(Object id)
- Specified by:
getSubContext
in interfaceContext<T>
-
findParent
public Context findParent(Object o)
Retrieves the context of the specified object starting with the specified Context. This will traverse into the specified context's sub contexts until it finds the lowest level context the object is in.- Specified by:
findParent
in interfaceContext<T>
- Parameters:
o
- an object to find in the contexts- Returns:
- the context that contains the object
-
findContext
public Context findContext(Object id)
This searches the specified context and its descendants for the context with the specified id. This searches through the contexts in a breadth-first manner, and will return the first context whose getId() method returns the specified id. This includes the start context.- Specified by:
findContext
in interfaceContext<T>
- Parameters:
id
- the id of the context to search for- Returns:
- the first context found with the given id, or null if no context is found
-
getAgentTypes
public Iterable<Class> getAgentTypes()
- Specified by:
getAgentTypes
in interfaceContext<T>
-
getAgentLayer
public Iterable<T> getAgentLayer(Class<T> agentType)
- Specified by:
getAgentLayer
in interfaceContext<T>
-
getProjection
public <X extends Projection<?>> X getProjection(Class<X> projection, String name)
Description copied from interface:Context
Gets the named projection. This does not query subcontexts.- Specified by:
getProjection
in interfaceContext<T>
- Parameters:
projection
- the type of the projectionname
- the name of the projection to get- Returns:
- the named projection.
-
getProjections
public <X extends Projection<?>> Iterable<X> getProjections(Class<X> clazz)
Description copied from interface:Context
Gets all the projections in this Context of the specified type. This does not query subcontexts.- Specified by:
getProjections
in interfaceContext<T>
- Parameters:
clazz
- the type of projections to get- Returns:
- all the projections in this Context of the specified type.
-
getProjection
public Projection<?> getProjection(String name)
Description copied from interface:Context
Gets the named projection. This does not search subcontexts.- Specified by:
getProjection
in interfaceContext<T>
- Parameters:
name
- the name of the projection to get- Returns:
- the named projection.
-
addProjection
public void addProjection(Projection<? super T> projection)
Description copied from interface:Context
Adds the specified Projection to this Context.- Specified by:
addProjection
in interfaceContext<T>
- Parameters:
projection
- the projection to add
-
removeProjection
public Projection<? super T> removeProjection(String projectionName)
Removes the named projection from this Context.- Specified by:
removeProjection
in interfaceContext<T>
- Parameters:
projectionName
- the name projection to remove- Returns:
- the removed projection.
-
getProjections
public Collection<Projection<?>> getProjections()
Gets an iterable over all the projections contained by this Context.- Specified by:
getProjections
in interfaceContext<T>
- Returns:
- an iterable over all the projections contained by this Context.
-
addValueLayer
public void addValueLayer(ValueLayer valueLayer)
Adds the specified ValueLayer to this Context.- Specified by:
addValueLayer
in interfaceContext<T>
- Parameters:
valueLayer
- the ValueLayer to add
-
removeValueLayer
public ValueLayer removeValueLayer(String name)
Removes the named ValueLayer from this Context.- Specified by:
removeValueLayer
in interfaceContext<T>
- Parameters:
name
- the name of the ValueLayer to remove.- Returns:
- the removed ValueLayer or null if the named ValueLayer was not found.
-
getValueLayers
public Collection<ValueLayer> getValueLayers()
Gets an iterable over the ValueLayer-s contained by this Context.- Specified by:
getValueLayers
in interfaceContext<T>
- Returns:
- an iterable over the ValueLayer-s contained by this Context.
-
getValueLayer
public ValueLayer getValueLayer(String name)
Gets the named ValueLayer. This does not search subcontexts.- Specified by:
getValueLayer
in interfaceContext<T>
- Parameters:
name
- the name of the ValueLayer to get- Returns:
- the named ValueLayer.
-
-