public abstract class BaseContext extends Object implements Iterable<BaseContext>
Specific implementations of contexts would normally add additional properties to the context to represent the state that is to be stored by that particular context implementation.
A context may also function as a container of subcontexts. Access to subcontexts is class-based. The parent context may hold only one instance of a given class at a given time. This class-based indexing approach is used to enforce type-safety over the subcontext instances returned from the parent context, and avoids the need for casting.
When a subcontext is requested and it does not exist in the parent context, it may optionally be auto-created. In order to be auto-created in this manner, the subcontext type MUST have a no-arg constructor. If the requested subcontext does not conform to this convention, auto-creation will fail.
Modifier and Type | Class and Description |
---|---|
protected class |
BaseContext.ContextSetNoRemoveIteratorDecorator
Iterator decorator which disallows the remove() operation on the iterator.
|
Constructor and Description |
---|
BaseContext()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addSubcontext(BaseContext subContext)
Add a subcontext to the current context.
|
void |
addSubcontext(BaseContext subcontext,
boolean replace)
Add a subcontext to the current context.
|
void |
clearSubcontexts()
Clear the subcontexts of the current context.
|
<T extends BaseContext> |
containsSubcontext(Class<T> clazz)
Return whether the current context currently contains an instance of
the specified subcontext class.
|
protected <T extends BaseContext> |
createSubcontext(Class<T> clazz)
Create an instance of the specified subcontext class.
|
BaseContext |
getParent()
Get the parent context, if there is one.
|
<T extends BaseContext> |
getSubcontext(Class<T> clazz)
Get a subcontext of the current context.
|
<T extends BaseContext> |
getSubcontext(Class<T> clazz,
boolean autocreate)
Get a subcontext of the current context.
|
BaseContext |
getSubcontext(String className)
Get a subcontext of the current context.
|
BaseContext |
getSubcontext(String className,
boolean autocreate)
Get a subcontext of the current context.
|
boolean |
isAutoCreateSubcontexts()
Get whether the context auto-creates subcontexts by default.
|
Iterator<BaseContext> |
iterator() |
void |
removeSubcontext(BaseContext subcontext)
Remove a subcontext from the current context.
|
<T extends BaseContext> |
removeSubcontext(Class<T> clazz)
Remove the subcontext from the current context which corresponds to the supplied class.
|
void |
setAutoCreateSubcontexts(boolean autoCreate)
Set whether the context auto-creates subcontexts by default.
|
protected void |
setParent(BaseContext newParent)
Set the context parent.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
@Nullable public BaseContext getParent()
protected void setParent(@Nullable BaseContext newParent)
newParent
- the new context parent@Nullable public <T extends BaseContext> T getSubcontext(@Nonnull Class<T> clazz)
T
- the type of subcontext being operated onclazz
- the class type to obtain@Nullable public <T extends BaseContext> T getSubcontext(@Nonnull Class<T> clazz, boolean autocreate)
T
- the type of subcontext being operated onclazz
- the class type to obtainautocreate
- flag indicating whether the subcontext instance should be auto-created@Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty String className) throws ClassNotFoundException
className
- the name of the class type to obtainClassNotFoundException
@Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty String className, boolean autocreate) throws ClassNotFoundException
className
- the name of the class type to obtainautocreate
- flag indicating whether the subcontext instance should be auto-createdClassNotFoundException
public void addSubcontext(@Nonnull BaseContext subContext)
subContext
- the subcontext to addpublic void addSubcontext(@Nonnull BaseContext subcontext, boolean replace)
subcontext
- the subcontext to addreplace
- flag indicating whether to replace the existing instance of the subcontext if presentpublic void removeSubcontext(@Nonnull BaseContext subcontext)
subcontext
- the subcontext to removepublic <T extends BaseContext> void removeSubcontext(@Nonnull Class<T> clazz)
T
- the type of subcontext being operated onclazz
- the subcontext class to removepublic <T extends BaseContext> boolean containsSubcontext(@Nonnull Class<T> clazz)
T
- the type of subcontext being operated onclazz
- the class to checkpublic void clearSubcontexts()
public boolean isAutoCreateSubcontexts()
public void setAutoCreateSubcontexts(boolean autoCreate)
autoCreate
- whether the context should auto-create subcontexts@Nonnull public Iterator<BaseContext> iterator()
iterator
in interface Iterable<BaseContext>
@Nonnull protected <T extends BaseContext> T createSubcontext(@Nonnull Class<T> clazz)
T
- the type of subcontextclazz
- the class of the subcontext instance to createCopyright © 2018. All rights reserved.