All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.openapi.view.AbstractViewConfig Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.openapi.view;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Abstract View Config.
 *
 * @author croudet
 */
abstract class AbstractViewConfig {
    protected String prefix;
    protected String version = "";
    protected Map options = new HashMap<>();

    /**
     * An AbstractViewConfig.
     * @param prefix The configuration key prefix.
     */
    protected AbstractViewConfig(String prefix) {
        this.prefix = prefix;
    }

    /**
     * Returns the converter associated with the key.
     * @param key A key.
     * @return A converter or null.
     */
    protected abstract Function getConverter(String key);

    /**
     * Adds an option.
     * @param entry The user specified entry.
     */
    protected void addAttribute(Map.Entry entry) {
        String key = entry.getKey().substring(prefix.length());
        Function converter = getConverter(key);
        if (converter != null) {
            options.put(key, converter.apply(entry.getValue()));
        }
    }

    /**
     * Converts to html attributes.
     * @return A String.
     */
    protected String toHtmlAttributes() {
        return options.entrySet().stream().map(e -> e.getKey() + "=\"" + e.getValue() + '"')
                .collect(Collectors.joining(" "));
    }

    /**
     * Builds and parse a View Config.
     * @param  A View config type.
     * @param cfg A View config.
     * @param defaultOptions The default options.
     * @param properties The options to parse.
     * @return A View config.
     */
    static  T fromProperties(T cfg, Map defaultOptions, Map properties) {
        cfg.version = properties.getOrDefault(cfg.prefix + "version", cfg.version);
        cfg.options.putAll(defaultOptions);
        properties.entrySet().stream().filter(entry -> entry.getKey().startsWith(cfg.prefix))
            .forEach(cfg::addAttribute);
        return cfg;
    }

    /**
     * Converts to a Boolean.
     * @param v The input.
     * @return A Boolean.
     */
    static Object asBoolean(String v) {
        return Boolean.valueOf(v);
    }

    /**
     * Converts to a String.
     * @param v The input.
     * @return A String.
     */
    static Object asString(String v) {
        return v;
    }

    /**
     * Converts to a quoted String.
     * @param v The input.
     * @return A quoted String.
     */
    static Object asQuotedString(String v) {
        return v == null ? null : '"' + v + '"';
    }

    /**
     * Converts to an enum.
     *
     * @author croudet
     *
     * @param  An Enum class.
     */
    static class EnumConverter> implements Function {
        private final Class type;

        /**
         * EnumConverter.
         * @param type An Enum type.
         */
        public EnumConverter(Class type) {
            this.type = type;
        }

        /**
         * Converts to an Enum.
         */
        @Override
        public Object apply(String v) {
            return v == null ? null : Enum.valueOf(type, v.toUpperCase(Locale.US));
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy