Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Introduce builder pattern for processing context

AuthorDaveJarvis <email>
Date2021-12-12 01:58:08 GMT-0800
Commit83e3de2c193480dda81c79b28921db6e3c5ae337
Parent48b6d4c
src/main/java/com/keenwrite/AppCommands.java
* editor's directory; {@code false} means to export only the
* actively edited file.
+ *
private void file_export_pdf( final Path theme, final boolean concat ) {
if( Typesetter.canRun() ) {
file_export( MARKDOWN_PLAIN );
}
- */
+ */
/**
src/main/java/com/keenwrite/MainPane.java
final ExportFormat format,
final Caret caret ) {
-
- return new ProcessorContext(
- mPreview,
- mActiveDefinitionEditor,
- inputPath,
- outputPath,
- format,
- mWorkspace,
- caret
- );
+ return ProcessorContext.builder()
+ .with( ProcessorContext.Mutator::setInputPath, inputPath )
+ .with( ProcessorContext.Mutator::setOutputPath, outputPath )
+ .with( ProcessorContext.Mutator::setExportFormat, format )
+ .with( ProcessorContext.Mutator::setHtmlPreview, mPreview )
+ .with( ProcessorContext.Mutator::setTextDefinition, mActiveDefinitionEditor )
+ .with( ProcessorContext.Mutator::setWorkspace, mWorkspace )
+ .with( ProcessorContext.Mutator::setCaret, caret )
+ .build();
}
src/main/java/com/keenwrite/cmdline/Arguments.java
import com.keenwrite.ExportFormat;
import com.keenwrite.processors.ProcessorContext;
+import com.keenwrite.processors.ProcessorContext.Mutator;
import picocli.CommandLine;
public ProcessorContext createProcessorContext() {
- return ProcessorContext.create(
- mFileInput.toPath(),
- mFileOutput.toPath(),
- ExportFormat.valueFrom(mFormatType, mFormatSubtype)
- );
+ final var format = ExportFormat.valueFrom( mFormatType, mFormatSubtype );
+ return ProcessorContext
+ .builder()
+ .with( Mutator::setInputPath, mFileInput )
+ .with( Mutator::setOutputPath, mFileOutput )
+ .with( Mutator::setExportFormat, format )
+ .build();
}
src/main/java/com/keenwrite/processors/ProcessorContext.java
import com.keenwrite.preferences.Workspace;
import com.keenwrite.preview.HtmlPreview;
+import com.keenwrite.util.GenericBuilder;
import javafx.beans.property.ObjectProperty;
+import java.io.File;
import java.nio.file.Path;
import java.util.Map;
*/
public final class ProcessorContext {
- private final HtmlPreview mHtmlPreview;
- private final ObjectProperty<TextDefinition> mTextDefinition;
- private final Path mInputPath;
- private final Path mOutputPath;
- private final Caret mCaret;
- private final ExportFormat mExportFormat;
- private final Workspace mWorkspace;
+
+ private final Mutator mMutator;
/**
* Creates a new context for use by the {@link ProcessorFactory} when
* instantiating new {@link Processor} instances. Although all the
* parameters are required, not all {@link Processor} instances will use
* all parameters.
- *
- * @param htmlPreview Where to display the final (HTML) output.
- * @param textDefinition Fully expanded interpolated strings.
- * @param inputPath Path to the document to process.
- * @param outputPath Fully qualified filename to use when exporting.
- * @param exportFormat Indicate configuration options for export format.
+ */
+ private ProcessorContext( final Mutator mutator ) {
+ assert mutator != null;
+
+ mMutator = mutator;
+ }
+
+ public static class Mutator {
+ private HtmlPreview mHtmlPreview;
+ private ObjectProperty<TextDefinition> mTextDefinition;
+ private Path mInputPath;
+ private Path mOutputPath;
+ private Caret mCaret;
+ private ExportFormat mExportFormat;
+ private Workspace mWorkspace;
+
+ public void setHtmlPreview( final HtmlPreview htmlPreview ) {
+ mHtmlPreview = htmlPreview;
+ }
+
+ public void setTextDefinition( final ObjectProperty<TextDefinition> textDefinition ) {
+ mTextDefinition = textDefinition;
+ }
+
+ public void setInputPath( final Path inputPath ) {
+ mInputPath = inputPath;
+ }
+
+ public void setInputPath( final File inputPath ) {
+ setInputPath( inputPath.toPath() );
+ }
+
+ public void setOutputPath( final Path outputPath ) {
+ mOutputPath = outputPath;
+ }
+
+ public void setOutputPath( final File outputPath ) {
+ setOutputPath( outputPath.toPath() );
+ }
+
+ public void setCaret( final Caret caret ) {
+ mCaret = caret;
+ }
+
+ public void setExportFormat( final ExportFormat exportFormat ) {
+ mExportFormat = exportFormat;
+ }
+
+ public void setWorkspace( final Workspace workspace ) {
+ mWorkspace = workspace;
+ }
+ }
+
+ public static GenericBuilder<Mutator, ProcessorContext> builder() {
+ return GenericBuilder.of(
+ Mutator::new,
+ ProcessorContext::new
+ );
+ }
+
+ /**
+ * @param preview Where to display the final (HTML) output.
+ * @param textDefinition Source for fully expanded interpolated strings.
* @param workspace Persistent user preferences settings.
* @param caret Location of the caret in the edited document,
* which is used to synchronize the scrollbars.
+ * @param inputPath Path to the document to process.
+ * @param format Indicate configuration options for export format.
+ * @return A context that may be used for processing documents.
*/
- public ProcessorContext(
- final HtmlPreview htmlPreview,
+ public static ProcessorContext create(
+ final HtmlPreview preview,
final ObjectProperty<TextDefinition> textDefinition,
final Path inputPath,
final Path outputPath,
- final ExportFormat exportFormat,
+ final ExportFormat format,
final Workspace workspace,
final Caret caret ) {
- assert htmlPreview != null;
- assert textDefinition != null;
- assert inputPath != null;
- assert exportFormat != null;
- assert workspace != null;
- assert caret != null;
-
- mHtmlPreview = htmlPreview;
- mTextDefinition = textDefinition;
- mInputPath = inputPath;
- mOutputPath = outputPath;
- mExportFormat = exportFormat;
- mWorkspace = workspace;
- mCaret = caret;
+ return builder()
+ .with( Mutator::setInputPath, inputPath )
+ .with( Mutator::setOutputPath, outputPath )
+ .with( Mutator::setExportFormat, format )
+ .with( Mutator::setHtmlPreview, preview )
+ .with( Mutator::setTextDefinition, textDefinition )
+ .with( Mutator::setWorkspace, workspace )
+ .with( Mutator::setCaret, caret )
+ .build();
}
- public static ProcessorContext create( final Path inputPath, final ExportFormat format ) {
- return new ProcessorContext( null, null, inputPath, null, format, null, null );
+ /**
+ * @param inputPath Path to the document to process.
+ * @param format Indicate configuration options for export format.
+ * @return A context that may be used for processing documents.
+ */
+ public static ProcessorContext create(
+ final Path inputPath,
+ final ExportFormat format ) {
+ return builder()
+ .with( Mutator::setInputPath, inputPath )
+ .with( Mutator::setExportFormat, format )
+ .build();
}
+ /**
+ * @param inputPath Path to the document to process.
+ * @param outputPath Fully qualified filename to use when exporting.
+ * @param format Indicate configuration options for export format.
+ * @return A context that may be used for processing documents.
+ */
public static ProcessorContext create(
final Path inputPath, final Path outputPath, final ExportFormat format ) {
- return new ProcessorContext( null, null, inputPath, outputPath, format, null, null );
+ return builder()
+ .with( Mutator::setInputPath, inputPath )
+ .with( Mutator::setOutputPath, outputPath )
+ .with( Mutator::setExportFormat, format )
+ .build();
}
public boolean isExportFormat( final ExportFormat format ) {
- return mExportFormat == format;
+ return mMutator.mExportFormat == format;
}
HtmlPreview getPreview() {
- return mHtmlPreview;
+ return mMutator.mHtmlPreview;
}
/**
* Returns the variable map of interpolated definitions.
*
* @return A map to help dereference variables.
*/
Map<String, String> getResolvedMap() {
- return mTextDefinition.get().getDefinitions();
+ return mMutator.mTextDefinition.get().getDefinitions();
}
/**
* Fully qualified file name to use when exporting (e.g., document.pdf).
*
* @return Full path to a file name.
*/
public Path getOutputPath() {
- return mOutputPath;
+ return mMutator.mOutputPath;
}
public ExportFormat getExportFormat() {
- return mExportFormat;
+ return mMutator.mExportFormat;
}
/**
* Returns the current caret position in the document being edited and is
* always up-to-date.
*
* @return Caret position in the document.
*/
public Caret getCaret() {
- return mCaret;
+ return mMutator.mCaret;
}
public Path getDocumentPath() {
- return mInputPath;
+ return mMutator.mInputPath;
}
FileType getFileType() {
return lookup( getDocumentPath() );
}
public Workspace getWorkspace() {
- return mWorkspace;
+ return mMutator.mWorkspace;
}
}
Delta132 lines added, 57 lines removed, 75-line increase