- java.lang.Object
-
- org.praxislive.code.CodeContext<D>
-
- Type Parameters:
D
- supported delegate type
- Direct Known Subclasses:
AudioCodeContext
,CodeContainer.Context
,CodeRoot.Context
,CoreCodeContext
,P2DCodeContext
,P3DCodeContext
,VideoCodeContext
public abstract class CodeContext<D extends CodeDelegate> extends Object
A CodeContext wraps eachCodeDelegate
, managing state and the transition from one iteration of delegate to the next on behalf of aCodeComponent
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CodeContext.ClockListener
Listener for responding to time changes inside the context.
-
Constructor Summary
Constructors Modifier Constructor Description protected
CodeContext(CodeConnector<D> connector)
Create a CodeContext by processing the providedCodeConnector
(containing CodeDelegate).protected
CodeContext(CodeConnector<D> connector, boolean requireClock)
Create a CodeContext by processing the providedCodeConnector
(containing CodeDelegate).
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addClockListener(CodeContext.ClockListener listener)
Add a clock listener.protected boolean
checkActive()
Check whether the CodeContext is running inside an ExecutionContext with active state.protected void
configure(CodeComponent<D> cmp, CodeContext<D> oldCtxt)
A hook method that will be called when the CodeContext is configured on a component.protected void
dispose()
Hook called during disposal of code context.protected void
flush()
Flush the code context.protected ControlAddress
getAddress(Control control)
Find the address of the passed in control, or null if it does not have one.CodeComponent<D>
getComponent()
Get the code component this code context is attached to, if there is one.protected Control
getControl(String id)
Get the control to handle the specified ID, or null if there isn't one.protected ControlDescriptor
getControlDescriptor(String id)
Get the control descriptor for the specified ID, or null if there isn't one.protected String[]
getControlIDs()
Deprecated.D
getDelegate()
Get the delegate this context wraps.protected ExecutionContext
getExecutionContext()
Get the execution context, or null if not attached.protected ComponentInfo
getInfo()
Get component info.LogBuilder
getLog()
Get the log builder for writing log messages.protected LogLevel
getLogLevel()
Get the active log level.Lookup
getLookup()
Get lookup.protected Port
getPort(String id)
Get the port with the specified ID, or null if there isn't one.protected PortDescriptor
getPortDescriptor(String id)
Get the port descriptor for the specified ID, or null if there isn't one.protected String[]
getPortIDs()
Deprecated.long
getTime()
Get current time in nanoseconds.protected void
hierarchyChanged()
Called when the hierarchy changes, which might be because the component hierarchy has changed (seeComponent.hierarchyChanged()
), the context has been added or is being removed from the component, or for any other reason that cached information should be invalidated (eg.void
invoke(long time, Runnable task)
Invoke the provided task, if the context is active, and after updating the clock to the specified time (if later).<V> V
invokeCallable(long time, Callable<V> task)
Invoke the provided task and return the result, if the context is active, and after updating the clock to the specified time (if later).Optional<ComponentAddress>
locateService(Class<? extends Service> type)
Locate the provided service type, if available.protected void
log(LogBuilder log)
Process and send messages from an external log builder.void
removeClockListener(CodeContext.ClockListener listener)
Remove a clock listener.protected void
reset(boolean full)
Reset all control, port and reference descriptors.protected void
starting(ExecutionContext source)
Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE
) or the context is added to a component within an active execution context.protected void
starting(ExecutionContext source, boolean fullStart)
Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE
) or the context is added to a component within an active execution context.protected void
stopping(ExecutionContext source)
Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE
) or the context is removed from a component within an active execution context.protected void
stopping(ExecutionContext source, boolean fullStop)
Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE
) or the context is removed from a component within an active execution context.protected void
tick(ExecutionContext source)
Hook called by the clock listener on the execution context.protected void
update(long time)
Update the time in this context to the specified time.
-
-
-
Constructor Detail
-
CodeContext
protected CodeContext(CodeConnector<D> connector)
Create a CodeContext by processing the providedCodeConnector
(containing CodeDelegate).- Parameters:
connector
- code connector with delegate
-
CodeContext
protected CodeContext(CodeConnector<D> connector, boolean requireClock)
Create a CodeContext by processing the providedCodeConnector
(containing CodeDelegate). This constructor takes a boolean to force connecting the context to the execution clock, should the subtype always require clock signals.- Parameters:
connector
- code connector with delegaterequireClock
- true to force clock connection
-
-
Method Detail
-
configure
protected void configure(CodeComponent<D> cmp, CodeContext<D> oldCtxt)
A hook method that will be called when the CodeContext is configured on a component. It is called after controls, ports and refs have been configured. Subclasses may override this to do additional configuration. The default implementation does nothing.- Parameters:
cmp
- component being attached tooldCtxt
- previous context, or null if there was none
-
hierarchyChanged
protected void hierarchyChanged()
Called when the hierarchy changes, which might be because the component hierarchy has changed (seeComponent.hierarchyChanged()
), the context has been added or is being removed from the component, or for any other reason that cached information should be invalidated (eg. anything retrieved from the lookup). Subclasses may override this to handle such events / invalidate lookup results. The default implementation does nothing.
-
starting
protected void starting(ExecutionContext source, boolean fullStart)
Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE
) or the context is added to a component within an active execution context. Full start will be true in the former case when the execution context itself is changing state.This method may be overridden in subclasses. The default implementation delegates to
starting(org.praxislive.core.ExecutionContext)
.- Parameters:
source
- execution contextfullStart
- whether the context itself is transitioning state
-
starting
protected void starting(ExecutionContext source)
Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE
) or the context is added to a component within an active execution context.- Parameters:
source
- execution context
-
stopping
protected void stopping(ExecutionContext source, boolean fullStop)
Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE
) or the context is removed from a component within an active execution context. Full stop will be true in the former case when the execution context itself is changing state.This method may be overridden in subclasses. The default implementation delegates to
stopping(org.praxislive.core.ExecutionContext)
.- Parameters:
source
- execution contextfullStop
- whether the context itself is transitioning state
-
stopping
protected void stopping(ExecutionContext source)
Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE
) or the context is removed from a component within an active execution context.- Parameters:
source
- execution context
-
tick
protected void tick(ExecutionContext source)
Hook called by the clock listener on the execution context. The default implementation does nothing.- Parameters:
source
- execution context
-
reset
protected final void reset(boolean full)
Reset all control, port and reference descriptors. A full reset generally happens on execution context state changes as opposed to code change transitions. Descriptors may handle this differently - eg. clear injected values or dispose references on full.- Parameters:
full
- whether reset is full (eg. execution state change)
-
dispose
protected void dispose()
Hook called during disposal of code context. The default implementation does nothing.
-
getComponent
public CodeComponent<D> getComponent()
Get the code component this code context is attached to, if there is one.- Returns:
- code component, or null
-
getDelegate
public D getDelegate()
Get the delegate this context wraps.- Returns:
- delegate
-
getControl
protected Control getControl(String id)
Get the control to handle the specified ID, or null if there isn't one.- Parameters:
id
- control ID- Returns:
- control or null
-
getControlDescriptor
protected ControlDescriptor getControlDescriptor(String id)
Get the control descriptor for the specified ID, or null if there isn't one.- Parameters:
id
- control ID- Returns:
- control descriptor or null
-
getControlIDs
@Deprecated protected String[] getControlIDs()
Deprecated.Get all the available control IDs.- Returns:
- control IDs
-
getPort
protected Port getPort(String id)
Get the port with the specified ID, or null if there isn't one.- Parameters:
id
- port ID- Returns:
- port or null
-
getPortDescriptor
protected PortDescriptor getPortDescriptor(String id)
Get the port descriptor for the specified ID, or null if there isn't one.- Parameters:
id
- port ID- Returns:
- port descriptor or null
-
getPortIDs
@Deprecated protected String[] getPortIDs()
Deprecated.Get the available port IDs.- Returns:
- port IDs
-
getInfo
protected ComponentInfo getInfo()
Get component info.- Returns:
- component info
-
getAddress
protected ControlAddress getAddress(Control control)
Find the address of the passed in control, or null if it does not have one.- Parameters:
control
- control to find address for- Returns:
- control address or null
-
getLookup
public Lookup getLookup()
Get lookup.- Returns:
- lookup
-
locateService
public Optional<ComponentAddress> locateService(Class<? extends Service> type)
Locate the provided service type, if available.- Parameters:
type
- service to lookup- Returns:
- optional service address
-
getTime
public long getTime()
Get current time in nanoseconds.- Returns:
- time in nanoseconds
-
addClockListener
public void addClockListener(CodeContext.ClockListener listener)
Add a clock listener. Resources used inside code delegates should add a clock listener rather than listen directly on the execution context.- Parameters:
listener
- clock listener
-
removeClockListener
public void removeClockListener(CodeContext.ClockListener listener)
Remove a clock listener.- Parameters:
listener
- to remove
-
getExecutionContext
protected ExecutionContext getExecutionContext()
Get the execution context, or null if not attached.- Returns:
- execution context, or null
-
checkActive
protected boolean checkActive()
Check whether the CodeContext is running inside an ExecutionContext with active state. If the execution context is active, but a transition to active has not yet been handled in this code context, the state transition will be triggered.- Returns:
- true if active
-
update
protected void update(long time)
Update the time in this context to the specified time. A value the same or behind the current value will be ignored. This method will call all clock listeners.- Parameters:
time
- updated time
-
invoke
public void invoke(long time, Runnable task)
Invoke the provided task, if the context is active, and after updating the clock to the specified time (if later). Any exception will be caught and logged, and the context will be flushed.- Parameters:
time
- new clock timetask
- runnable task to execute
-
invokeCallable
public <V> V invokeCallable(long time, Callable<V> task) throws Exception
Invoke the provided task and return the result, if the context is active, and after updating the clock to the specified time (if later). Any exception will be logged and rethrown, and the context flushed. Throws anIllegalStateException
ifcheckActive()
returnsfalse
.- Type Parameters:
V
- the result type of method call- Parameters:
time
- new clock timetask
- runnable task to execute- Returns:
- result
- Throws:
Exception
- if unable to compute a result
-
flush
protected void flush()
Flush the code context. By default this message checks for pending log messages and delivers to the log.
-
getLog
public LogBuilder getLog()
Get the log builder for writing log messages.- Returns:
- log builder
-
getLogLevel
protected LogLevel getLogLevel()
Get the active log level.- Returns:
- active log level
-
log
protected void log(LogBuilder log)
Process and send messages from an external log builder.- Parameters:
log
- external log builder
-
-