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

com.github.alkedr.SimplePropertyLoader Maven / Gradle / Ivy

The newest version!
package com.github.alkedr;

import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

public class SimplePropertyLoader {
    private static final SimplePropertyLoader PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY = new SimplePropertyLoader(System::getProperty);

    private final Function propertyNameToNullablePropertyValueFunction;


    public SimplePropertyLoader(Function propertyNameToNullablePropertyValueFunction) {
        this.propertyNameToNullablePropertyValueFunction = propertyNameToNullablePropertyValueFunction;
    }


    public String getProperty(String name) {
        return optionalToRequired(name, getOptionalProperty(name));
    }

    public  T getProperty(String name, PropertyValueParser parser) {
        return optionalToRequired(name, getOptionalProperty(name, parser));
    }

    public  Stream getProperty(String name, PropertyValueParser> splitter,
                                     PropertyValueParser elementParser) {
        return optionalToRequired(name, getOptionalProperty(name, splitter, elementParser));
    }


    public Optional getOptionalProperty(String name) {
        return Optional.ofNullable(propertyNameToNullablePropertyValueFunction.apply(name));
    }

    public  Optional getOptionalProperty(String name, PropertyValueParser parser) {
        return getOptionalProperty(name).map(value -> invokeParser(parser, name, value));
    }

    public  Optional> getOptionalProperty(String name, PropertyValueParser> splitter,
                                                       PropertyValueParser elementParser) {
        return getOptionalProperty(name)
                .map(value -> {
                    int[] counter = {0};
                    return invokeParser(splitter, name, value)
                            .map(elementValue -> invokeParser(
                                    elementParser,
                                    String.format("%s[%d]", name, counter[0]++),
                                    elementValue
                            ));
                });
    }


    public static String getSystemProperty(String name) {
        return PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY.getProperty(name);
    }

    public static  T getSystemProperty(String name, PropertyValueParser parser) {
        return PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY.getProperty(name, parser);
    }

    public static  Stream getSystemProperty(String name, PropertyValueParser> splitter,
                                                  PropertyValueParser elementParser) {
        return PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY.getProperty(name, splitter, elementParser);
    }


    public static Optional getOptionalSystemProperty(String name) {
        return PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY.getOptionalProperty(name);
    }

    public static  Optional getOptionalSystemProperty(String name, PropertyValueParser parser) {
        return PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY.getOptionalProperty(name, parser);
    }

    public static  Optional> getOptionalSystemProperty(String name, PropertyValueParser> splitter,
                                                                    PropertyValueParser elementParser) {
        return PROPERTY_LOADER_FROM_SYSTEM_GET_PROPERTY.getOptionalProperty(name, splitter, elementParser);
    }


    private static  T optionalToRequired(String name, Optional optional) {
        return optional.orElseThrow(() -> new PropertyIsNotDefinedException(name));
    }

    private static  T invokeParser(PropertyValueParser parser, String name, String value) {
        try {
            return parser.parse(value);
        } catch (Exception e) {
            throw new PropertyValueParsingException(name, value, e);
        }
    }


    @FunctionalInterface
    public interface PropertyValueParser {
        T parse(String value) throws Exception;
    }


    public static class PropertyIsNotDefinedException extends RuntimeException {
        public PropertyIsNotDefinedException(String name) {
            super(String.format("Property '%s' is not defined", name));
        }
    }

    public static class PropertyValueParsingException extends RuntimeException {
        public PropertyValueParsingException(String name, String value, Exception e) {
            super(String.format("Can't parse value of property '%s': '%s': %s", name, value, e.getMessage()), e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy