Initial Commit

This commit is contained in:
Isaac Parenteau
2019-07-20 12:39:03 -05:00
commit 79529ecc40
66 changed files with 6549 additions and 0 deletions

View File

@@ -0,0 +1,125 @@
package net.locusworks.common.configuration;
import static net.locusworks.common.Charsets.UTF_8;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import net.locusworks.common.immutables.Pair;
/**
* Properties manager class to help load and read properties
* @author Isaac Parenteau
* @version 1.0.0
* @date 02/15/2018
*/
public class PropertiesManager {
/**
* Load a configuration from resource
* @param clazz class loader
* @param src source of the file
* @return properties
* @throws IOException Exception thrown the file can't be read
*/
public static Properties loadConfiguration(Class<?> clazz, String src) throws IOException {
InputStream is = clazz.getResourceAsStream(src);
if (is == null) {
is = clazz.getClassLoader().getResourceAsStream(src);
}
if (is == null) {
return null;
}
BufferedReader br = new BufferedReader(new InputStreamReader(is, UTF_8));
return loadConfiguration(br);
}
/**
* Load configuration from a file
* @param file File to load
* @return properties
* @throws IOException Exception thrown the file can't be read
*/
public static Properties loadConfiguration(File file) throws IOException {
if (!file.exists()) {
return new Properties();
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8));
return loadConfiguration(br);
}
/**
* Load configuration from a buffered reader
* @param reader Buffered reader to read the properties values from
* @return properties
* @throws IOException Exception thrown the file can't be read
*/
public static Properties loadConfiguration(BufferedReader reader) throws IOException {
Properties config = new Properties();
config.load(reader);
return config;
}
/**
* Add configurations from one properties file to another
* @param to Properties file to copy values to
* @param from Properties file to copy values from
* @return a map containing the results of the values added
*/
public static Map<String, String> addConfiguration(Properties to, Properties from) {
Map<String, String> results = from.entrySet()
.stream()
.filter(entry -> !to.containsKey(entry.getKey()))
.map(entry -> {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
to.put(key, value);
return new Pair<String, String>(key, value);
})
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2()));
return results;
}
/**
* Removes configuration values that are not present in the comparedTo
* @param from Properties file to remove values from
* @param comparedTo Properties file to compare to
* @return a map containing the results of the values removed
*/
public static Map<String, String> removeConfiguration(Properties from, Properties comparedTo) {
Map<String, String> results = from.keySet()
.stream()
.filter(key -> !comparedTo.containsKey(key)) //only get the items that are not in the comparedTo properties
.map(key -> new Pair<String, String>(String.valueOf(key), String.valueOf(from.get(key))))
.collect(Collectors.toList()) //Create a list of paired items (key value) of the items that were filtered
.stream()
.map(pair -> { //remove those pairs from the from properties
from.remove(pair.getValue1());
return pair;
})
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); //create a map of what was removed
return results;
}
/**
* Save the properties file to disk
* @param props Properties file to save
* @param fileToSave File to save to
* @param comment Any comments to add
*/
public static void saveConfiguration(Properties props, File fileToSave, String comment) {
try(FileOutputStream fos = new FileOutputStream(fileToSave)) {
props.store(fos, comment == null ? "" : comment);
} catch (IOException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
}