001/*
002 * Copyright 2023 the original author or authors.
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * https://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.tools.formatting.template;
017
018import java.util.HashMap;
019import java.util.Locale;
020import java.util.Map;
021
022import lombok.AccessLevel;
023import lombok.RequiredArgsConstructor;
024
025/**
026 * @author Sven Haag
027 * @param <T> This should any value, which extends FormatterSupport interface
028 *
029 */
030@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
031public class TemplateManager<T extends FormatterSupport> {
032
033    private final TemplateFormatter<T> defaultFormatter;
034
035    private final Map<Locale, TemplateFormatter<T>> localeSpecificMap;
036
037    /**
038     * @param targetToFormat
039     * @param locale
040     * @return This should format the template with the location information
041     */
042    public String format(final T targetToFormat, final Locale locale) {
043        if (localeSpecificMap.containsKey(locale)) {
044            return localeSpecificMap.get(locale).format(targetToFormat);
045        }
046        return defaultFormatter.format(targetToFormat);
047    }
048
049    /**
050     * Builder inner class for the template manager
051     *
052     * @param <B> at least {@link FormatterSupport}
053     */
054    public static class TemplateManagerBuilder<B extends FormatterSupport> {
055
056        private final Map<Locale, TemplateFormatter<B>> map;
057
058        private TemplateFormatter<B> defFormatter;
059
060        /**
061         * Constructor
062         */
063        public TemplateManagerBuilder() {
064            map = new HashMap<>();
065        }
066
067        /**
068         * @param mapValue
069         * @return This method should return the current object
070         */
071        public TemplateManagerBuilder<B> with(final Map<Locale, TemplateFormatter<B>> mapValue) {
072            if (null != mapValue) {
073                map.putAll(mapValue);
074            }
075            return this;
076        }
077
078        /**
079         *
080         * @param locale    mapValue Map consists of key, which is {@link Locale}
081         * @param formatter and a template formatter {@link TemplateFormatter}
082         *
083         * @return This method should add new locale to the current template
084         */
085        public TemplateManagerBuilder<B> with(final Locale locale, final TemplateFormatter<B> formatter) {
086            map.put(locale, formatter);
087            return this;
088        }
089
090        /**
091         * @param formatter
092         * @return TemplateManagerBuilder return the default formatter
093         */
094        public TemplateManagerBuilder<B> useAsDefault(final TemplateFormatter<B> formatter) {
095            defFormatter = formatter;
096            return this;
097        }
098
099        /**
100         * @return This method builds the object with the given information.
101         */
102        public TemplateManager<B> build() {
103            return new TemplateManager<>(defFormatter, map);
104        }
105
106    }
107
108}