W - The type of SymbolWriter to create and manage.public class WriterDelegator<W extends SymbolWriter<W,? extends ImportContainer>> extends java.lang.Object implements SymbolDependencyContainer
Creates and manages SymbolWriters for files and namespaces based
on Symbols created for a Shape.
WriterDelegator is designed to generate code in files
returned by the Symbol.getDefinitionFile() method of a Symbol.
If multiple Symbols are created that need to be defined in the same
file, then the WriterDelegator ensures that the state of code
generator associated with the file is persisted and only written when all
shapes have been generated.
SymbolWriters are lazily created each time a new filename is
requested. If a SymbolWriter is already associated with a filename,
a newline (\n) is written to the file before providing access to the
SymbolWriter. All of the files and SymbolWriters stored in the
delegator are eventually written to the provided FileManifest when
the flushWriters() method is called.
This class is not thread-safe.
WriterDelegatorLanguage-specific code generators that utilize Symbol and
SymbolDependency should extend both
WriterDelegator and SymbolWriter to implement
language specific functionality. Extending these classes also makes it
easier to create new instances of them because they will be easier to work
with since generic types aren't needed in concrete implementations.
| Constructor and Description |
|---|
WriterDelegator(FileManifest fileManifest,
SymbolProvider symbolProvider,
SymbolWriter.Factory<W> factory) |
| Modifier and Type | Method and Description |
|---|---|
void |
flushWriters()
Writes each pending
SymbolWriter to the FileManifest. |
java.util.List<SymbolDependency> |
getDependencies()
Gets all of the dependencies that have been registered in writers
created by the
WriterDelegator. |
java.util.Map<java.lang.String,W> |
getWriters()
Returns an immutable
Map of created SymbolWriters. |
void |
setAutomaticSeparator(java.lang.String automaticSeparator)
Sets the automatic separator that is written to a
SymbolWriter
each time the writer is reused. |
void |
setInterceptors(java.util.List<? extends CodeInterceptor<? extends CodeSection,W>> interceptors)
Sets the list of code interceptors to registered with each newly created writer.
|
void |
useFileWriter(java.lang.String filename,
java.util.function.Consumer<W> writerConsumer)
Gets a previously created
SymbolWriter or creates a new one
if needed. |
void |
useFileWriter(java.lang.String filename,
java.lang.String namespace,
java.util.function.Consumer<W> writerConsumer)
Gets a previously created writer or creates a new one if needed.
|
void |
useShapeWriter(Shape shape,
java.util.function.Consumer<W> writerConsumer)
|
public WriterDelegator(FileManifest fileManifest, SymbolProvider symbolProvider, SymbolWriter.Factory<W> factory)
fileManifest - Where code is written when flushWriters() is called.symbolProvider - Maps Shape to Symbol to determine the "namespace" and file of a shape.factory - Factory used to create new SymbolWriters.public void setInterceptors(java.util.List<? extends CodeInterceptor<? extends CodeSection,W>> interceptors)
interceptors - Interceptors to register.public java.util.List<SymbolDependency> getDependencies()
WriterDelegator.
This method essentially just aggregates the results of calling
SymbolWriter.getDependencies() of each created writer into
a single array.
This method may be overridden as needed (for example, to add in some list of default dependencies or to inject other generative dependencies).
getDependencies in interface SymbolDependencyContainerpublic void flushWriters()
SymbolWriter to the FileManifest.
The toString method is called on each writer to generate
the code to write to the manifest.
This method clears out the managed SymbolWriters, meaning a
subsequent call to getWriters() will return an empty map.
This method may be overridden as needed.
public final java.util.Map<java.lang.String,W> getWriters()
Map of created SymbolWriters.
Each map key is the relative filename where the code will be written
in the FileManifest, and each map value is the associated
SymbolWriter of type T.
public final void useFileWriter(java.lang.String filename,
java.util.function.Consumer<W> writerConsumer)
SymbolWriter or creates a new one
if needed.
If a writer already exists, a newline is automatically appended to
the writer (either a newline or whatever value was set on
setAutomaticSeparator(java.lang.String)).
filename - Name of the file to create.writerConsumer - Consumer that is expected to write to the SymbolWriter.public final void useFileWriter(java.lang.String filename,
java.lang.String namespace,
java.util.function.Consumer<W> writerConsumer)
If a writer already exists, a newline is automatically appended to
the writer (either a newline or whatever value was set on
setAutomaticSeparator(java.lang.String)).
filename - Name of the file to create.namespace - Namespace associated with the file (or an empty string).writerConsumer - Consumer that is expected to write to the SymbolWriter.public void useShapeWriter(Shape shape, java.util.function.Consumer<W> writerConsumer)
Shape by converting the Shape
to a Symbol.
Any dependencies (i.e., SymbolDependency) required by the
Symbol are automatically registered with the writer.
Any imports required to declare the Symbol in code (i.e.,
SymbolReference.ContextOption.DECLARE) are automatically
registered with the writer.
If a writer already exists, a newline is automatically appended to
the writer (either a newline or whatever value was set on
setAutomaticSeparator(java.lang.String)).
This method may be overridden as needed.
shape - Shape to create the writer for.writerConsumer - Consumer that is expected to write to the SymbolWriter.public final void setAutomaticSeparator(java.lang.String automaticSeparator)
SymbolWriter
each time the writer is reused.
The default line separator is a newline ("\n"), but some implementations may wish to use an alternative value (e.g., "\r\n") or to disable the newline separator altogether by proving an empty string.
automaticSeparator - The non-null line separator to use.