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

com.swirlds.config.extensions.sources.SimpleConfigSource Maven / Gradle / Ivy

Go to download

Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.

There is a newer version: 0.56.6
Show newest version
/*
 * Copyright (C) 2022-2024 Hedera Hashgraph, LLC
 *
 * 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 com.swirlds.config.extensions.sources;

import static com.swirlds.config.extensions.sources.ConfigSourceOrdinalConstants.PROGRAMMATIC_VALUES_ORDINAL;

import com.swirlds.base.ArgumentUtils;
import com.swirlds.config.api.Configuration;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * A {@link com.swirlds.config.api.source.ConfigSource} implementation that can be used to provide values of properties
 * programmatically by calling {@link #withValue(String, Long)} (String, String)}.
 */
public final class SimpleConfigSource extends AbstractConfigSource {

    private final Map internalProperties;

    private int oridinal = PROGRAMMATIC_VALUES_ORDINAL;

    /**
     * Creates an instance without any config properties.
     */
    public SimpleConfigSource() {
        this.internalProperties = new HashMap<>();
    }

    /**
     * Creates an instance without any config properties.
     */
    public SimpleConfigSource(@NonNull final Map properties) {
        // defensive copy
        this.internalProperties = new HashMap<>(Objects.requireNonNull(properties));
    }

    /**
     * Creates an instance and directly adds the given config property.
     *
     * @param propertyName name of the config property
     * @param value        value of the config property
     */
    public SimpleConfigSource(final String propertyName, final String value) {
        this();
        withValue(propertyName, value);
    }

    /**
     * Creates an instance and directly adds the given config property.
     *
     * @param propertyName name of the config property
     * @param value        value of the config property
     */
    public SimpleConfigSource(final String propertyName, final Integer value) {
        this();
        withValue(propertyName, value);
    }

    /**
     * Creates an instance and directly adds the given config property.
     *
     * @param propertyName name of the config property
     * @param value        value of the config property
     */
    public SimpleConfigSource(final String propertyName, final Long value) {
        this();
        withValue(propertyName, value);
    }

    /**
     * Creates an instance and directly adds the given config property.
     *
     * @param propertyName name of the config property
     * @param value        value of the config property
     */
    public SimpleConfigSource(final String propertyName, final Double value) {
        this();
        withValue(propertyName, value);
    }

    /**
     * Creates an instance and directly adds the given config property.
     *
     * @param propertyName name of the config property
     * @param value        value of the config property
     */
    public SimpleConfigSource(final String propertyName, final Float value) {
        this();
        withValue(propertyName, value);
    }

    /**
     * Creates an instance and directly adds the given config property.
     *
     * @param propertyName name of the config property
     * @param value        value of the config property
     */
    public SimpleConfigSource(final String propertyName, final Boolean value) {
        this();
        withValue(propertyName, value);
    }

    /**
     * Adds a string value to this source.
     *
     * @param propertyName name of the property
     * @param value        default value
     */
    public SimpleConfigSource withValue(final String propertyName, final String value) {
        setValue(propertyName, value, v -> v);
        return this;
    }

    /**
     * Adds an int value to this source.
     *
     * @param propertyName name of the property
     * @param value        default value
     */
    public SimpleConfigSource withValue(final String propertyName, final Integer value) {
        setValue(propertyName, value, v -> Integer.toString(v));
        return this;
    }

    /**
     * Adds a double value to this source.
     *
     * @param propertyName name of the property
     * @param value        default value
     */
    public SimpleConfigSource withValue(final String propertyName, final Double value) {
        setValue(propertyName, value, v -> Double.toString(v));
        return this;
    }

    /**
     * Adds a float value to this source.
     *
     * @param propertyName name of the property
     * @param value        default value
     */
    public SimpleConfigSource withValue(final String propertyName, final Float value) {
        setValue(propertyName, value, v -> Float.toString(v));
        return this;
    }

    /**
     * Adds a long value to this source.
     *
     * @param propertyName name of the property
     * @param value        default value
     */
    public SimpleConfigSource withValue(final String propertyName, final Long value) {
        setValue(propertyName, value, v -> Long.toString(v));
        return this;
    }

    /**
     * Adds a boolean value to this source.
     *
     * @param propertyName name of the property
     * @param value        default value
     */
    @NonNull
    public SimpleConfigSource withValue(@NonNull final String propertyName, @NonNull final Boolean value) {
        setValue(propertyName, value, v -> Boolean.toString(v));
        return this;
    }

    private  void setValue(
            @NonNull final String propertyName, @Nullable final T value, @NonNull Function converter) {
        ArgumentUtils.throwArgBlank(propertyName, "propertyName");
        Objects.requireNonNull(converter, "converter must not be null");
        internalProperties.put(
                propertyName, Optional.ofNullable(value).map(converter::apply).orElse(null));
    }

    /**
     * Adds a list value to this source.
     *
     * @param propertyName name of the property
     * @param values       default values list
     */
    public SimpleConfigSource withBooleanValues(final String propertyName, final List values) {
        setValues(propertyName, values, v -> Boolean.toString(v));
        return this;
    }

    /**
     * Adds a list value to this source.
     *
     * @param propertyName name of the property
     * @param values       default values list
     */
    public SimpleConfigSource withIntegerValues(final String propertyName, final List values) {
        setValues(propertyName, values, v -> Integer.toString(v));
        return this;
    }

    /**
     * Adds a list value to this source.
     *
     * @param propertyName name of the property
     * @param values       default values list
     */
    public SimpleConfigSource withLongValues(final String propertyName, final List values) {
        setValues(propertyName, values, v -> Long.toString(v));
        return this;
    }

    /**
     * Adds a list value to this source.
     *
     * @param propertyName name of the property
     * @param values       default values list
     */
    public SimpleConfigSource withDoubleValues(final String propertyName, final List values) {
        setValues(propertyName, values, v -> Double.toString(v));
        return this;
    }

    /**
     * Adds a list value to this source.
     *
     * @param propertyName name of the property
     * @param values       default values list
     */
    public SimpleConfigSource withStringValues(final String propertyName, final List values) {
        setValues(propertyName, values, v -> v);
        return this;
    }

    private  void setValues(
            @NonNull final String propertyName,
            @Nullable final List values,
            @NonNull Function converter) {
        ArgumentUtils.throwArgBlank(propertyName, "propertyName");
        Objects.requireNonNull(converter, "converter must not be null");
        if (values == null) {
            internalProperties.put(propertyName, null);
        } else if (values.isEmpty()) {
            internalProperties.put(propertyName, Configuration.EMPTY_LIST);
        } else {
            String rawValues = values.stream().map(converter).collect(Collectors.joining(","));
            internalProperties.put(propertyName, rawValues);
        }
    }

    /**
     * Specify the ordinal of this source. Default is {@link ConfigSourceOrdinalConstants#PROGRAMMATIC_VALUES_ORDINAL}.
     *
     * @param ordinal the ordinal
     * @return this
     */
    public SimpleConfigSource withOrdinal(final int ordinal) {
        this.oridinal = ordinal;
        return this;
    }

    /**
     * Removes all default properties.
     */
    public void reset() {
        internalProperties.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Map getInternalProperties() {
        return Collections.unmodifiableMap(internalProperties);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getOrdinal() {
        return oridinal;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy