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

com.expediagroup.rhapsody.util.ConfigLoading Maven / Gradle / Ivy

There is a newer version: 0.5.16
Show newest version
/**
 * Copyright 2019 Expedia, Inc.
 *
 * 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.expediagroup.rhapsody.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class ConfigLoading {

    private ConfigLoading() {

    }

    public static  T load(Map configs, String property, Function parser, T defaultValue) {
        return load(configs, property, parser).orElse(defaultValue);
    }

    public static  T loadOrThrow(Map configs, String property, Function parser) {
        return load(configs, property, parser).orElseThrow(supplyMissingConfigPropertyException(property));
    }

    public static  Optional load(Map configs, String property, Function parser) {
        return Optional.ofNullable(configs.get(property)).map(Object::toString).map(parser);
    }

    public static  R loadCollectionOrThrow(Map configs, String property, Function parser, Collector collector) {
        return loadCollection(configs, property, parser, collector).orElseThrow(supplyMissingConfigPropertyException(property));
    }

    public static  Optional loadCollection(Map configs, String property, Function parser, Collector collector) {
        return Optional.ofNullable(configs.get(property))
            .map(ConfigLoading::convertToCollection)
            .map(collection -> collection.stream().map(Objects::toString).map(parser).collect(collector));
    }

    public static Map loadPrefixedEnvironmentalProperties(String prefix) {
        return Stream.of(System.getenv().entrySet(), System.getProperties().entrySet())
            .flatMap(Collection::stream)
            .filter(entry -> Objects.toString(entry.getKey()).startsWith(prefix))
            .collect(Collectors.toMap(entry -> entry.getKey().toString().substring(prefix.length()), Map.Entry::getValue,
                (firstValue, secondValue) -> secondValue));
    }

    public static  Map loadPrefixed(Map configs, String prefix, Function parser) {
        return configs.entrySet().stream()
            .filter(entry -> entry.getKey().startsWith(prefix))
            .collect(Collectors.toMap(entry -> entry.getKey().substring(prefix.length()), entry -> parser.apply(entry.getValue().toString())));
    }

    private static Collection convertToCollection(Object config) {
        if (config instanceof Collection) {
            return Collection.class.cast(config);
        } else if (config instanceof CharSequence) {
            return Arrays.asList(config.toString().split(","));
        } else {
            return Collections.singletonList(config);
        }
    }

    private static Supplier supplyMissingConfigPropertyException(String property) {
        return () -> new IllegalArgumentException("Missing config: " + property);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy