PatternConfigBuilder.java

package io.jstach.rainbowgum.pattern.format;

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

/**
 * Builder to create {@link io.jstach.rainbowgum.pattern.format.PatternConfig }.
 * <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_zoneId }</code></td>
 * <td><code>String (converted)</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td></td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_lineSeparator }</code></td>
 * <td><code>String</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td></td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_ansiDisabled }</code></td>
 * <td><code>Boolean</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td></td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_sequenceNumberStart }</code></td>
 * <td><code>Long</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td></td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_abbreviatorCache }</code></td>
 * <td><code>String (converted)</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td></td>
 * </tr>
 * </table>
 */
public final class PatternConfigBuilder implements io.jstach.rainbowgum.LogBuilder<PatternConfigBuilder,io.jstach.rainbowgum.pattern.format.PatternConfig> {

	/**
	 * The properties to be retrieved from config will have
	 * this prefix.
	 */
	static final String PROPERTY_PREFIX = "logging.pattern.config.{name}.";
	
	/**
	 * <code>{@value #PROPERTY_zoneId } = java.time.ZoneId</code> 
	 */
	static final String PROPERTY_zoneId = PROPERTY_PREFIX + "zoneId";
	/**
	 * <code>{@value #PROPERTY_lineSeparator } = java.lang.String</code> 
	 */
	static final String PROPERTY_lineSeparator = PROPERTY_PREFIX + "lineSeparator";
	/**
	 * <code>{@value #PROPERTY_ansiDisabled } = java.lang.Boolean</code> 
	 */
	static final String PROPERTY_ansiDisabled = PROPERTY_PREFIX + "ansiDisabled";
	/**
	 * <code>{@value #PROPERTY_sequenceNumberStart } = java.lang.Long</code> 
	 */
	static final String PROPERTY_sequenceNumberStart = PROPERTY_PREFIX + "sequenceNumberStart";
	/**
	 * <code>{@value #PROPERTY_abbreviatorCache } = io.jstach.rainbowgum.pattern.format.PatternConfig.CacheType</code> 
	 */
	static final String PROPERTY_abbreviatorCache = PROPERTY_PREFIX + "abbreviatorCache";
	
	private final String propertyPrefix;
	final String property_zoneId;
	final String property_lineSeparator;
	final String property_ansiDisabled;
	final String property_sequenceNumberStart;
	final String property_abbreviatorCache;
	
	private final java.lang.String name;
	private java.time.@org.jspecify.annotations.Nullable ZoneId zoneId = null;
	private java.lang.@org.jspecify.annotations.Nullable String lineSeparator = null;
	private java.lang.@org.jspecify.annotations.Nullable Boolean ansiDisabled = null;
	private java.util.function.@org.jspecify.annotations.Nullable Function<java.lang.String, java.lang.@org.jspecify.annotations.Nullable String> propertyFunction;
	private java.time.@org.jspecify.annotations.Nullable Instant startTime;
	private java.lang.@org.jspecify.annotations.Nullable Long sequenceNumberStart = null;
	private io.jstach.rainbowgum.pattern.format.PatternConfig.@org.jspecify.annotations.Nullable CacheType abbreviatorCache = null;
	
	/**
	 * Create a builder for {@code io.jstach.rainbowgum.pattern.format.PatternConfig }.
	 * @param name will fill <code>{name}</code> in <code>logging.pattern.config.{name}.</code>. 
	 */
	public PatternConfigBuilder(
			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_zoneId = LogProperties.interpolateKey(PROPERTY_zoneId, prefixParameters);
			property_lineSeparator = LogProperties.interpolateKey(PROPERTY_lineSeparator, prefixParameters);
			property_ansiDisabled = LogProperties.interpolateKey(PROPERTY_ansiDisabled, prefixParameters);
			property_sequenceNumberStart = LogProperties.interpolateKey(PROPERTY_sequenceNumberStart, prefixParameters);
			property_abbreviatorCache = LogProperties.interpolateKey(PROPERTY_abbreviatorCache, prefixParameters);
		}
		catch (IllegalArgumentException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}


	/**
	 * Sets propertyFunction.
	 * 
	 * Default is <code>null</code>.
	 * @param propertyFunction not configurable through properties 
	 * @return this builder.
	 */
	public PatternConfigBuilder propertyFunction(java.util.function.@org.jspecify.annotations.Nullable Function<java.lang.String, java.lang.@org.jspecify.annotations.Nullable String> propertyFunction) {
		this.propertyFunction = propertyFunction;
		return this;
	}

	/**
	 * Sets startTime.
	 * 
	 * Default is <code>null</code>.
	 * @param startTime not configurable through properties 
	 * @return this builder.
	 */
	public PatternConfigBuilder startTime(java.time.@org.jspecify.annotations.Nullable Instant startTime) {
		this.startTime = startTime;
		return this;
	}

	
	/**
	 * Sets zoneId.
	 * 
	 * Default is <code>null</code>.
	 * @param zoneId <code>{@value #PROPERTY_zoneId } = java.time.ZoneId</code> 
	 * @return this builder.
	 */
	public PatternConfigBuilder zoneId(java.time.@org.jspecify.annotations.Nullable ZoneId zoneId) {
		this.zoneId = zoneId;
		return this;
	}
	
	/**
	 * Sets lineSeparator.
	 * 
	 * Default is <code>null</code>.
	 * @param lineSeparator <code>{@value #PROPERTY_lineSeparator } = java.lang.String</code> 
	 * @return this builder.
	 */
	public PatternConfigBuilder lineSeparator(java.lang.@org.jspecify.annotations.Nullable String lineSeparator) {
		this.lineSeparator = lineSeparator;
		return this;
	}
	
	/**
	 * Sets ansiDisabled.
	 * 
	 * Default is <code>null</code>.
	 * @param ansiDisabled <code>{@value #PROPERTY_ansiDisabled } = java.lang.Boolean</code> 
	 * @return this builder.
	 */
	public PatternConfigBuilder ansiDisabled(java.lang.@org.jspecify.annotations.Nullable Boolean ansiDisabled) {
		this.ansiDisabled = ansiDisabled;
		return this;
	}
	
	/**
	 * Sets sequenceNumberStart.
	 * 
	 * Default is <code>null</code>.
	 * @param sequenceNumberStart <code>{@value #PROPERTY_sequenceNumberStart } = java.lang.Long</code> 
	 * @return this builder.
	 */
	public PatternConfigBuilder sequenceNumberStart(java.lang.@org.jspecify.annotations.Nullable Long sequenceNumberStart) {
		this.sequenceNumberStart = sequenceNumberStart;
		return this;
	}
	
	/**
	 * Sets abbreviatorCache.
	 * 
	 * Default is <code>null</code>.
	 * @param abbreviatorCache <code>{@value #PROPERTY_abbreviatorCache } = io.jstach.rainbowgum.pattern.format.PatternConfig.CacheType</code> 
	 * @return this builder.
	 */
	public PatternConfigBuilder abbreviatorCache(io.jstach.rainbowgum.pattern.format.PatternConfig.@org.jspecify.annotations.Nullable CacheType abbreviatorCache) {
		this.abbreviatorCache = abbreviatorCache;
		return this;
	}
	
	/**
	 * Creates {@code io.jstach.rainbowgum.pattern.format.PatternConfig } from this builder.
	 * @return {@code io.jstach.rainbowgum.pattern.format.PatternConfig }.
	 */
	public io.jstach.rainbowgum.pattern.format.PatternConfig build() {
		try {
			return io.jstach.rainbowgum.pattern.format.PatternConfigurator.provideFormatterConfig(
					this.name
					, this.zoneId
					, this.lineSeparator
					, this.ansiDisabled
					, this.propertyFunction
					, this.startTime
					, this.sequenceNumberStart
					, this.abbreviatorCache
					);
		}
		catch (IllegalArgumentException | LogProperty.PropertyMissingException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}
	
	@Override
	public PatternConfigBuilder fromProperties(LogProperties properties) {
		var __v = LogProperty.Validator.of(this.getClass());
		var _zoneId = properties.forKey(property_zoneId).ofString().map(_v -> io.jstach.rainbowgum.pattern.format.PatternConfigurator.convertZoneId(_v)).or(this.zoneId).validateIfError(__v);
		var _lineSeparator = properties.forKey(property_lineSeparator).ofString().or(this.lineSeparator).validateIfError(__v);
		var _ansiDisabled = properties.forKey(property_ansiDisabled).ofBoolean().or(this.ansiDisabled).validateIfError(__v);
		var _sequenceNumberStart = properties.forKey(property_sequenceNumberStart).ofString().map(_v -> io.jstach.rainbowgum.pattern.format.PatternConfigurator.convertSequenceNumberStart(_v)).or(this.sequenceNumberStart).validateIfError(__v);
		var _abbreviatorCache = properties.forKey(property_abbreviatorCache).ofString().map(_v -> io.jstach.rainbowgum.pattern.format.PatternConfigurator.convertAbbreviatorCache(_v)).or(this.abbreviatorCache).validateIfError(__v);
		__v.validate();
		this.zoneId = _zoneId.valueOrNull();
		this.lineSeparator = _lineSeparator.valueOrNull();
		this.ansiDisabled = _ansiDisabled.valueOrNull();
		this.sequenceNumberStart = _sequenceNumberStart.valueOrNull();
		this.abbreviatorCache = _abbreviatorCache.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;
	}
	
}