001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.logging;
003
004import static io.cui.tools.string.MoreStrings.lenientFormat;
005import static io.cui.tools.string.MoreStrings.nullToEmpty;
006import java.util.function.Supplier;
007import lombok.NonNull;
008
009/**
010 * Represents a log-entry. Especially focuses on enforcing log-entry identifier, see
011 * {@link #getPrefix()} and {@link #getIdentifier()}. The template mechanism is the same as with
012 * {@link CuiLogger}, saying it accepts as well '%s' and '{}' as placeholder, even mixed.
013 * To simplify usage the prefix string will always be prepended on calling
014 * {@link #format(Object...)}
015 *
016 * @author Oliver Wolff
017 */
018public class LogRecordModel implements LogRecord {
019    private static final String PREFIX_IDENTIFIER_TEMPLATE = "%s-%s";
020    private static final String AFTER_PREFIX = ": ";
021    @NonNull
022    private final String prefix;
023    @NonNull
024    private final Integer identifier;
025    @NonNull
026    private final String template;
027    /**
028     * Tiniest of optimization. Needs to be verified.
029     */
030    private String parsedMessageTemplate;
031    private String parsedIdentifier;
032
033    protected String getParsedMessageTemplate() {
034        if (null == parsedMessageTemplate) {
035            parsedMessageTemplate = CuiLogger.SLF4J_PATTERN.matcher(nullToEmpty(getTemplate())).replaceAll("%s");
036        }
037        return parsedMessageTemplate;
038    }
039
040    @Override
041    public String format(Object... parameter) {
042        return new StringBuilder(resolveIdentifierString()).append(AFTER_PREFIX).append(lenientFormat(getParsedMessageTemplate(), parameter)).toString();
043    }
044
045    @Override
046    public Supplier<String> supplier(Object... parameter) {
047        return () -> format(parameter);
048    }
049
050    @Override
051    public String resolveIdentifierString() {
052        if (null == parsedIdentifier) {
053            parsedIdentifier = String.format(PREFIX_IDENTIFIER_TEMPLATE, getPrefix(), getIdentifier());
054        }
055        return parsedIdentifier;
056    }
057
058    private LogRecordModel(@NonNull String prefix, @NonNull Integer identifier, @NonNull String template) {
059        super();
060        if (prefix == null) {
061            throw new java.lang.NullPointerException("prefix is marked non-null but is null");
062        }
063        if (identifier == null) {
064            throw new java.lang.NullPointerException("identifier is marked non-null but is null");
065        }
066        if (template == null) {
067            throw new java.lang.NullPointerException("template is marked non-null but is null");
068        }
069        this.prefix = prefix;
070        this.identifier = identifier;
071        this.template = template;
072    }
073
074
075    @java.lang.SuppressWarnings("all")
076    @lombok.Generated
077    public static class LogRecordModelBuilder {
078        @java.lang.SuppressWarnings("all")
079        @lombok.Generated
080        private String prefix;
081        @java.lang.SuppressWarnings("all")
082        @lombok.Generated
083        private Integer identifier;
084        @java.lang.SuppressWarnings("all")
085        @lombok.Generated
086        private String template;
087
088        @java.lang.SuppressWarnings("all")
089        @lombok.Generated
090        LogRecordModelBuilder() {
091        }
092
093        /**
094         * @return {@code this}.
095         */
096        @java.lang.SuppressWarnings("all")
097        @lombok.Generated
098        public LogRecordModel.LogRecordModelBuilder prefix(@NonNull final String prefix) {
099            if (prefix == null) {
100                throw new java.lang.NullPointerException("prefix is marked non-null but is null");
101            }
102            this.prefix = prefix;
103            return this;
104        }
105
106        /**
107         * @return {@code this}.
108         */
109        @java.lang.SuppressWarnings("all")
110        @lombok.Generated
111        public LogRecordModel.LogRecordModelBuilder identifier(@NonNull final Integer identifier) {
112            if (identifier == null) {
113                throw new java.lang.NullPointerException("identifier is marked non-null but is null");
114            }
115            this.identifier = identifier;
116            return this;
117        }
118
119        /**
120         * @return {@code this}.
121         */
122        @java.lang.SuppressWarnings("all")
123        @lombok.Generated
124        public LogRecordModel.LogRecordModelBuilder template(@NonNull final String template) {
125            if (template == null) {
126                throw new java.lang.NullPointerException("template is marked non-null but is null");
127            }
128            this.template = template;
129            return this;
130        }
131
132        @java.lang.SuppressWarnings("all")
133        @lombok.Generated
134        public LogRecordModel build() {
135            return new LogRecordModel(this.prefix, this.identifier, this.template);
136        }
137
138        @java.lang.Override
139        @java.lang.SuppressWarnings("all")
140        @lombok.Generated
141        public java.lang.String toString() {
142            return "LogRecordModel.LogRecordModelBuilder(prefix=" + this.prefix + ", identifier=" + this.identifier + ", template=" + this.template + ")";
143        }
144    }
145
146    @java.lang.SuppressWarnings("all")
147    @lombok.Generated
148    public static LogRecordModel.LogRecordModelBuilder builder() {
149        return new LogRecordModel.LogRecordModelBuilder();
150    }
151
152    @NonNull
153    @java.lang.SuppressWarnings("all")
154    @lombok.Generated
155    public String getPrefix() {
156        return this.prefix;
157    }
158
159    @NonNull
160    @java.lang.SuppressWarnings("all")
161    @lombok.Generated
162    public Integer getIdentifier() {
163        return this.identifier;
164    }
165
166    @NonNull
167    @java.lang.SuppressWarnings("all")
168    @lombok.Generated
169    public String getTemplate() {
170        return this.template;
171    }
172}