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

com.crabshue.commons.properties.PropertiesUtils Maven / Gradle / Ivy

package com.crabshue.commons.properties;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.properties.exceptions.PropertiesErrorContext;
import com.crabshue.commons.properties.exceptions.PropertiesErrorType;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * Utility class for properties files manipulations.
 */
@Slf4j
public class PropertiesUtils {

    protected PropertiesUtils() {
    }

    /**
     * Read a properties file.
     *
     * @param propertiesFile the file.
     * @return the properties.
     */
    public static Properties readPropertiesFile(@NonNull final File propertiesFile) {

        return readPropertiesFile(propertiesFile.toURI());
    }

    /**
     * Read a properties file.
     *
     * @param propertiesFilePath the file.
     * @return the properties.
     */
    public static Properties readPropertiesFile(@NonNull final Path propertiesFilePath) {

        return readPropertiesFile(propertiesFilePath.toUri());
    }


    /**
     * Read a properties file.
     *
     * @param propertiesFileUri the file.
     * @return the properties.
     */
    public static Properties readPropertiesFile(@NonNull final URI propertiesFileUri) {

        final Properties ret = new Properties();

        try (InputStream is = propertiesFileUri.toURL().openStream()) {
            ret.load(is);
        } catch (IOException e) {
            throw new SystemException(PropertiesErrorType.CANNOT_READ_PROPERTIES, "Cannot read properties file.")
                .addContextValue(PropertiesErrorContext.PROPERTIES, propertiesFileUri);
        }
        return ret;
    }

    /**
     * Filter the properties whose key matches prefix.
     *
     * @param properties the properties.
     * @param prefix     the prefix to filter with.
     * @return the matching properties.
     */
    public static Map collectPropertiesForPrefix(@NonNull final Properties properties,
                                                                 @NonNull final String prefix) {

        final Map ret = new HashMap<>();

        for (final Map.Entry property : properties.entrySet()) {
            final String key = (String) property.getKey();
            if (StringUtils.startsWith(key, prefix)) {
                final String value = (String) property.getValue();
                ret.put(key, value);
            }
        }

        logger.info("Extracted [{}] properties with prefix [{}] from [{}]", ret, prefix, properties);
        return ret;
    }

    /**
     * Filter the properties whose key matches pattern.
     *
     * @param properties the properties.
     * @param pattern    the pattern to filter with.
     * @return the matching properties.
     */
    public static Map collectPropertiesForKeyMatchingPattern(@NonNull final Properties properties,
                                                                             @NonNull final String pattern) {

        final Map ret = new HashMap<>();

        for (final Map.Entry property : properties.entrySet()) {
            final String key = (String) property.getKey();
            if (key.matches(pattern)) {
                final String value = (String) property.getValue();
                ret.put(key, value);
            }
        }

        logger.info("Extracted [{}] properties with pattern [{}] from [{}]", ret, pattern, properties);
        return ret;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy