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

io.helidon.webserver.observe.ObserverConfigBase Maven / Gradle / Ivy

The 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;

import java.util.Objects;
import java.util.Optional;

import io.helidon.builder.api.Prototype;
import io.helidon.common.Errors;
import io.helidon.common.Generated;
import io.helidon.common.config.Config;

/**
 * Base configuration for any observer that exposes an endpoint.
 */
@Generated(value = "io.helidon.builder.codegen.BuilderCodegen", trigger = "io.helidon.webserver.observe.ObserverConfigBaseBlueprint")
public interface ObserverConfigBase extends ObserverConfigBaseBlueprint, Prototype.Api {

    /**
     * Create a new fluent API builder to customize configuration.
     *
     * @return a new builder
     */
    static ObserverConfigBase.Builder builder() {
        return new ObserverConfigBase.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 ObserverConfigBase.Builder builder(ObserverConfigBase instance) {
        return ObserverConfigBase.builder().from(instance);
    }

    /**
     * Fluent API builder base for {@link ObserverConfigBase}.
     *
     * @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 ObserverConfigBase> implements Prototype.ConfiguredBuilder {

        private boolean enabled = true;
        private Config config;
        private String name;

        /**
         * Protected to support extensibility.
         */
        protected BuilderBase() {
        }

        /**
         * 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(ObserverConfigBase prototype) {
            enabled(prototype.enabled());
            name(prototype.name());
            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(ObserverConfigBase.BuilderBase builder) {
            enabled(builder.enabled());
            builder.name().ifPresent(this::name);
            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;
            config.get("enabled").as(Boolean.class).ifPresent(this::enabled);
            return self();
        }

        /**
         * Whether this observer is enabled.
         *
         * @param enabled {@code false} to disable observer
         * @return updated builder instance
         * @see #enabled()
         */
        public BUILDER enabled(boolean enabled) {
            this.enabled = enabled;
            return self();
        }

        /**
         * Name of this observer. Each observer should provide its own default for this property.
         *
         * @param name observer name
         * @return updated builder instance
         * @see #name()
         */
        public BUILDER name(String name) {
            Objects.requireNonNull(name);
            this.name = name;
            return self();
        }

        /**
         * Whether this observer is enabled.
         *
         * @return the enabled
         */
        public boolean enabled() {
            return enabled;
        }

        /**
         * Name of this observer. Each observer should provide its own default for this property.
         *
         * @return the name
         */
        public Optional name() {
            return Optional.ofNullable(name);
        }

        /**
         * 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 "ObserverConfigBaseBuilder{"
                    + "enabled=" + enabled + ","
                    + "name=" + name
                    + "}";
        }

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

        /**
         * Validates required properties.
         */
        protected void validatePrototype() {
            Errors.Collector collector = Errors.collector();
            if (name == null) {
                collector.fatal(getClass(), "Property \"name\" is required, but not set");
            }
            collector.collect().checkValid();
        }

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

            private final boolean enabled;
            private final String name;

            /**
             * Create an instance providing a builder.
             *
             * @param builder extending builder base of this prototype
             */
            protected ObserverConfigBaseImpl(ObserverConfigBase.BuilderBase builder) {
                this.enabled = builder.enabled();
                this.name = builder.name().get();
            }

            @Override
            public boolean enabled() {
                return enabled;
            }

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

            @Override
            public String toString() {
                return "ObserverConfigBase{"
                        + "enabled=" + enabled + ","
                        + "name=" + name
                        + "}";
            }

            @Override
            public boolean equals(Object o) {
                if (o == this) {
                    return true;
                }
                if (!(o instanceof ObserverConfigBase other)) {
                    return false;
                }
                return enabled == other.enabled()
                    && Objects.equals(name, other.name());
            }

            @Override
            public int hashCode() {
                return Objects.hash(enabled, name);
            }

        }

    }

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

        Builder() {
        }

        @Override
        public ObserverConfigBase buildPrototype() {
            preBuildPrototype();
            validatePrototype();
            return new ObserverConfigBaseImpl(this);
        }

        @Override
        public ObserverConfigBase build() {
            return buildPrototype();
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy