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

fi.jubic.easyconfig.internal.parameter.MappableParameter Maven / Gradle / Ivy

There is a newer version: 0.10.4
Show newest version
package fi.jubic.easyconfig.internal.parameter;

import fi.jubic.easyconfig.internal.Result;
import fi.jubic.easyconfig.providers.EnvProvider;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.BiFunction;

/**
 * A single parameter instance that can be injected into an initializer.
 *
 * @param  the type of the parameter
 */
public final class MappableParameter implements Mappable {
    private final String variableName;
    private final Class parameterClass;
    private final boolean nullable;
    private final boolean noPrefix;
    private final BiFunction> parser;
    @Nullable
    private final T defaultValue;

    MappableParameter(
            String variableName,
            Class parameterClass,
            boolean nullable,
            boolean noPrefix,
            BiFunction> parser,
            @Nullable T defaultValue
    ) {
        if (nullable && (defaultValue != null)) {
            throw new IllegalStateException();
        }
        this.variableName = variableName;
        this.parameterClass = parameterClass;
        this.nullable = nullable;
        this.noPrefix = noPrefix;
        this.parser = parser;
        this.defaultValue = defaultValue;
    }

    @Override
    public Result initialize(String prefix, EnvProvider envProvider) {
        String effectiveVariableName = noPrefix
                ? variableName
                : prefix + variableName;
        return envProvider.getVariable(effectiveVariableName)
                .map(strVal -> parser.apply(prefix, strVal))
                .orElseGet(() -> {
                    if (nullable) {
                        return Result.of(null);
                    }
                    return Optional.ofNullable(defaultValue)
                            .map(Result::of)
                            .orElseGet(() -> Result.message(
                                    String.format(
                                            "Missing variable \"%s\" [%s]",
                                            effectiveVariableName,
                                            parameterClass.getSimpleName()
                                    )
                            ));
                });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy