BatchSwapAsyncLogPublisherBuilder.java

package io.jstach.rainbowgum.publisher;

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

/**
 * Builder to create {@link io.jstach.rainbowgum.LogPublisher.PublisherFactory }.
 * Creates a publisher factory with validated options.
 * <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_bufferSize }</code></td>
 * <td><code>Integer</code></td>
 * <td><code>true</code></td>
 * <td>{@value io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher#DEFAULT_BUFFER_SIZE }</td>
 * <td>Positive pending capacity, excluding the batch being written.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_shutdownTimeout }</code></td>
 * <td><code>String (converted)</code></td>
 * <td><code>true</code></td>
 * <td>{@link io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher#DEFAULT_SHUTDOWN_TIMEOUT }</td>
 * <td>Nonnegative close timeout (property unit: milliseconds).</td>
 * </tr>
 * </table>
 */
public final class BatchSwapAsyncLogPublisherBuilder implements io.jstach.rainbowgum.LogBuilder<BatchSwapAsyncLogPublisherBuilder,io.jstach.rainbowgum.LogPublisher.PublisherFactory> {

	/**
	 * The properties to be retrieved from config will have
	 * this prefix.
	 */
	static final String PROPERTY_PREFIX = "logging.route.{name}.publisher.{scheme}.";
	
	/**
	 * <code>{@value #PROPERTY_bufferSize } = java.lang.Integer</code> Positive pending capacity, excluding the batch being written.
	 */
	static final String PROPERTY_bufferSize = PROPERTY_PREFIX + "bufferSize";
	/**
	 * <code>{@value #PROPERTY_shutdownTimeout } = java.time.Duration</code> Nonnegative close timeout (property unit: milliseconds).
	 */
	static final String PROPERTY_shutdownTimeout = PROPERTY_PREFIX + "shutdownTimeout";
	
	private final String propertyPrefix;
	final String property_bufferSize;
	final String property_shutdownTimeout;
	
	private final java.lang.String name;
	private final java.lang.String scheme;
	private java.lang.Integer bufferSize = io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher.DEFAULT_BUFFER_SIZE;
	private java.time.Duration shutdownTimeout = io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher.DEFAULT_SHUTDOWN_TIMEOUT;
	
	/**
	 * Create a builder for {@code io.jstach.rainbowgum.LogPublisher.PublisherFactory }.
	 * @param name will fill <code>{name}</code> in <code>logging.route.{name}.publisher.{scheme}.</code>. Publisher name used for property lookup.
	 * @param scheme will fill <code>{scheme}</code> in <code>logging.route.{name}.publisher.{scheme}.</code>. URI scheme the publisher was selected with, used for property lookup.
	 */
	public BatchSwapAsyncLogPublisherBuilder(
			java.lang.String name
			, java.lang.String scheme
			) {
		try {
			java.util.Map<String,String> prefixParameters = java.util.Map.of(
					"name", name
					, "scheme", scheme
				);
			this.propertyPrefix = LogProperties.interpolateKey(PROPERTY_PREFIX, prefixParameters);
			this.name = name;
			this.scheme = scheme;
			property_bufferSize = LogProperties.interpolateKey(PROPERTY_bufferSize, prefixParameters);
			property_shutdownTimeout = LogProperties.interpolateKey(PROPERTY_shutdownTimeout, prefixParameters);
		}
		catch (IllegalArgumentException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}


	
	/**
	 * Sets <strong>required</strong> bufferSize.
	 * Positive pending capacity, excluding the batch being written.
	 * Default is {@value io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher#DEFAULT_BUFFER_SIZE }.
	 * @param bufferSize <code>{@value #PROPERTY_bufferSize } = java.lang.Integer</code> Positive pending capacity, excluding the batch being written.
	 * @return this builder.
	 */
	public BatchSwapAsyncLogPublisherBuilder bufferSize(java.lang.Integer bufferSize) {
		this.bufferSize = bufferSize;
		return this;
	}
	
	/**
	 * Sets <strong>required</strong> shutdownTimeout.
	 * Nonnegative close timeout (property unit: milliseconds).
	 * Default is {@link io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher#DEFAULT_SHUTDOWN_TIMEOUT }.
	 * @param shutdownTimeout <code>{@value #PROPERTY_shutdownTimeout } = java.time.Duration</code> Nonnegative close timeout (property unit: milliseconds).
	 * @return this builder.
	 */
	public BatchSwapAsyncLogPublisherBuilder shutdownTimeout(java.time.Duration shutdownTimeout) {
		this.shutdownTimeout = shutdownTimeout;
		return this;
	}
	
	/**
	 * Creates {@code io.jstach.rainbowgum.LogPublisher.PublisherFactory } from this builder.
	 * @return {@code io.jstach.rainbowgum.LogPublisher.PublisherFactory }.
	 */
	public io.jstach.rainbowgum.LogPublisher.PublisherFactory build() {
		try {
			return io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher.of(
					this.name
					, this.scheme
					, io.jstach.rainbowgum.LogProperty.require(property_bufferSize, this.bufferSize)
					, io.jstach.rainbowgum.LogProperty.require(property_shutdownTimeout, this.shutdownTimeout)
					);
		}
		catch (IllegalArgumentException | LogProperty.PropertyMissingException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}
	
	@Override
	public BatchSwapAsyncLogPublisherBuilder fromProperties(LogProperties properties) {
		var __v = LogProperty.Validator.of(this.getClass());
		var _bufferSize = properties.forKey(property_bufferSize).ofInt().or(this.bufferSize).validate(__v);
		var _shutdownTimeout = properties.forKey(property_shutdownTimeout).ofString().map(_v -> io.jstach.rainbowgum.publisher.BatchSwapAsyncLogPublisher.parseShutdownTimeout(_v)).or(this.shutdownTimeout).validate(__v);
		__v.validate();
		this.bufferSize = _bufferSize.value();
		this.shutdownTimeout = _shutdownTimeout.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;
	}
	
}