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

io.github.toolfactory.jvm.util.Properties Maven / Gradle / Ivy

There is a newer version: 9.7.1
Show newest version
/*
 * This file is part of ToolFactory JVM driver.
 *
 * Hosted at: https://github.com/toolfactory/jvm-driver
 *
 * --
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2019-2021 Luke Hutchison, Roberto Gentili
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 * OR OTHER DEALINGS IN THE SOFTWARE.
 */
package io.github.toolfactory.jvm.util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class Properties {


	public static Map loadFromResources(String resRelPath, String propertyName, ClassLoader... classLoaders) throws IOException, ParseException {
		Map resources = Resources.getAsInputStreams(resRelPath, classLoaders);
		TreeMap orderedProperties = new TreeMap<>();
		if (resources.isEmpty()) {
			return orderedProperties;
		}
		DecimalFormatSymbols symbols = new DecimalFormatSymbols();
		symbols.setGroupingSeparator(',');
		symbols.setDecimalSeparator('.');
		DecimalFormat decimalFormat = new DecimalFormat("#.#", symbols);
		decimalFormat.setParseBigDecimal(true);
		Collection propertiesWithoutPriority = new ArrayList<>();
		for (Entry entry : resources.entrySet()) {
		    java.util.Properties props = new java.util.Properties();
		    try (InputStream inputStream = entry.getValue()) {
			    props.load(entry.getValue());
			    String propValue = props.getProperty(propertyName);
			    if (propValue != null && !propValue.trim().isEmpty()) {
			    	try {
						orderedProperties.put(stringToBigDecimal(propValue, decimalFormat), props);
					} catch (ParseException exc) {
						throw new IllegalArgumentException(
							Strings.compile(
								"The value '{}' of property named '{}' inside the file {} is incorrect",
								propValue, propertyName, entry.getKey().getPath()
							), exc
						);
					}
			    } else {
			    	propertiesWithoutPriority.add(props);
			    }
		    }
		}
		if (!propertiesWithoutPriority.isEmpty()) {
			BigDecimal currentMinPriority = stringToBigDecimal("1", decimalFormat);
			if (!orderedProperties.isEmpty()) {
				currentMinPriority = orderedProperties.firstKey();
			}
			for (java.util.Properties props : propertiesWithoutPriority) {
				orderedProperties.put(currentMinPriority.subtract(new BigDecimal(1)), props);
			}
		}
		return orderedProperties;
	}


	private static BigDecimal stringToBigDecimal(String value, DecimalFormat decimalFormat) throws ParseException {
		value =	value.trim();
		if (value.contains(".")) {
			String wholeNumber = value.substring(0, value.indexOf("."));
			String fractionalPart = value.substring(value.indexOf(".") + 1, value.length());
			fractionalPart = fractionalPart.replace(".", "");
			value = wholeNumber + "." + fractionalPart;
		}
		return ((BigDecimal)decimalFormat.parse(value));
	}


	public static java.util.Properties loadFromResourceWithHigherPropertyValue(ClassLoader resourceClassLoader, String resRelPath, String propertyName, ClassLoader... classLoaders) throws IOException, ParseException {
		Map orderedProperties = ((TreeMap)loadFromResources(resRelPath, propertyName, classLoaders)).descendingMap();
		return orderedProperties.entrySet().iterator().next().getValue();
	}

	public static java.util.Properties loadFromResourcesAndMerge(String resRelPath, String propertyName, ClassLoader... classLoaders) throws IOException, ParseException {
		Map orderedProperties = loadFromResources(resRelPath, propertyName, classLoaders);
		java.util.Properties properties = new java.util.Properties();
		for (Entry entry : orderedProperties.entrySet()) {
			properties.putAll(entry.getValue());
		}
		properties.remove(propertyName);
		return properties;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy