StdOutOutputBuilder.java

package io.jstach.rainbowgum.output;

import io.jstach.rainbowgum.LogProperties;
import io.jstach.rainbowgum.LogProperty;

/**
 * Builder to create {@link io.jstach.rainbowgum.LogProvider<io.jstach.rainbowgum.LogOutput> }.
 * Creates a standard out output.
 * <table class="table">
 * <caption><strong>Properties retrieved from LogProperties</strong></caption>
 * <tr>
 * <th>Property Pattern</th>
 * <th>Type</th>
 * <th>Required</th>
 * <th>Default</th>
 * <th>Description</th>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_stream }</code></td>
 * <td><code>String (converted)</code></td>
 * <td><code>true</code></td>
 * <td>{@link io.jstach.rainbowgum.output.ConsoleStream#CACHED cached}</td>
 * <td>{@link ConsoleStream#CACHED} (default) uses {@link System#out} as it is when the output is created, {@link ConsoleStream#FOLLOW} reads it on every write.</td>
 * </tr>
 * </table>
 */
public final class StdOutOutputBuilder implements io.jstach.rainbowgum.LogBuilder<StdOutOutputBuilder,io.jstach.rainbowgum.LogProvider<io.jstach.rainbowgum.LogOutput>> {

	/**
	 * The properties to be retrieved from config will have
	 * this prefix.
	 */
	static final String PROPERTY_PREFIX = "logging.appender.{name}.output.stdout.";
	
	/**
	 * <code>{@value #PROPERTY_stream } = io.jstach.rainbowgum.output.ConsoleStream</code> {@link ConsoleStream#CACHED} (default) uses {@link System#out} as it is when the output is created, {@link ConsoleStream#FOLLOW} reads it on every write.
	 */
	static final String PROPERTY_stream = PROPERTY_PREFIX + "stream";
	
	private final String propertyPrefix;
	final String property_stream;
	
	private final java.lang.String name;
	private io.jstach.rainbowgum.output.ConsoleStream stream = io.jstach.rainbowgum.output.ConsoleStream.CACHED;
	
	/**
	 * Create a builder for {@code io.jstach.rainbowgum.LogProvider<io.jstach.rainbowgum.LogOutput> }.
	 * @param name will fill <code>{name}</code> in <code>logging.appender.{name}.output.stdout.</code>. Output name, used for property lookup.
	 */
	public StdOutOutputBuilder(
			java.lang.String name
			) {
		try {
			java.util.Map<String,String> prefixParameters = java.util.Map.of(
					"name", name
				);
			this.propertyPrefix = LogProperties.interpolateKey(PROPERTY_PREFIX, prefixParameters);
			this.name = name;
			property_stream = LogProperties.interpolateKey(PROPERTY_stream, prefixParameters);
		}
		catch (IllegalArgumentException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}


	
	/**
	 * Sets <strong>required</strong> stream.
	 * {@link ConsoleStream#CACHED} (default) uses {@link System#out} as it is when the output is created, {@link ConsoleStream#FOLLOW} reads it on every write.
	 * Default is {@link io.jstach.rainbowgum.output.ConsoleStream#CACHED cached}.
	 * @param stream <code>{@value #PROPERTY_stream } = io.jstach.rainbowgum.output.ConsoleStream</code> {@link ConsoleStream#CACHED} (default) uses {@link System#out} as it is when the output is created, {@link ConsoleStream#FOLLOW} reads it on every write.
	 * @return this builder.
	 */
	public StdOutOutputBuilder stream(io.jstach.rainbowgum.output.ConsoleStream stream) {
		this.stream = stream;
		return this;
	}
	
	/**
	 * Creates {@code io.jstach.rainbowgum.LogProvider<io.jstach.rainbowgum.LogOutput> } from this builder.
	 * @return {@code io.jstach.rainbowgum.LogProvider<io.jstach.rainbowgum.LogOutput> }.
	 */
	public io.jstach.rainbowgum.LogProvider<io.jstach.rainbowgum.LogOutput> build() {
		try {
			return io.jstach.rainbowgum.output.ConsoleOutputs.stdout(
					this.name
					, io.jstach.rainbowgum.LogProperty.require(property_stream, this.stream)
					);
		}
		catch (IllegalArgumentException | LogProperty.PropertyMissingException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}
	
	@Override
	public StdOutOutputBuilder fromProperties(LogProperties properties) {
		var __v = LogProperty.Validator.of(this.getClass());
		var _stream = properties.forKey(property_stream).ofString().map(_v -> io.jstach.rainbowgum.output.ConsoleOutputs.parseStream(_v)).or(this.stream).validate(__v);
		__v.validate();
		this.stream = _stream.value();
		return this;
	}
	
	/**
	 * The interpolated property prefix: {@value #PROPERTY_PREFIX}.
	 * @return resolved prefix which should end with a "<code>.</code>".
	 */
	@Override
	public String propertyPrefix() {
		return this.propertyPrefix;
	}
	
}