Interface TokenSource<D,T>
- Type Parameters:
D- the type of source document identifiersT- the type of token types
- All Superinterfaces:
AutoCloseable,Closeable,LookaheadTokenSource<D,,T, Void> Supplier<Token<D,T>>
- All Known Implementing Classes:
TokenFilter
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Token sources must not return null or throw an exception to
indicate that there are no more tokens available. Instead, it must supply an
infinite number of special tokens that indicate the end of the input stream.
Instead of creating multiple such tokens, a token source may return multiple
references to the same token object. How the client recognizes end-of-stream
tokens is application-dependent; it is recommended that a distinguished token
type be defined for the purpose.
A class that implements this interface in addition to the superinterface
LookaheadTokenSource indicates that is uses no lookahead buffer, does
not require explicit transfer of lookahead data, and may be switched in and
out of a pipeline preemptively without causing interference.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault TokenSource<D, T> parallel(int capacity) Returns a secondary token source that forwards tokens from this token source asynchronously in a new worker thread.default VoidClears the internal lookahead buffer and return its contents.default TokenSource<D, T> removeTypes(Set<T> types) Returns a secondary token source that forwards all tokens from this token source that have none of the given types.default TokenSource<D, T> removeTypes(T... types) Returns a secondary token source that forwards all tokens from this token source that have none of the given types.default voidtakeOverLookahead(Void lookaheadData) Fills the internal lookahead buffer with data.default TokenSource<D, T> with(Function<? super TokenSource<D, T>, ? extends TokenSource<D, T>> processor) Deprecated, for removal: This API element is subject to removal in a future version.Methods inherited from interface eu.bandm.tools.lexic.LookaheadTokenSource
close, forgetLookahead
-
Method Details
-
relinquishLookahead
Clears the internal lookahead buffer and return its contents.This implementation does nothing.
- Specified by:
relinquishLookaheadin interfaceLookaheadTokenSource<D,T, Void> - Returns:
null
-
takeOverLookahead
Fills the internal lookahead buffer with data.The buffer must be empty before this method is invoked. After return, the given data will be consumed from the buffer before the usual input is considered.
This implementation does nothing.
- Specified by:
takeOverLookaheadin interfaceLookaheadTokenSource<D,T, Void> - Parameters:
lookaheadData- null, ignored
-
with
@Deprecated(forRemoval=true, since="1.1.1") default TokenSource<D,T> with(Function<? super TokenSource<D, T>, ? extends TokenSource<D, T>> processor) Deprecated, for removal: This API element is subject to removal in a future version.UseLookaheadTokenProcessorinstead.Creates a new token processor by applying a given factory function to this source.- Parameters:
processor- the factory function for creating a processor- Returns:
- a new token processor by applying the given factory function to this source
-
removeTypes
Returns a secondary token source that forwards all tokens from this token source that have none of the given types.- Specified by:
removeTypesin interfaceLookaheadTokenSource<D,T, Void> - Parameters:
types- the types of tokens to remove- Returns:
- a token source that forwards all tokens from this token source that have none of the given types
-
removeTypes
Returns a secondary token source that forwards all tokens from this token source that have none of the given types.- Specified by:
removeTypesin interfaceLookaheadTokenSource<D,T, Void> - Parameters:
types- the types of tokens to remove- Returns:
- a token source that forwards all tokens from this token source that have none of the given types
-
parallel
Returns a secondary token source that forwards tokens from this token source asynchronously in a new worker thread.Tokens may be produced in advance by the worker, up to a given capacity.
This construct is useful for high-throughput scenarios, because worker and consumer can be executed on different CPU cores. Because of asynchronous computation, it does not support dynamic changes to the token pipeline, such as
LookaheadTokenSource.transferLookahead(eu.bandm.tools.lexic.LookaheadTokenSource<?, ?, ? extends L>, eu.bandm.tools.lexic.LookaheadTokenSource<?, ?, ? super L>).The worker thread is started right away as a daemon thread. When the consumer ceases to consume tokens, the worker will fill the queue up to its capacity, and then block indefinitely. Calling the
LookaheadTokenSource.close()method of the parallel token source will interrupt the worker thread, and thus cause it to terminate as soon as possible. This is recommended for releasing thread-related resources.- Parameters:
capacity- the maximum number of tokens that may be produced in advance- Returns:
- a token source that forwards tokens from this token source asynchronously in a new worker thread
- Throws:
IllegalArgumentException- ifcapacityis not positive- Since:
- 1.1
- See Also:
-
LookaheadTokenProcessorinstead.