001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.formatting.template;
003
004import java.util.HashMap;
005import java.util.Locale;
006import java.util.Map;
007
008/**
009 * @author Sven Haag
010 * @param <T> This should any value, which extends FormatterSupport interface
011 */
012public class TemplateManager<T extends FormatterSupport> {
013    private final TemplateFormatter<T> defaultFormatter;
014    private final Map<Locale, TemplateFormatter<T>> localeSpecificMap;
015
016    /**
017     * @param targetToFormat
018     * @param locale
019     * @return This should format the template with the location information
020     */
021    public String format(final T targetToFormat, final Locale locale) {
022        if (localeSpecificMap.containsKey(locale)) {
023            return localeSpecificMap.get(locale).format(targetToFormat);
024        }
025        return defaultFormatter.format(targetToFormat);
026    }
027
028
029    /**
030     * Builder inner class for the template manager
031     *
032     * @param <B> at least {@link FormatterSupport}
033     */
034    public static class TemplateManagerBuilder<B extends FormatterSupport> {
035        private final Map<Locale, TemplateFormatter<B>> map;
036        private TemplateFormatter<B> defFormatter;
037
038        /**
039         * Constructor
040         */
041        public TemplateManagerBuilder() {
042            this.map = new HashMap<>();
043        }
044
045        /**
046         * @param mapValue
047         * @return This method should return the current object
048         */
049        public TemplateManagerBuilder<B> with(final Map<Locale, TemplateFormatter<B>> mapValue) {
050            if (null != mapValue) {
051                this.map.putAll(mapValue);
052            }
053            return this;
054        }
055
056        /**
057         *
058         * @param locale
059         *            mapValue Map consists of key, which is {@link Locale}
060         * @param formatter
061         *            and a template formatter {@link TemplateFormatter}
062         *
063         * @return This method should add new locale to the current template
064         */
065        public TemplateManagerBuilder<B> with(final Locale locale, final TemplateFormatter<B> formatter) {
066            this.map.put(locale, formatter);
067            return this;
068        }
069
070        /**
071         * @param formatter
072         * @return TemplateManagerBuilder return the default formatter
073         */
074        public TemplateManagerBuilder<B> useAsDefault(final TemplateFormatter<B> formatter) {
075            this.defFormatter = formatter;
076            return this;
077        }
078
079        /**
080         * @return This method builds the object with the given information.
081         */
082        public TemplateManager<B> build() {
083            return new TemplateManager<>(defFormatter, map);
084        }
085    }
086
087    @java.lang.SuppressWarnings("all")
088    @lombok.Generated
089    private TemplateManager(final TemplateFormatter<T> defaultFormatter, final Map<Locale, TemplateFormatter<T>> localeSpecificMap) {
090        this.defaultFormatter = defaultFormatter;
091        this.localeSpecificMap = localeSpecificMap;
092    }
093}