LogbackJsonEncoderBuilder.java

package io.jstach.rainbowgum.json.encoder;

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

/**
 * Builder to create {@link io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder }.
 * Creates a Logback JSON encoder.
 * <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_prettyPrint }</code></td>
 * <td><code>Boolean</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>true</code> will pretty print the JSON, default is false.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_maxBufferSize }</code></td>
 * <td><code>Integer</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td>Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.</td>
 * </tr>
 * </table>
 */
public final class LogbackJsonEncoderBuilder implements io.jstach.rainbowgum.LogBuilder<LogbackJsonEncoderBuilder,io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder> {

	/**
	 * The properties to be retrieved from config will have
	 * this prefix.
	 */
	static final String PROPERTY_PREFIX = "logging.appender.{name}.encoder.logback.";
	
	/**
	 * <code>{@value #PROPERTY_prettyPrint } = java.lang.Boolean</code> <code>true</code> will pretty print the JSON, default is false.
	 */
	static final String PROPERTY_prettyPrint = PROPERTY_PREFIX + "prettyPrint";
	/**
	 * <code>{@value #PROPERTY_maxBufferSize } = java.lang.Integer</code> Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 */
	static final String PROPERTY_maxBufferSize = PROPERTY_PREFIX + "maxBufferSize";
	
	private final String propertyPrefix;
	final String property_prettyPrint;
	final String property_maxBufferSize;
	
	private final java.lang.String name;
	private java.lang.@org.jspecify.annotations.Nullable Boolean prettyPrint = null;
	private java.lang.@org.jspecify.annotations.Nullable Integer maxBufferSize = null;
	
	/**
	 * Create a builder for {@code io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder }.
	 * @param name will fill <code>{name}</code> in <code>logging.appender.{name}.encoder.logback.</code>. Property name prefix.
	 */
	public LogbackJsonEncoderBuilder(
			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_prettyPrint = LogProperties.interpolateKey(PROPERTY_prettyPrint, prefixParameters);
			property_maxBufferSize = LogProperties.interpolateKey(PROPERTY_maxBufferSize, prefixParameters);
		}
		catch (IllegalArgumentException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}


	
	/**
	 * Sets prettyPrint.
	 * <code>true</code> will pretty print the JSON, default is false.
	 * Default is <code>null</code>.
	 * @param prettyPrint <code>{@value #PROPERTY_prettyPrint } = java.lang.Boolean</code> <code>true</code> will pretty print the JSON, default is false.
	 * @return this builder.
	 */
	public LogbackJsonEncoderBuilder prettyPrint(java.lang.@org.jspecify.annotations.Nullable Boolean prettyPrint) {
		this.prettyPrint = prettyPrint;
		return this;
	}
	
	/**
	 * Sets maxBufferSize.
	 * Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 * Default is <code>null</code>.
	 * @param maxBufferSize <code>{@value #PROPERTY_maxBufferSize } = java.lang.Integer</code> Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 * @return this builder.
	 */
	public LogbackJsonEncoderBuilder maxBufferSize(java.lang.@org.jspecify.annotations.Nullable Integer maxBufferSize) {
		this.maxBufferSize = maxBufferSize;
		return this;
	}
	
	/**
	 * Creates {@code io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder } from this builder.
	 * @return {@code io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder }.
	 */
	public io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder build() {
		try {
			return io.jstach.rainbowgum.json.encoder.LogbackJsonEncoder.of(
					this.name
					, this.prettyPrint
					, this.maxBufferSize
					);
		}
		catch (IllegalArgumentException | LogProperty.PropertyMissingException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}
	
	@Override
	public LogbackJsonEncoderBuilder fromProperties(LogProperties properties) {
		var __v = LogProperty.Validator.of(this.getClass());
		var _prettyPrint = properties.forKey(property_prettyPrint).ofBoolean().or(this.prettyPrint).validateIfError(__v);
		var _maxBufferSize = properties.forKey(property_maxBufferSize).ofInt().or(this.maxBufferSize).validateIfError(__v);
		__v.validate();
		this.prettyPrint = _prettyPrint.valueOrNull();
		this.maxBufferSize = _maxBufferSize.valueOrNull();
		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;
	}
	
}