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

io.microconfig.core.properties.PropertiesImpl Maven / Gradle / Ivy

package io.microconfig.core.properties;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import static io.microconfig.utils.StreamUtils.*;
import static java.util.function.Function.identity;

@EqualsAndHashCode
@RequiredArgsConstructor
public class PropertiesImpl implements Properties {
    private final List properties;

    public static Properties flat(List properties) {
        return new PropertiesImpl(flatMapEach(properties, Properties::asTypedProperties));
    }

    @Override
    public Properties resolveBy(Resolver resolver) {
        return withEachComponent(c -> c.resolveBy(resolver));
    }

    @Override
    public Properties withoutVars() {
        return withEachComponent(TypedProperties::withoutVars);
    }

    @Override
    public Properties without(Predicate excluded) {
        return withEachComponent(c -> c.without(excluded));
    }

    @Override
    public Properties withPrefix(String prefix) {
        return withEachComponent(tp -> tp.withPrefix(prefix));
    }

    @Override
    public Map getPropertiesAsMap() {
        return propertyKeyTo(identity());
    }

    @Override
    public Map getPropertiesAsKeyValue() {
        return propertyKeyTo(Property::getValue);
    }

    @Override
    public Collection getProperties() {
        return flatMapEach(properties, TypedProperties::getProperties);
    }

    @Override
    public Optional getPropertyWithKey(String key) {
        return findFirstResult(properties, p -> p.getPropertyWithKey(key));
    }

    @Override
    public  List save(PropertySerializer serializer) {
        return forEach(properties, p -> p.save(serializer));
    }

    @Override
    public List asTypedProperties() {
        return properties;
    }

    @Override
    public TypedProperties first() {
        return properties.get(0);
    }

    @Override
    public Properties forEachComponent(UnaryOperator callback) {
        return withEachComponent(callback);
    }

    private Properties withEachComponent(UnaryOperator applyFunction) {
        return new PropertiesImpl(forEach(properties.parallelStream(), applyFunction));
    }

    private  Map propertyKeyTo(Function valueGetter) {
        return properties.stream()
                .map(TypedProperties::getProperties)
                .flatMap(Collection::stream)
                .collect(toLinkedMap(Property::getKey, valueGetter));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy