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

io.deepsense.neptune.clientlibrary.config.user.MultiSourcedUserConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.config.user;

import com.google.common.base.Preconditions;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public final class MultiSourcedUserConfiguration extends StaticUserConfiguration {

    /**
     * Creates a combined configuration based on given configurations.
     * Each next configuration overrides the previous one.
     *
     * @param configurations Base configurations, ordered from the least important to most important.
     * @return multiSourcedUserConfiguration
     */
    public static MultiSourcedUserConfiguration create(UserConfiguration... configurations) {
        List configurationList = Arrays.asList(Preconditions.checkNotNull(configurations));
        return new MultiSourcedUserConfiguration(
                getOverridenConfigurationProperty(configurationList, UserConfiguration::getUsername).orElse(null),
                getOverridenConfigurationProperty(configurationList, UserConfiguration::getPassword).orElse(null));
    }

    private MultiSourcedUserConfiguration(String username, String password) {
        super(username, password);
    }

    private static  Optional getOverridenConfigurationProperty(
            List configurations,
            Function> configurationPropertyExtractor) {

        return configurations.stream()
                .map(configurationPropertyExtractor)
                .reduce(Optional.empty(), MultiSourcedUserConfiguration::overrideConfigurationProperty);
    }

    private static  Optional overrideConfigurationProperty(
            Optional propertyToOverride,
            Optional overridingProperty) {
        return Optional.ofNullable(overridingProperty.orElse(propertyToOverride.orElse(null)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy