All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wniemiec.io.java.StandardTerminalBuilder Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) William Niemiec.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package wniemiec.io.java;

import java.nio.file.Path;
import java.util.function.Consumer;


/**
 * Responsible for creating standard terminal.
 */
public class StandardTerminalBuilder {

    //-------------------------------------------------------------------------
    //		Attributes
    //-------------------------------------------------------------------------
    private static StandardTerminalBuilder instance;
    private InputTerminal inputTerminal;
    private OutputTerminal outputTerminal;
    private Path workingDirectory;
    private Consumer outputHandler;
    private Consumer outputErrorHandler;



    //-------------------------------------------------------------------------
    //		Constructor
    //-------------------------------------------------------------------------
    private StandardTerminalBuilder() {
    }


    //-------------------------------------------------------------------------
    //		Factory
    //-------------------------------------------------------------------------
    public static StandardTerminalBuilder getInstance() {
        if (instance == null) {
            instance = new StandardTerminalBuilder();
        }

        return instance;
    }
    

    //-------------------------------------------------------------------------
    //		Methods
    //-------------------------------------------------------------------------
    public StandardTerminalBuilder workingDirectory(Path workingDirectory) {
        this.workingDirectory = workingDirectory;

        return this;
    }

    public StandardTerminalBuilder outputHandler(Consumer outputHandler) {
        this.outputHandler = outputHandler;

        return this;
    }

    public StandardTerminalBuilder outputErrorHandler(Consumer outputErrorHandler) {
        this.outputErrorHandler = outputErrorHandler;

        return this;
    }

    public Terminal build() {
        buildOutputTerminal();
        buildInputTerminal();

        return buildTerminal();
    }

    private void buildOutputTerminal() {
        outputTerminal = new StandardOutputTerminal(outputHandler, outputErrorHandler);
    }

    private void buildInputTerminal() {
        inputTerminal = new StandardInputTerminal(workingDirectory, outputTerminal);
    }

    private Terminal buildTerminal() {
        return new Terminal(inputTerminal, outputTerminal);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy