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

io.helidon.webserver.observe.info.InfoObserverConfig Maven / Gradle / Ivy

There is a newer version: 4.1.4
Show newest version
/*
 * Copyright (c) 2024 Oracle and/or its affiliates.
 *
 * 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
 *
 *     http://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.helidon.webserver.observe.info;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

import io.helidon.builder.api.Prototype;
import io.helidon.common.Generated;
import io.helidon.common.config.Config;
import io.helidon.webserver.observe.ObserverConfigBase;

/**
 * Info Observer configuration.
 *
 * @see #builder()
 * @see #create()
 */
@Generated(value = "io.helidon.builder.codegen.BuilderCodegen", trigger = "io.helidon.webserver.observe.info.InfoObserverConfigBlueprint")
public interface InfoObserverConfig extends InfoObserverConfigBlueprint, Prototype.Api, ObserverConfigBase {

    /**
     * Create a new fluent API builder to customize configuration.
     *
     * @return a new builder
     */
    static InfoObserverConfig.Builder builder() {
        return new InfoObserverConfig.Builder();
    }

    /**
     * Create a new fluent API builder from an existing instance.
     *
     * @param instance an existing instance used as a base for the builder
     * @return a builder based on an instance
     */
    static InfoObserverConfig.Builder builder(InfoObserverConfig instance) {
        return InfoObserverConfig.builder().from(instance);
    }

    /**
     * Create a new instance from configuration.
     *
     * @param config used to configure the new instance
     * @return a new instance configured from configuration
     */
    static InfoObserverConfig create(Config config) {
        return InfoObserverConfig.builder().config(config).buildPrototype();
    }

    /**
     * Create a new instance with default values.
     *
     * @return a new instance
     */
    static InfoObserverConfig create() {
        return InfoObserverConfig.builder().buildPrototype();
    }

    /**
     * Fluent API builder base for {@link InfoObserver}.
     *
     * @param  type of the builder extending this abstract builder
     * @param  type of the prototype interface that would be built by {@link #buildPrototype()}
     */
    abstract class BuilderBase, PROTOTYPE extends InfoObserverConfig> extends ObserverConfigBase.BuilderBase implements Prototype.ConfiguredBuilder {

        private final Map values = new LinkedHashMap<>();
        private Config config;
        private String endpoint = "info";

        /**
         * Protected to support extensibility.
         */
        protected BuilderBase() {
            name("info");
        }

        /**
         * Update this builder from an existing prototype instance. This method disables automatic service discovery.
         *
         * @param prototype existing prototype to update this builder from
         * @return updated builder instance
         */
        public BUILDER from(InfoObserverConfig prototype) {
            super.from(prototype);
            endpoint(prototype.endpoint());
            addValues(prototype.values());
            return self();
        }

        /**
         * Update this builder from an existing prototype builder instance.
         *
         * @param builder existing builder prototype to update this builder from
         * @return updated builder instance
         */
        public BUILDER from(InfoObserverConfig.BuilderBase builder) {
            super.from(builder);
            endpoint(builder.endpoint());
            addValues(builder.values);
            return self();
        }

        /**
         * Update builder from configuration (node of this type).
         * If a value is present in configuration, it would override currently configured values.
         *
         * @param config configuration instance used to obtain values to update this builder
         * @return updated builder instance
         */
        @Override
        public BUILDER config(Config config) {
            Objects.requireNonNull(config);
            this.config = config;
            super.config(config);
            config.get("endpoint").as(String.class).ifPresent(this::endpoint);
            config.get("values").detach().asMap().ifPresent(this::values);
            return self();
        }

        /**
         *
         *
         * @param endpoint
         * @return updated builder instance
         * @see #endpoint()
         */
        public BUILDER endpoint(String endpoint) {
            Objects.requireNonNull(endpoint);
            this.endpoint = endpoint;
            return self();
        }

        /**
         * This method replaces all values with the new ones.
         *
         * @param values value map
         * @return updated builder instance
         * @see #values()
         */
        public BUILDER values(Map values) {
            Objects.requireNonNull(values);
            this.values.clear();
            this.values.putAll(values);
            return self();
        }

        /**
         * This method keeps existing values, then puts all new values into the map.
         *
         * @param values value map
         * @return updated builder instance
         * @see #values()
         */
        public BUILDER addValues(Map values) {
            Objects.requireNonNull(values);
            this.values.putAll(values);
            return self();
        }

        /**
         * This method adds a new value to the map, or replaces it if the key already exists.
         *
         * @param key key to add or replace
         * @param value new value for the key
         * @return updated builder instance
         * @see #values()
         */
        public BUILDER putValue(String key, String value) {
            Objects.requireNonNull(key);
            Objects.requireNonNull(value);
            this.values.put(key, value);
            return self();
        }

        /**
         *
         *
         * @return the endpoint
         */
        public String endpoint() {
            return endpoint;
        }

        /**
         * Values to be exposed using this observability endpoint.
         *
         * @return the values
         */
        public Map values() {
            return values;
        }

        /**
         * If this instance was configured, this would be the config instance used.
         *
         * @return config node used to configure this builder, or empty if not configured
         */
        public Optional config() {
            return Optional.ofNullable(config);
        }

        @Override
        public String toString() {
            return "InfoObserverConfigBuilder{"
                    + "endpoint=" + endpoint + ","
                    + "values=" + values
                    + "};"
                    + super.toString();
        }

        /**
         * Handles providers and decorators.
         */
        protected void preBuildPrototype() {
            super.preBuildPrototype();
        }

        /**
         * Validates required properties.
         */
        protected void validatePrototype() {
            super.validatePrototype();
        }

        /**
         * Generated implementation of the prototype, can be extended by descendant prototype implementations.
         */
        protected static class InfoObserverConfigImpl extends ObserverConfigBaseImpl implements InfoObserverConfig, Supplier {

            private final Map values;
            private final String endpoint;

            /**
             * Create an instance providing a builder.
             *
             * @param builder extending builder base of this prototype
             */
            protected InfoObserverConfigImpl(InfoObserverConfig.BuilderBase builder) {
                super(builder);
                this.endpoint = builder.endpoint();
                this.values = Collections.unmodifiableMap(new LinkedHashMap<>(builder.values()));
            }

            @Override
            public InfoObserver build() {
                return InfoObserver.create(this);
            }

            @Override
            public InfoObserver get() {
                return build();
            }

            @Override
            public String endpoint() {
                return endpoint;
            }

            @Override
            public Map values() {
                return values;
            }

            @Override
            public String toString() {
                return "InfoObserverConfig{"
                        + "endpoint=" + endpoint + ","
                        + "values=" + values
                        + "};"
                        + super.toString();
            }

            @Override
            public boolean equals(Object o) {
                if (o == this) {
                    return true;
                }
                if (!(o instanceof InfoObserverConfig other)) {
                    return false;
                }
                return super.equals(other)
                            && Objects.equals(endpoint, other.endpoint())
                    && Objects.equals(values, other.values());
            }

            @Override
            public int hashCode() {
                return 31 * super.hashCode() + Objects.hash(endpoint, values);
            }

        }

    }

    /**
     * Fluent API builder for {@link InfoObserver}.
     */
    class Builder extends InfoObserverConfig.BuilderBase implements io.helidon.common.Builder {

        private Builder() {
        }

        @Override
        public InfoObserverConfig buildPrototype() {
            preBuildPrototype();
            validatePrototype();
            return new InfoObserverConfigImpl(this);
        }

        @Override
        public InfoObserver build() {
            return InfoObserver.create(this.buildPrototype());
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy