Interface ContextCalculator
-
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface ContextCalculator
Calculates theContext
s applicable for aContextual
.Implementations of this interface should satisfy the following requirements:
- Context lookups should be fast: lookup methods are likely to be invoked frequently, and should therefore be fast to execute. If determining the current contexts involves a particularly time consuming lookup (database queries, network requests, etc), then such results should be cached ahead of time.
- Context lookups should be thread-safe: lookups will sometimes be performed from "async" threads, and therefore should not access any part of the server only safe for access from a sync context. If necessary, such results should be determined ahead of time and stored in a thread-safe collection for retrieval later.
- Context lookups should not query active contexts: doing so is
likely to result in a stack overflow, or thread deadlock. Care should be
taken to avoid (indirect) calls to
ContextService.contexts()
()}.
Calculators should be registered with the corresponding
ContextService
usingContextService.registerContextCalculator(ContextCalculator)
.Context lookups for instances provided by the platform, (e.g.
Player
) are delegated to the activeContextService
. Plugins wishing to provide contexts for these instances should register calculators here.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description void
accumulateContexts(Cause source, java.util.function.Consumer<Context> accumulator)
Adds anyContext
s this calculator determines to be applicable to thetarget
contextual.static ContextCalculator
forSingleContext(java.lang.String key, java.util.function.Function<Cause,java.lang.String> valueFunction)
Creates a newContextCalculator
that provides a single context.
-
-
-
Method Detail
-
forSingleContext
static ContextCalculator forSingleContext(java.lang.String key, java.util.function.Function<Cause,java.lang.String> valueFunction)
Creates a newContextCalculator
that provides a single context.- Parameters:
key
- The key of the context provided by the calculatorvalueFunction
- The function used to compute the corresponding value for each query. A context will not be "accumulated" if the value returned is null.- Returns:
- The resultant calculator
-
accumulateContexts
void accumulateContexts(Cause source, java.util.function.Consumer<Context> accumulator)
Adds anyContext
s this calculator determines to be applicable to thetarget
contextual.Care should be taken to ensure implementations of this method meet the general requirements for
ContextCalculator
, defined in the class doc.Calculators should not rely on the state of
accumulator
during this call, and also shouldn't make calls to remove contexts added by other calculators.- Parameters:
source
- The cause stack to draw from for this operationaccumulator
- aSet
ofContext
s this operation will accumulate to.
-
-